C#套接字跳过第二个接收

时间:2019-05-13 02:24:13

标签: c# visual-studio sockets

我有一个程序需要客户端向它发送一个ip +端口(用于参考程序),否则它将完全跳过第二个接收并直接进入发送

    static void Main(string[] args)
    {
            //---listen at the specified IP and port no.---
            IPAddress localAdd = IPAddress.Parse(SERVER_IP);
            TcpListener listener = new TcpListener(localAdd, PORT_NO);
            Console.WriteLine("Listening...");
            listener.Start();

            //---incoming client connected---
            TcpClient client = listener.AcceptTcpClient();

            //---get the incoming data through a network stream---
            NetworkStream nwStream = client.GetStream();
            byte[] buffer = new byte[client.ReceiveBufferSize];

            //---read incoming stream---
            int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

            //---convert the data received into a string---
            string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Received : " + dataReceived);


            NetworkStream nwStream2 = client.GetStream();
            byte[] buffer2 = new byte[client.ReceiveBufferSize];

            //---read incoming stream---
            int bytesRead2 = nwStream.Read(buffer2, 0, client.ReceiveBufferSize);

            //---convert the data received into a string---
            string dataReceived2 = Encoding.ASCII.GetString(buffer2, 0, bytesRead2);
            Console.WriteLine("Received : " + dataReceived2);

            //---write back the text to the client---
            Console.WriteLine("Sending back : " + dataReceived);
            nwStream.Write(buffer, 0, bytesRead);
            client.Close();
            listener.Stop();
            Console.ReadLine();
    }

当前的实际结果是

Listening...

Received : 9050

Received :

Sending back : 9050

我试图让第二个接收值是一个值,但它会跳过它

0 个答案:

没有答案