import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.io.*;
import java.net.*;
public class DataSocketSer 
{
public static void main(String[] args) 
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label("    111111111111      ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]="wwwwwwwww".getBytes();
InetAddress address=null;
try
{
address=InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException e)
{
e.getMessage();
}
DatagramPacket dp =new DatagramPacket(b,6,address,4700);
try
{
da.send(dp);
}
catch( IOException e)
{
e.getMessage();
}
}
}import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.io.*;
import java.net.*;
public class DataSocketSer 
{
public static void main(String[] args) 
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label("    111111111111      ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]="wwwwwwwww".getBytes();
InetAddress address=null;
try
{
address=InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException e)
{
e.getMessage();
}
DatagramPacket dp =new DatagramPacket(b,6,address,4700);
try
{
da.send(dp);
}
catch( IOException e)
{
e.getMessage();
}
}
}我先打开第一个程序,再打第二个程序,但是没什么反应,这是怎么一回事

解决方案 »

  1.   

    我是楼主,上面程序发错了,其中一个应该是:
    import java.net.*;
    import java.awt.*;
    import java.io.*;
    public class DataSocket 
    { public static void main(String[] args) 
    {
    String str="";
    Frame f=new Frame();
    f.setSize(355, 355);
    f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
    Label l=new Label("    111111111111      ");
    f.add(l);
    f.setVisible(true);
    DatagramSocket da=null;
    try
    {
    da=new DatagramSocket(4700);
    }
    catch(SocketException e)
    {
    e.getMessage();
    }
    byte b[]=new byte[1024];
    DatagramPacket dp=new DatagramPacket(b,6);
    try
    {
    da.receive(dp);
    }
    catch(IOException e)
    {
    e.getMessage();
    }
    l.setText(new String(dp.getData()));

    }}
      

  2.   

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.net.*;
    public class server extends JFrame implements Runnable,ActionListener{JTextField out;
    JTextArea text;
    JPanel p1;
    JButton btn;
    JScrollPane textPanel;
    Thread tr;
     InetAddress ip = null;//  构造方法:
     
     public server(){
      Container c = getContentPane();
      out = new JTextField(15);
      text = new JTextArea(30,30);
      btn = new JButton("确定");
      textPanel = new JScrollPane(text);
      text.setEditable(false);
      text.setLineWrap(true);
      p1 = new JPanel();
      p1.add(out);
      p1.add(btn);
      out.addActionListener(this);
      btn.addActionListener(this);
      c.add(textPanel,"Center");
      c.add(p1,"South");
      setSize(400,400);
      setVisible(true);
      tr = new Thread(this);
      tr.start();
      addWindowListener(new WindowAdapter(){                     public void windowClosing(WindowEvent we){
                                 System.exit(0);}
                         });
     }
     public void actionPerformed(ActionEvent ae){
     
     if((ae.getSource() == btn) || (ae.getSource() == out)){  byte buffer[] = (out.getText()).trim().getBytes();
      try{
          
         ip = InetAddress.getByName("127.0.0.1");
       DatagramPacket da = new DatagramPacket(buffer,buffer.length,ip,2006);
       DatagramSocket mail = new DatagramSocket();
       text.append("输入:" + out.getText() + '\n');
       mail.send(da);
       }catch(Exception ie){}
      out.setText(null);
      }
     
     } //  接收数据报线程体:
      public void run(){  DatagramPacket pack = null;
      DatagramSocket mail = null;
      byte data[] = new byte[8192];
      try{
          pack = new DatagramPacket(data,data.length);
          mail = new DatagramSocket(2006);
       }catch(Exception ie){} while(true){  if(mail == null)
       break;
      else
       try{
            mail.receive(pack);
            int len = pack.getLength();
            ip = pack.getAddress();
            String datas = new String(pack.getData(),0,len);
            Thread.sleep(3000);
           this.text.append("接收:" + datas +'\n');
         }catch(Exception ie){}
       } } public static void main(String args[]){
           
           server ser = new server();
     } } 
      看你的都晕了,这是我前年练习的时间写的自发自收的小程序,接收是用线程实现的,参考下,