简体   繁体   中英

Could not send second message over TCP/IP

I am trying to send message over TCP/IP in c# application using TCPClient and TCPListner classes

Following is my code which i got from codeproject site.

Client code written over btn click

try
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");

            tcpclnt.Connect("192.168.0.102", 8001);
            // use the ipaddress as in the server program

            Console.WriteLine("Connected");
            //Console.Write("Enter the string to be transmitted : ");

            String str = textBox1.Text;
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));


            tcpclnt.Close();
        }

        catch (Exception ex)
        {
            Console.WriteLine("Error..... " + ex.Message);
        }

Server code written on form_load

try
        {
            IPAddress ipAd = IPAddress.Parse("192.168.0.102");
            // use local m/c IP address, and 
            // use the same in the client

            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 8001);

            /* Start Listeneting at the specified port */
            myList.Start();

            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is  :" +
                              myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");

            Socket s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);
            Console.WriteLine("Recieved...");
            string str = string.Empty;
            for (int i = 0; i < k; i++)
            {
                Console.Write(Convert.ToChar(b[i]));
                str = str + Convert.ToChar(b[i]);

            }
            label1.Text = str;
            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\nSent Acknowledgement");
            /* clean up */
            s.Close();
           // myList.Stop();

        }

Here on client , im sending string written in textbox over tcp and it well recieved by server .

But when i trying to send another string, it fails without any exception and client application hangs for infinite time.

What is wrong here ?

Looking at the code you provided, the server only attempts to read 1 message from the client, so needs to be put in a loop to read the multiple incoming messages from the client, process the message and send response and then get more messages.

Note also that the server currently expects only one client to ever connect, processes that client and then closes.

The client is set up basically the same in the example, so you cant modify how one works without also modifying the other.

Server should always be in listening mode, ie the server code should be in while loop such that it can accept clients continuously. Your server will accept one client and then close itself. Therefore, if you click client's button, a new client tries to connect to server, but now the server won't be available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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