none
Sending multiple Textmessages and Images via TcpClient RRS feed

  • Question

  • Hey,

    i'm trying to get into Sockets and im struggeling with sending multiple images.

    Server:

            public static void SendImage(int clientID)
            {
                TcpClient client = clientList[clientID];
                NetworkStream nwStream = client.GetStream();
    
                //---write back the text to the client---
                byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes("Sending Image");
                Console.WriteLine("Sending Image Message");
                nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
    
    
                Bitmap img = new Bitmap("mainchar.jpg");
                byte[] bytesToSend = ImageToByte(img);
                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
                nwStream.Dispose();
                Console.WriteLine("Image sent");
                
            }
    
            public static void SendMessage(int clientID,String message)
            {
                TcpClient client = clientList[clientID];
                NetworkStream nwStream = client.GetStream();
    
                byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes("Sending Text " + message);
                Console.WriteLine("Sending Image Message " + message);
                nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
            }

    Client:

        class Program
        {
            const int PORT_NO = 8081;
            const string SERVER_IP = "127.0.0.1";
            static void Main(string[] args)
            {
                Console.WriteLine("Enter id:");
                String ergebnis = Console.ReadLine();
    
                //---create a TCPClient object at the IP and port no.---
                TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
                NetworkStream nwStream = client.GetStream();
    
                byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(ergebnis);
    
                //---send the text---
                Console.WriteLine("Sending : " + ergebnis);
                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
    
                //---read back the text---
    
                byte[] bytesToRead = new byte[client.ReceiveBufferSize];
    
                while (true)
                {
                    if (nwStream.DataAvailable)
                    {
    
                        int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
                        Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
    
                        if (Encoding.ASCII.GetString(bytesToRead, 0, bytesRead).Contains("Image"))
                        {
                            Console.WriteLine();
                            Image img = Image.FromStream(nwStream);
                            img.Save($"newimage.png", ImageFormat.Png);
                        }
                    }
                }
    
            }

    Sending a textmessage multiple times is no problem at all but the sending of an Image only works once.

    Once i send the image textmessaging is not possible anymore either and the line

    NetworkStream nwStream = client.GetStream();

    is throwing a InvalidOperationException. I ran out of ideas how to fix this.

    Thanks in advance

    Jan

    Friday, June 22, 2018 7:55 PM

All replies

  • client.GetStream returns you THE network stream for the TcpClient.  When you call nwStream.Dispose, you are shutting down the communication.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Saturday, June 23, 2018 5:23 AM
  • You're right thanks i overlooked that one. The problem is the program won't step over

    Image img = Image.FromStream(nwStream);
    if i don't close or dispose of the stream. It will just do nothing and wait. Is there any way to tell it that the stream is done transmitting the image?

    Saturday, June 23, 2018 11:23 AM
  • Hi Hasok,

    You could try to use Thread. Open multiple threads when you send a textmessage multiple times in Thread pool, after finished, it would dispose the thread automatically. There is no need to call dispose() method by yourself.

    Here is the MSDN document for your reference.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/threading/how-to-use-a-thread-pool

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, June 28, 2018 2:13 AM
    Tuesday, June 26, 2018 8:27 AM
    Moderator