UdpClient udpClient = new UdpClient();
            string sendMSG = "测试信息";
            byte[] sendBytes = Encoding.ASCII.GetBytes(sendMSG);
            udpClient.Send(sendBytes, sendBytes.Length, "CAT", 2112);
            IPAddress ip=IPAddress.Parse("10.187.202.41");
            IPEndPoint endPoint = new IPEndPoint(0,0);
            byte[] rcvBytes = udpClient.Receive(ref endPoint);
            string recieveMSG = Encoding.ASCII.GetString(rcvBytes, 0, rcvBytes.Length);
            Console.WriteLine(recieveMSG);
直接抛出SocketException的异常
远程主机强迫关闭了一个现有的连接。
不知道什么情况,请各位高手指点。

解决方案 »

  1.   

    IPAddress ip=IPAddress.Parse("10.187.202.41");
    ip没用?
      

  2.   

    IPEndPoint endPoint = new IPEndPoint(0,0);
    有问题!
    把IP地址和端口号进去...
      

  3.   


    //ipIP地址
    //11004 设置的端口号
    IPEndPoint ipEndPoint = new IPEndPoint(ip, 11004);  
      

  4.   

    我问下2楼,UDP传输数据对端口有要求么,就是我这个机子必须开着什么端口啊什么的,
      

  5.   

    udpClient.Send(sendBytes, sendBytes.Length, "CAT", 2112);发送的 udpClient 对象也要IP,端口来进行初始化啊
      

  6.   

    IPEndPoint endPoint = new IPEndPoint(0,0); 
    这里没给 Ip和端口
      

  7.   


    UdpClient udpClient = new UdpClient();
                string sendMSG = "测试信息";
                byte[] sendBytes = Encoding.ASCII.GetBytes(sendMSG);
                udpClient.Send(sendBytes, sendBytes.Length, "10.187.202.41", 123);
                IPAddress ip = IPAddress.Parse("10.187.202.41");
                IPEndPoint endPoint = new IPEndPoint(ip,123);
                byte[] rcvBytes = udpClient.Receive(ref endPoint);
                string recieveMSG = Encoding.ASCII.GetString(rcvBytes, 0, rcvBytes.Length);
                Console.WriteLine(recieveMSG);
    我改变了下代码,把我自己的IP加进入了,当用不同的端口有两种不同的情况,我用netstat/an命令查找了下自己的端口
    123是里面有的UDP端口我用这个测试代码,到了接受那里就会一直接受,等好久都没反应,不知道什么情况,如果输入命令后没有找到的端口依然报原来的错误:SocketException的异常 
    远程主机强迫关闭了一个现有的连接。 
    请问这是怎么回事...头大了,
      

  8.   

    87708849 c# P2P技术讨论群,加进来一起讨论
      

  9.   

    会楼上...我这里不能上QQ,郁闷死了
    不过我自己刚才试出来了,我在UdpClient类初始化时给它了一个端口的构造函数,然后下面都用那个端口然后就发送成功了。
    后来我把发送和接受分成两个程序,发现在收时要指定端口,请问这是为什么?而且我发现发送时端口是递增的请问这个为什么呢?
    附上我改好的代码:接受的代码
    UdpClient udpClient = new UdpClient(2112);
                    IPAddress ip = IPAddress.Parse("10.187.202.41");
                    IPEndPoint endPoint = new IPEndPoint(0,0);
                    byte[] rcvBytes = udpClient.Receive(ref endPoint);
                    string recieveMSG = Encoding.ASCII.GetString(rcvBytes, 0, rcvBytes.Length);
                    udpClient.Close();
                    Console.WriteLine(endPoint.Address + "|" + endPoint.Port);
                    Console.WriteLine(recieveMSG);发送的代码:
    UdpClient udpClient = new UdpClient();
                string sendMSG = "aaa";
                byte[] sendBytes = Encoding.ASCII.GetBytes(sendMSG);
                udpClient.Send(sendBytes, sendBytes.Length, "10.187.202.41", 2112);
      

  10.   

    public int errint=0;//错误记数
            public bool Get(string LocalFile, string RemoteFile, string Host,int port,
                Modes Mode, int BlockSize, int Timeout,int ReErr)
            {
                int recvLen, remoteFileSize = 0, buffer = BlockSize + 4;
                long bytesReceived = 0;
                System.IO.Stream filestream;
                long packnum=0,stapack=0;
                TFTPSession.OpCodes opCode = new TFTPSession.OpCodes();
                byte[] sendData;
                byte[] recvData;
                Byte[] bufferb=new byte[512];
                IPHostEntry hInfo = Dns.GetHostEntry(Host);
                IPAddress address = hInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(address, port);
                EndPoint localEP = (remoteEP);
                Socket UDPSock = new Socket
                    (remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                UDPSock.ReceiveTimeout = Timeout * 1000;
                // Create initial request and buffer for response
                     sendData = _packetBuilder.Request(TFTPSession.OpCodes.RRQ,
                         RemoteFile, Mode, BlockSize, 0, Timeout);
                     recvData = new byte[BlockSize + 4];
                     // Send request and wait for response
                     UDPSock.SendTo(sendData, remoteEP);            recvLen = UDPSock.ReceiveFrom(recvData, ref localEP);