通过TcpClient发送多个Textmessages和图像 [英] Sending multiple Textmessages and Images via TcpClient

查看:262
本文介绍了通过TcpClient发送多个Textmessages和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我正试图进入套接字并且发送多个图像。

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

服务器:

        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);
        }

客户:

    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);
                    }
                }
            }

        }

发送多次textmessage根本不是问题,但发送图像只能工作一次。

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

一旦我发送图像textmessaging也不再可能和行

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

NetworkStream nwStream = client.GetStream();

抛出InvalidOperationException。我没有想法如何解决这个问题。

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

提前致谢

Jan

推荐答案

client.GetStream返回TcpClient的网络流。 当你调用nwStream.Dispose时,你正在关闭通信。
client.GetStream returns you THE network stream for the TcpClient.  When you call nwStream.Dispose, you are shutting down the communication.


这篇关于通过TcpClient发送多个Textmessages和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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