C#TCP客户端不能让行超过1消息 [英] C# TCP Client Cant get more than 1 message in row

查看:226
本文介绍了C#TCP客户端不能让行超过1消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个应用程序,可以连接到我的服务器。一切进展顺利,但我有一个问题,当该服务器同时将消息发送到客户端。如服务器行发送2消息作为。客户刚收到的第一个。 ?是否有可能得到行多个消息



下面是我的客户端代码的一部分:

  TcpClient的ClientSocket的; 
公共字符串IPS =### ### ### ###。
公众诠释插座= ####;

公共无效ConnectingToServer()
{
ClientSocket的=新的TcpClient();
clientSocket.Connect(IPS,插座);
如果(clientSocket.Connected)
{
serverStream = clientSocket.GetStream();
字节[] = outStream System.Text.Encoding.ASCII.GetBytes();
serverStream.Write(outStream,0,outStream.Length);
serverStream.Flush();
}
}

//函数用于发送数据到服务器。
公共无效SendDataToServer(字符串StrSend)
{
如果(clientSocket.Connected)
{
字节[] = outStream System.Text.Encoding.ASCII.GetBytes( StrSend);
serverStream.Write(outStream,0,outStream.Length);
serverStream.Flush();
}
}

//功能从服务器接收数据(我把这个循环)。
公共无效的getMessage()
{
如果(ClientSocket的!= NULL)
{
如果(clientSocket.Connected)
{
如果(serverStream.DataAvailable)
{
INT BUFFSIZE = 0;
BUFFSIZE = clientSocket.ReceiveBufferSize;
字节[] = inStream中新的字节[BUFFSIZE]
serverStream.Read(插播广告,0,BUFFSIZE);
串StrReceive = System.Text.Encoding.ASCII.GetString(插播广告);
}
}
}
}


解决方案

发送/接收插座的功能并不保证所有的你提供数据将在一个呼叫发送/接收的 。该函数返回发送/接收的实际字节数。在你的情况,你的不能忽视 serverStream.Read(...)方法调用的结果。



这就是为什么应用层协议的设计应交换的东西(你称之为信息)。



有许多方法设计协议,但让我们考虑下面的消息协议为例:

  -------- -------------------------------------------- 
|字符串的字节数|字节的字符串以UTF-8 |
---------------------------------------------- ------
| 1字节| 2 - ...字节|
---------------------------------------------- ------

发出通知:应当将字符串转换为 UTF-8(例如)表示并与字节表示的字节长度(如上所述)寄的。



接收消息:接收数据到内存缓冲区。提取消息的过程是相反的发送的。当然,你可以一次获得多个消息,如此彻底地处理缓冲区。



示例



我刚才写小文与代码示例


I have build a app that can connect to my server. Everything is running smoothly, but I have a problem when the server send message to client simultaneously. Such as when the server sends 2 messages in row. The client just receives the first one. Is there possible to get more than one message in row?

Here is my part of code for client:

TcpClient clientSocket;
public string IPS= "###.###.###.###";
public int SocketS = ####;

public void ConnectingToServer()
{
    clientSocket= new TcpClient();
    clientSocket.Connect(IPS, SocketS);
    if (clientSocket.Connected)
    {
        serverStream = clientSocket.GetStream();
        byte[] outStream = System.Text.Encoding.ASCII.GetBytes();
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();
    }
}

// Function for send data to server.
public void SendDataToServer(string StrSend)
{
    if (clientSocket.Connected)
    {
        byte[] outStream = System.Text.Encoding.ASCII.GetBytes(StrSend);
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();
    }
}

// Function for receive data from server (I put this in looping).
public void getMessage()
{
    if (clientSocket != null)
    {
        if (clientSocket.Connected)
        {
            if (serverStream.DataAvailable)
            {
                int buffSize = 0;
                buffSize = clientSocket.ReceiveBufferSize;
                byte[] inStream = new byte[buffSize];
                serverStream.Read(inStream, 0, buffSize);
                string StrReceive= System.Text.Encoding.ASCII.GetString(inStream);
            }
        }
    }
}

解决方案

The send/receive functions of socket do not guarantee that all the data you provided will be sent/received at one call. The functions return actual number of sent/received bytes. In your case, you must not ignore the result of the serverStream.Read(...) method call.

That is why application-level protocol should be designed to exchange the things (you call it "messages").

There are many approaches to design the protocol, but let's consider the following "message" protocol as an example:

----------------------------------------------------
| Number of string bytes | String bytes in UTF-8   |
----------------------------------------------------
| 1 byte                 | 2 - ... bytes           |
----------------------------------------------------

Sending the "message": the string should be converted to UTF-8 (for example) representation and sent it with the byte length of the byte representation (as described above).

Receiving the message: receive the data to memory buffer. The process of extracting the "message" is opposite to the sending ones. Of course, you can receive more than one "message" at once, so process the buffer thoroughly.

Example

I have just written a small article with code example.

这篇关于C#TCP客户端不能让行超过1消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆