简体   繁体   English

C#套接字。 只能收到第一条消息

[英]C# socket. Only able to receive first message

I am having some trouble figuring out why I am only receiving one reply from a server application running on my computer(LocalHost). 我在弄清楚为什么我只收到来自我的计算机上运行的服务器应用程序(LocalHost)的一个回复时遇到了一些麻烦。 I do not have the source for this server application but it is a java application. 我没有此服务器应用程序的源代码,但它是一个Java应用程序。 Messages that is sent is a xml structure and have to end with a EoT tag. 发送的消息是xml结构,必须以EoT标记结束。

The communication: 沟通:

  1. Client connect to sever. 客户端连接到服务器。
  2. Client sends message to server. 客户端向服务器发送消息。
  3. Server sends message recived to client. 服务器将收到的消息发送给客户端。
  4. Client sends message to server. 客户端向服务器发送消息。
  5. Server sends a End of Transmission character. 服务器发送传输结束字符。
  6. Client sends message to server. 客户端向服务器发送消息。
  7. Server sends a End of Transmission character. 服务器发送传输结束字符。

This is how my client connect, send and receive: 这是我的客户端连接,发送和接收的方式:

public bool ConnectSocket(string server, int port)
{
System.Net.IPHostEntry hostEntry = null;

    try
    {
        // Get host related information.
        hostEntry = System.Net.Dns.GetHostEntry(server);
    }
    catch (System.Exception ex)
    {
            return false;
    }


    // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
    // an exception that occurs when the host IP Address is not compatible with the address family
    // (typical in the IPv6 case).
    foreach (System.Net.IPAddress address in hostEntry.AddressList)
    {
            System.Net.IPEndPoint ipe = new System.Net.IPEndPoint(address, port);
            System.Net.Sockets.Socket tempSocket = new System.Net.Sockets.Socket(ipe.AddressFamily, System.Net.Sockets.SocketType.Stream, 
                                                                                 System.Net.Sockets.ProtocolType.Tcp);
            tempSocket.Connect(ipe);

            if (tempSocket.Connected)
            {
                m_pSocket = tempSocket;
                m_pSocket.NoDelay = true;
                return true;
            }
            else
                continue;
        }
        return false;
    }
}

public void Send(string message)
{
    message += (char)4;//We add end of transmission character
    m_pSocket.Send(m_Encoding.GetBytes(message.ToCharArray()));
}

private void Recive()
{
    byte[] tByte = new byte[1024];
    m_pSocket.Receive(tByte);
    string recivemessage = (m_Encoding.GetString(tByte));
}

Your Receive code looks very wrong; 您的Receive代码看起来非常错误; you should never assume that packets arrive in the same constructions that the server sends messages - TCP is just a stream. 你永远不应该假设数据包到达服务器发送消息的相同结构 - TCP只是一个流。 So: you must catch the return from Receive , to see how many bytes you received. 所以:你必须Receive返回,看看你收到了多少字节。 It could be part of one message, an entire message, multiple entire messages, or the last half of one message and the first half of the next. 它可以是一条消息的一部分,一条完整的消息,多条完整的消息,或者一条消息的后半部分和下一条消息的前半部分。 Normally, you need some kind of "framing" decision, which could mean "messages split by LF characters", or could mean "the length of each message is prefixed by network-byte-order integer, 4 bytes". 通常,您需要某种“框架”决策,这可能意味着“由LF字符拆分的消息”,或者可能意味着“每条消息的长度以网络字节顺序整数为前缀,4个字节”。 This usually means you need to buffer until you have a full frame, and worry about spare data at the end of the buffer that is part of the next frame. 这通常意味着您需要缓冲,直到您有一个完整的帧, 担心作为下一帧的一部分的缓冲区末尾的备用数据。 Key bits to add, though: 但是要添加的关键位:

int bytes = m_pSocket.Receive(tByte);
// now process "bytes" bytes **only** from tByte, nothing that this
// could be part of a message, an entire message, several messages, or
// the end of message "A", the entire of message "B", and the first byte of
// message "C" (which might not be an entire character)

In particular, with text formats, you cannot start decoding until you are sure you have an entire message buffered, because a multi-byte character might be split between two messages. 特别是,对于文本格式,在确定要缓冲整个消息之前, 不能开始解码,因为多字节字符可能在两个消息之间分开。

There may well also be problems in your receive loop, but you don't show that (nothing calls Receive ), so we can't comment. 您的接收循环中也可能存在问题,但您没有显示(没有任何调用Receive ),因此我们无法发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM