0

当我通过 TCP 服务器将数据发送到客户端第一次检索全名时,当我再次发送另一个名称(如在 C 语言中检索为(OK)的书时)

客户端代码

sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 而(真){

            IPEndPoint localEndPoint;
            localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 1);

            try
            {
                sck.Connect(localEndPoint);

            }
            catch
            {
                Console.Write("Unable to connect to remote end point!\r\n");
             );
            }
            Console.Write("Enter Text: ");
            string text = Console.ReadLine();
            byte[] data = Encoding.ASCII.GetBytes(text);
            sck.Send(data);
            Console.Write("Data Sent!\r\n");
            sck.Close();
            Console.ReadLine();

服务器端代码

while (true) { 接受的套接字 = sck.Accept();

                Buffer = new byte[accepted.SendBufferSize];
                int bytesRead = accepted.Receive(Buffer);

                byte[] formatted = new byte[bytesRead];


                for (int i = 0; i < bytesRead; i++)
                {
                    formatted[i] = Buffer[i];
                    //Console.WriteLine(Buffer[i] + "\r\n");
                }
                //string strData = Encoding.ASCII.GetString(Buffer);
                string strData = Encoding.ASCII.GetString(formatted);

                Console.Write(strData + "\r\n");
                accepted.Close();
            }
4

1 回答 1

0

有些东西我不明白,你使用 Buffer 作为变量,但这是 C# 中的一种类型,我不知道你在哪里以及如何声明它。无论如何,我会这样做:

    while (true)
    {
        Socket accepted = sck.Accept();
        byte[] buffer = new byte[1024];
        int ret=0;
        StringBuilder sb= new StringBuilder();
        while ((ret=accepted.Receive(buffer)) > 0)
        {
            sb.Append(Encoding.ASCII.GetString(buffer, 0, ret));
        }
        Console.Write(sb.ToString() + "\r\n");
        accepted.Close();
    }

为了在两个套接字之间保持对话,您需要一种称为协议的东西,这意味着您知道何时收到了全部数据,然后回复发送者。一个协议可以是:发送一个数据包,通知你要发送什么,主要是消息的长度。另一种方法是在消息中出现某些内容时停止阅读,例如字符串“#END#”返回字符“\r”等。如果您只想发送一条消息并收到回复消息,那么您的一件事就是然而,可以做的是关闭客户端部分,只有发送部分Socket.Shutdown(SocketShutdown.Send);,发送者告诉接收者它已经发送了所有数据,接收者接收到该信号,然后它可以向发送者发送回复消息。

客户端:

    sck.Send(data);
    Console.Write("Data Sent!\r\n");
    // tell the server we have done sending data.
    sck.Shutdown(SocketShutdown.Send);
    // Now receive from the server
    byte[] buffer = new byte[1024];
    int ret = 0;
    StringBuilder sb = new StringBuilder();
    while ((ret = sck.Receive(buffer)) > 0)
    {
        sb.Append(Encoding.ASCII.GetString(buffer, 0, ret));
    }
    Console.Write(sb.ToString() + "\r\n");
    sck.Close();

服务器

    while (true)
    {
        Socket accepted = sck.Accept();
        byte[] buffer = new byte[1024];
        int ret=0;
        StringBuilder sb= new StringBuilder();
        while ((ret=accepted.Receive(buffer)) > 0)
        {
            sb.Append(Encoding.ASCII.GetString(buffer, 0, ret));
        }
        Console.Write(sb.ToString() + "\r\n");
        byte[] reply = Encoding.ASCII.GetBytes("I've got it!");
        accepted.Send(reply);
        accepted.Close();
    }
于 2013-07-17T10:02:34.623 回答