我写了一个客户端和服务端,当开启服务端后,开启客户端输入数据,服务器端收不到。import java.io.*;
import java.net.*;
class UdpSend 
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(); BufferedReader bufr = 
new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line=bufr.readLine())!=null)
{
byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.6.70"),10004); ds.send(dp); } ds.close();
}
}
class UdpRece
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(10004); while(true)
{ byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); String data = new String(dp.getData(),0,dp.getLength()); System.out.println(ip+"::"+data);
} }}
我的ip地址是192.168.6.70,前一阵子是可以用的,后来我改过一次ip,然后做表单提交的时候,
要提交到本地,然后把ip改回了192.168.6.70,<form action="http://192.168.6.70:10009">
写了个本地的服务端,端口10009打印一段字符串,然后也是连接不上,不知道怎么回事,是不是ip
设置问题?还是我改了什么地方了?

解决方案 »

  1.   

    http://my.csdn.net/peng_hao1988/code/detail/26828
    给个例子。
      

  2.   

    这样就行了:
    import java.io.*;
    import java.net.*;
    public class UdpRece
    {
            public static void main(String[] args) throws Exception
            {
                DatagramSocket ds = new DatagramSocket(8000);
     
                while(true)
                {
     
                    byte[] buf = new byte[1024];
                    DatagramPacket dp = new DatagramPacket(buf,buf.length);
     
                    ds.receive(dp);
     
                    String ip = dp.getAddress().getHostAddress();
     
                    String data = new String(dp.getData(),0,dp.getLength());
     
                    System.out.println(ip+"::"+data);
                }
     
            }
     
    }
    import java.io.*;
    import java.net.*;
    public class UdpSend 
    {
        public static void main(String[] args) throws Exception
        {
            DatagramSocket ds = new DatagramSocket();
     
            BufferedReader bufr = 
                new BufferedReader(new InputStreamReader(System.in));
     
            String line = null;
     
            while((line=bufr.readLine())!=null)
            {
                byte[] buf = line.getBytes();
     
                DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),8000);
     
                ds.send(dp);
     
            }
     
            ds.close();
        }
    }