写了个客户端,连不上(服务器端没问题),主要代码如下byte[] incomingbuf = new byte[1<<16];
System.Diagnostics.Debug.WriteLine("connecting to port  " + _port);
try
{
    _socket = new System.Net.Sockets.UdpClient(60001);
    _socket.Connect("192.168.2.2", 10011);
    String str="connection request";
    byte[] buf = System.Text.Encoding.ASCII.GetBytes(str.ToCharArray());
    int count = _socket.Send(buf,buf.Length);
    System.Diagnostics.Debug.WriteLine(_port + ":  send a string with the count=" + count);
    System.Net.IPEndPoint ipendpoint = null;
    incommingbuf = _socket.Receive(ref ipendpoint);
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~在这收不到东西了
    System.Diagnostics.Debug.WriteLine(_port + ": connected");
    _isConnected = true;
}
catch
{
     System.Diagnostics.Debug.WriteLine(_port + ":catched an exception while connecting");
}
用java写了个同样的东西 却能收到东西,代码如下
byte[] incomingbuf = new byte[1<<16];
DatagramPacket incoming = new DatagramPacket(incomingbuf,incomingbuf.length);
try
{
_socket=new DatagramSocket();
_socket.connect(InetAddress.getByName(“192.168.2.2”), 10011);
String buf="connection request";
DatagramPacket message = new DatagramPacket(buf, buf.length);
_socket.send(message);
incomingbuf.length);_socket.setSoTimeout(500);
_socket.receive(incoming);
//~~~~~~~~~~~~~~~~~~~可以正常收到数据包
_socket.setSoTimeout(0); //set to be blocking
System.out.println("["+_port+"] connected ");
_isConnected=true;
} catch (Exception ex) {}哪位高手指点一下可能是什么原因吧,急!
谢谢 :)

解决方案 »

  1.   

    不好意思  java代码部分错了 更正如下
    byte[] incomingbuf = new byte[1<<16];
    DatagramPacket incoming = new DatagramPacket(incomingbuf,incomingbuf.length);
    try
    {
    _socket=new DatagramSocket();
    _socket.connect(InetAddress.getByName(“192.168.2.2”), 10011);byte[] buf = (new String("connection request")).getBytes();
    DatagramPacket message = new DatagramPacket(buf, buf.length);
    _socket.send(message);_socket.setSoTimeout(500);
    _socket.receive(incoming);
    //~~~~~~~~~~~~~~~~~~~可以正常收到数据包
    _socket.setSoTimeout(0); //set to be blocking
    System.out.println("["+_port+"] connected ");
    _isConnected=true;
    } catch (Exception ex) {}
      

  2.   

    1.udp协议无须连接
    2.incommingbuf = _socket.Receive(ref ipendpoint); 参数 ipendpoint 为空//
    IPEndPoint rep= new IPEndPoint(你的主机IP,端口号);
    Byte[] receiveBytes = receivingUdpClient.Receive(ref rep);
      

  3.   

    System.Net.IPEndPoint ipendpoint = null;
        incommingbuf = _socket.Receive(ref ipendpoint);
     注意这两句
    不就为null
      

  4.   

    System.Net.IPEndPoint ipendpoint = null;
        incommingbuf = _socket.Receive(ref ipendpoint);
     注意这两句
    不应为null
      

  5.   

    呵呵,luyangph() 看得真切,引用对象不能为空