从套接字接收数据时清空消息

时间:2013-08-04 14:23:06

标签: c# sockets ussd

我正在尝试使用Socket类连接到UAP服务器(用于在华为发送和接收USSD消息)但从服务器接收数据时出现问题。我可以正确连接到服务器,也可以将数据发送到服务器,但是接收数据时遇到问题。

这是我连接和发送消息的代码:

IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); //server
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 2020);

clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

clientSocket.Connect(remoteEP);

//bind to socket, here I send bind message
Bind();

Bind函数内部,我调用receive方法从服务器获取数据:

private static void receive()
{
    try
    {
        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = clientSocket;

        // Begin receiving the data from the remote device.
        clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(receiveCallback), state);

    }
    catch (Exception ex)
    {
        Console.WriteLine("receive | " + ex.ToString());
    }

}//receive

在这里,您可以在连接,发送数据和从服务器接收数据时看到我的Wireshark日志:

Wireshark log

但正如您所看到的,来自服务器的最后两条消息以红色突出显示,这意味着从服务器接收数据时出现问题。另外,我没有详细查看data字段:

detail of a message

我真的不知道是什么问题,这是服务器的问题吗?我是否应该使用另一个Socket客户端来连接和接收来自服务器的数据?

1 个答案:

答案 0 :(得分:0)

TCP重置数据包不是您的应用程序级协议的一部分,您的应用程序不应该看到它们。

您的应用程序的错误行为究竟是什么,因为您发布的内容我没有看到任何问题。