C#udp客户端没有收到回复

时间:2015-10-15 19:33:17

标签: c# c sockets udp

我有用C编写的udp服务器,它的工作原理如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/sendfile.h>

typedef struct sockaddr_in sockaddr_in;
typedef struct sockaddr sockaddr;

void* handle_client_thread(void* client_sock){
    sockaddr_in* client_addr = (sockaddr_in*)client_sock;
    sockaddr_in server_sock_info;
    int desc = socket(AF_INET,SOCK_DGRAM,0);
    bzero(&server_sock_info, sizeof(sockaddr_in));
    server_sock_info.sin_addr.s_addr = INADDR_ANY;
    server_sock_info.sin_family = AF_INET;
    server_sock_info.sin_port = htons(0);
    bind(desc, (sockaddr*)&server_sock_info, sizeof(sockaddr_in));

    const int buf_size = 1024;
    char buf[1024] = "msg2";
    sendto(desc,buf,strlen(buf),0, (sockaddr*)client_addr, sizeof(sockaddr_in));

    usleep(1000000);
    close(desc);
    free(client_sock);
}


int main(int argc, char** argv) {
    sockaddr_in server_sock_info;
    int desc = socket(AF_INET,SOCK_DGRAM,0);
    bzero(&server_sock_info, sizeof(sockaddr_in));
    server_sock_info.sin_addr.s_addr = INADDR_ANY;
    server_sock_info.sin_family = AF_INET;
    server_sock_info.sin_port = htons(8080);
    bind(desc, (sockaddr*)&server_sock_info, sizeof(sockaddr_in));

    const int buf_size = 256;
    char buf[256] = {0};

    while(1){
        sockaddr_in* th_data = (sockaddr_in*)malloc(sizeof(sockaddr_in));
        int sz = sizeof(sockaddr_in);
        memset(buf, 0 ,sizeof(buf));
        recvfrom(desc,buf,buf_size,0,(sockaddr*)th_data, &sz);

        pthread_attr_t attr;
        pthread_t pubThread;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
        pthread_create (&pubThread, &attr, handle_client_thread, th_data);
    }

    close(desc);
    return EXIT_SUCCESS;
}

我用C编写的客户端对此进行了测试,并且工作正常。

现在我的C#客户端的工作方式如下:

private async void InitiateMainConnection()
{
    await Task.Run(() =>
    {
        try {
            string toSend = "ack";
            MainSocket.Send(toSend.ToBytes(),toSend.Length,MainRemoteEndPoint);
            //MainRemoteEndPoint = new IPEndPoint(IPAddress.Any, ((IPEndPoint)MainSocket.Client.LocalEndPoint).Port);
            while (true)
            {
                byte[] buf = MainSocket.Receive(ref MainRemoteEndPoint);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.searchKey.Text = buf.ToString(true);
                }));
            }

        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message);
        }

    });
}

private void connectMain_Click(object sender, RoutedEventArgs e)
{
    MainSocket = new UdpClient();

    // Connect to the remote endpoint.
    try {
        MainRemoteEndPoint = new IPEndPoint(IPAddress.Parse(mainIP.Text), int.Parse(mainPort.Text));
        InitiateMainConnection();

        //var c = new UdpClient();
        //Thread.Sleep(5000);
        //c.Send("sadas".ToBytes(), 5, new IPEndPoint(IPAddress.Parse("127.0.0.1"), ((IPEndPoint)MainSocket.Client.LocalEndPoint).Port));
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

它无法接收从C服务器的线程中创建的套接字发送的消息,但是如果我从实际接收客户端数据的套接字发送消息,则它正在正确接收消息。

我在Wireshark中观察过数据包,数据包到达客户端机器但客户端代码没有收到它。我该如何纠正?

0 个答案:

没有答案