C#UDP服务器客户端未接收彼此的消息?

发布于 2025-02-01 14:48:29 字数 3843 浏览 2 评论 0原文

因此,对于我的UDP C#聊天应用程序,我有一个服务器程序,该程序旨在通过侦听客户端将其发送到端口8001上,当它从客户端接收ACKNOLWEDEND时,它应该将消息发送回到客户。但是,尽管客户端和服务器都说他们正在互相收听,但服务器始终立即报告它将消息发送到IP地址0.0.0.0,因此它们不链接,以下是client和Server的代码:服务器

: :

const int listenPort = 8001;


    bool done = false;
UdpClient listener = new UdpClient(listenPort);


try
    {
    while (!done)
    {

string text_to_send = satt.Text;
    if (text_to_send.Length == 0)
    {
    done = true;
    }
    else
    {
    byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);





try
{

string IPAddress1 = "";
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList)
{
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPAddress1 = Convert.ToString(IP);
}
else

{
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
string data_recieved;
byte[] recieve_byte_array;
try
{
while (!done)
{
Console.WriteLine("Looking for attack buddies " + groupEP);
recieve_byte_array = listener.Receive(ref groupEP);
Console.WriteLine("New buddy found at + " + groupEP.ToString());
data_recieved = Encoding.ASCII.GetString(recieve_byte_array, 0, recieve_byte_array.Length);
Console.WriteLine(data_recieved);
}
}

catch (Exception n)
                                    {
                                        Console.WriteLine("Attack information could not be sent. Please make sure your buddies are reachable" + n.ToString());
}
}
}
}
catch (Exception j)
{
Console.WriteLine("Please make sure all needed information is filled out");
}

客户端:

const int listenPort = 8001;


            bool done = false;
            UdpClient listener = new UdpClient(listenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            string data_recieved;
            string searchcount_sent = searchcount.ToString();
            byte[] send_byte_array;
            byte[] recieve_byte_array;
            try
            {
                while (!done)


                {
           string IPAddress1 = "";
           IPHostEntry Host = default(IPHostEntry);
           string Hostname = null;
           Hostname = System.Environment.MachineName;
           Host = Dns.GetHostEntry(Hostname);
           foreach (IPAddress IP in Host.AddressList)
           {
           if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
             IPAddress1 = Convert.ToString(IP);
              }
              }
            string text_to_send = "Request to connect from: " + IPAddress1;
            if (text_to_send.Length == 0)
            {
            done = true;
             }
            else

            {
                       
          MessageBox.Show("Now listening on port 8001.");
          Socket sending_socket = new Socket(AddressFamily.InterNetwork,       SocketType.Dgram, ProtocolType.Udp);
          IPAddress send_to_address = IPAddress.Parse(RecieveIP.Text);
           IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 8001);
   Console.WriteLine("Now sending out packet to be acknolwedged by lead computer.");

            byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
    Console.WriteLine("Listening for the attack information on port " + listenPort);
    recieve_byte_array = listener.Receive(ref groupEP);
    data_recieved = Encoding.ASCII.GetString(recieve_byte_array, 0, recieve_byte_array.Length);


    try
    {
    sending_socket.SendTo(send_buffer, sending_end_point);
     }
    catch (Exception send_exception)
      {

     Console.WriteLine("Exception {0}", send_exception);
       }
     MessageBox.Show("Listening for attack information.");

两台计算机正在通过ARP-A相互发现。所以想知道为什么他们不会收到彼此的消息?谢谢。

so for my UDP C# chat application, I have a server program which is intended to recieve a response by listening for the client to send it on Port 8001, when it recieves the acknolwedement from the client it is supposed to send a message back to the client. However, though the client and server both say they are listening for each other, the server always immediately reports that it sent the message to IP address 0.0.0.0, so they are not linking, below is the code to the client and server:

Server:

const int listenPort = 8001;


    bool done = false;
UdpClient listener = new UdpClient(listenPort);


try
    {
    while (!done)
    {

string text_to_send = satt.Text;
    if (text_to_send.Length == 0)
    {
    done = true;
    }
    else
    {
    byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);





try
{

string IPAddress1 = "";
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList)
{
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPAddress1 = Convert.ToString(IP);
}
else

{
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
string data_recieved;
byte[] recieve_byte_array;
try
{
while (!done)
{
Console.WriteLine("Looking for attack buddies " + groupEP);
recieve_byte_array = listener.Receive(ref groupEP);
Console.WriteLine("New buddy found at + " + groupEP.ToString());
data_recieved = Encoding.ASCII.GetString(recieve_byte_array, 0, recieve_byte_array.Length);
Console.WriteLine(data_recieved);
}
}

catch (Exception n)
                                    {
                                        Console.WriteLine("Attack information could not be sent. Please make sure your buddies are reachable" + n.ToString());
}
}
}
}
catch (Exception j)
{
Console.WriteLine("Please make sure all needed information is filled out");
}

Client:

const int listenPort = 8001;


            bool done = false;
            UdpClient listener = new UdpClient(listenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            string data_recieved;
            string searchcount_sent = searchcount.ToString();
            byte[] send_byte_array;
            byte[] recieve_byte_array;
            try
            {
                while (!done)


                {
           string IPAddress1 = "";
           IPHostEntry Host = default(IPHostEntry);
           string Hostname = null;
           Hostname = System.Environment.MachineName;
           Host = Dns.GetHostEntry(Hostname);
           foreach (IPAddress IP in Host.AddressList)
           {
           if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
             IPAddress1 = Convert.ToString(IP);
              }
              }
            string text_to_send = "Request to connect from: " + IPAddress1;
            if (text_to_send.Length == 0)
            {
            done = true;
             }
            else

            {
                       
          MessageBox.Show("Now listening on port 8001.");
          Socket sending_socket = new Socket(AddressFamily.InterNetwork,       SocketType.Dgram, ProtocolType.Udp);
          IPAddress send_to_address = IPAddress.Parse(RecieveIP.Text);
           IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 8001);
   Console.WriteLine("Now sending out packet to be acknolwedged by lead computer.");

            byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
    Console.WriteLine("Listening for the attack information on port " + listenPort);
    recieve_byte_array = listener.Receive(ref groupEP);
    data_recieved = Encoding.ASCII.GetString(recieve_byte_array, 0, recieve_byte_array.Length);


    try
    {
    sending_socket.SendTo(send_buffer, sending_end_point);
     }
    catch (Exception send_exception)
      {

     Console.WriteLine("Exception {0}", send_exception);
       }
     MessageBox.Show("Listening for attack information.");

The two computers are finding each other via arp-a. So wondering why they would not be receiving each others message? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文