C# 收购客户';s IP使用IP地址。任何
我目前正在使用一个简单的udp客户机/服务器系统,我偶然发现了以下几点:当我尝试在使用UdpClient的程序中获取IPEndPoint的IP(我使用IPAdress.Any获取)时,它工作正常,我得到了以下结果(顶部): 但是,当我使用普通套接字而不是UdpClient时,它不知何故无法区分客户机/IP(底部)。下面列出了两者的代码。我之所以想使用套接字,是因为在发送和接收时使用同一个类(不是同一个实例)很方便,而且代码更容易理解 首先C# 收购客户';s IP使用IP地址。任何,c#,.net,sockets,networking,udp,C#,.net,Sockets,Networking,Udp,我目前正在使用一个简单的udp客户机/服务器系统,我偶然发现了以下几点:当我尝试在使用UdpClient的程序中获取IPEndPoint的IP(我使用IPAdress.Any获取)时,它工作正常,我得到了以下结果(顶部): 但是,当我使用普通套接字而不是UdpClient时,它不知何故无法区分客户机/IP(底部)。下面列出了两者的代码。我之所以想使用套接字,是因为在发送和接收时使用同一个类(不是同一个实例)很方便,而且代码更容易理解 首先 bool messageReceived=false;
bool messageReceived=false;
bool done=false;
常量int listenPort=11000;
UdpClient listener=新的UdpClient(listenPort);
IPEndPoint receiveEP=新IPEndPoint(IPAddress.Any,listenPort);
字符串[]地址=新字符串[2];
public void ReceiveCallback(IAsyncResult ar)
{
int id=0;
Byte[]receiveBytes=listener.EndReceive(ar,ref receiveEP);
for(int i=0;i
第二
bool messageReceived = false;
bool done = false;
const int listenPort = 11000;
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint receiveEP = new IPEndPoint(IPAddress.Any, listenPort);
string[] adresses = new string[2];
byte[] buffer = new byte[1024];
public void ReceiveMessages()
{
listener.Bind(receiveEP);
Console.WriteLine("listening for messages");
while (!done)
{
try
{
messageReceived = false;
if (!messageReceived)
{
listener.BeginReceive(buffer, 0,1024,SocketFlags.None,new AsyncCallback(ReceiveCallback), null);
}
while (!messageReceived)
{
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine("Done receiving messages...");
for (int i = 0; i < adresses.Length; i++)
{
if (adresses[i] != null)
{
Console.WriteLine(adresses[i]);
}
}
Console.ReadLine();
listener.Close();
}
public void ReceiveCallback(IAsyncResult ar)
{
int id = 0;
int read = listener.EndReceive(ar);
for (int i = 0; i < adresses.Length; i++)
{
if (adresses[i] == receiveEP.Address.ToString())
{
id = i;
break;
}
else if (adresses[i] == null)
{
id = i;
adresses[i] = receiveEP.Address.ToString();
break;
}
}
Console.WriteLine("Received message from {0} (client {1}):", adresses[id], id);
string receiveString = Encoding.ASCII.GetString(buffer,0,read);
if (receiveString == "stop")
{
done = true;
}
Console.WriteLine("Received: {0}", receiveString);
messageReceived = true;
}'
bool messageReceived=false;
bool done=false;
常量int listenPort=11000;
套接字侦听器=新套接字(AddressFamily.InterNetwork、SocketType.Dgram、ProtocolType.Udp);
IPEndPoint receiveEP=新IPEndPoint(IPAddress.Any,listenPort);
字符串[]地址=新字符串[2];
字节[]缓冲区=新字节[1024];
public void ReceiveMessages()
{
Bind(receiveEP);
Console.WriteLine(“监听消息”);
而(!完成)
{
尝试
{
messageReceived=false;
如果(!messageReceived)
{
BeginReceive(缓冲区,01024,SocketFlags.None,新的异步回调(ReceiveCallback),null);
}
而(!messageReceived)
{
}
}
捕获(例外e)
{
Console.WriteLine(如ToString());
}
}
Console.WriteLine(“完成接收消息…”);
for(int i=0;i
我已经尝试过Socket.ReceiveMessageFrom()并使用它返回的packetinfo,但即使从另一台机器发送,我最终也使用了服务器的ip4。有人能帮我吗?尽管这从来都不是一个Q/a类型的问题,但我找到了一种让它工作的方法,这意味着receive()只需使用套接字而不是UdpClient(我模仿了UdpClient的工作方式),即可将groupEP更改为数据包发送方的IP。目前,它仍然是同步的(listener.Receive()是一个阻塞调用),但在不久的将来会发生变化:
private const int listenPort = 11000;
public static int Main()
{
bool done = false;
Listener listener = new Listener(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
byte[] buffer = new byte[2048];
string received_data;
try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
buffer=listener.Receive(ref groupEP);
received_data = Encoding.ASCII.GetString(buffer);
Console.WriteLine("Received a broadcast from {0}: {1}", groupEP.ToString(), received_data);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return 0;
}
}
public class Listener
{
public Socket client;
public AddressFamily adressFamily;
byte[] byte_buffer = new byte[2048];
public int port;
public Listener(int p)
{
port=p;
adressFamily = AddressFamily.InterNetwork;
IPEndPoint localEP;
localEP = new IPEndPoint(IPAddress.Any, port);
client = new Socket(this.adressFamily, SocketType.Dgram, ProtocolType.Udp);
client.Bind(localEP);
}
public byte[] Receive(ref IPEndPoint remoteEP)
{
IPEndPoint ipEndPoint=new IPEndPoint(IPAddress.Any,port);
EndPoint endPoint = (EndPoint)ipEndPoint;
int num = client.ReceiveFrom(byte_buffer, 2048, SocketFlags.None, ref endPoint);
remoteEP = (IPEndPoint)endPoint;
if (num < 2048)
{
byte[] array = new byte[num];
Buffer.BlockCopy(byte_buffer, 0, array, 0, num);
return array;
}
return byte_buffer;
}
private const int listenPort=11000;
公共静态int Main()
{
bool done=false;
侦听器侦听器=新侦听器(listenPort);
IPEndPoint groupEP=新IPEndPoint(IPAddress.Any,listenPort);
字节[]缓冲区=新字节[2048];
接收到的字符串数据;
尝试
{
而(!完成)
{
Console.WriteLine(“等待广播”);
buffer=listener.Receive(ref groupEP);
接收的数据=Encoding.ASCII.GetString(缓冲区);
WriteLine(“接收到来自{0}:{1}的广播”,groupEP.ToString(),接收到_数据);
}
}
捕获(例外e)
{
Console.WriteLine(如ToString());
}
返回0;
}
}
公共类侦听器
{
公共套接字客户端;
公共地址家庭地址家庭;
byte[]byte_buffer=新字节[2048];
公共国际港口;
公共侦听器(INTP)
{
端口=p;
地址家族
private const int listenPort = 11000;
public static int Main()
{
bool done = false;
Listener listener = new Listener(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
byte[] buffer = new byte[2048];
string received_data;
try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
buffer=listener.Receive(ref groupEP);
received_data = Encoding.ASCII.GetString(buffer);
Console.WriteLine("Received a broadcast from {0}: {1}", groupEP.ToString(), received_data);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return 0;
}
}
public class Listener
{
public Socket client;
public AddressFamily adressFamily;
byte[] byte_buffer = new byte[2048];
public int port;
public Listener(int p)
{
port=p;
adressFamily = AddressFamily.InterNetwork;
IPEndPoint localEP;
localEP = new IPEndPoint(IPAddress.Any, port);
client = new Socket(this.adressFamily, SocketType.Dgram, ProtocolType.Udp);
client.Bind(localEP);
}
public byte[] Receive(ref IPEndPoint remoteEP)
{
IPEndPoint ipEndPoint=new IPEndPoint(IPAddress.Any,port);
EndPoint endPoint = (EndPoint)ipEndPoint;
int num = client.ReceiveFrom(byte_buffer, 2048, SocketFlags.None, ref endPoint);
remoteEP = (IPEndPoint)endPoint;
if (num < 2048)
{
byte[] array = new byte[num];
Buffer.BlockCopy(byte_buffer, 0, array, 0, num);
return array;
}
return byte_buffer;
}