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的异常
远程主机强迫关闭了一个现有的连接。
不知道什么情况,请各位高手指点。
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的异常
远程主机强迫关闭了一个现有的连接。
不知道什么情况,请各位高手指点。
ip没用?
有问题!
把IP地址和端口号进去...
//ipIP地址
//11004 设置的端口号
IPEndPoint ipEndPoint = new IPEndPoint(ip, 11004);
这里没给 Ip和端口
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的异常
远程主机强迫关闭了一个现有的连接。
请问这是怎么回事...头大了,
不过我自己刚才试出来了,我在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);
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);