question

YRK-33345 avatar image
0 Votes"
YRK-33345 asked DanielZhang-MSFT commented

how to send an image in tcp server

I'm making a chat and I want to make an option to send a picture, how to do that?

Server code:

 using SimpleTcp;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
    
 namespace Server
 {
     public partial class Form1 : Form
     {
         IList<string> Clients = new List<string>() {};
         int h = 0;
         public Form1()
         {
             InitializeComponent();
                
         }
    
         SimpleTcpServer server;
    
         private void Form1_Load(object sender, EventArgs e)
         {
             server = new SimpleTcpServer(txtIP.Text);
         }
    
            
         private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
         {
    
         }
    
         private void btnStart_Click(object sender, EventArgs e)
         {
             server.Start();
             server.Events.ClientConnected += Events_ClientConnected;
             server.Events.ClientDisconnected += Events_ClientDisconnected;
             server.Events.DataReceived += Events_DataReceived;
    
             btnStart.Enabled = false;
         }
    
         private void Events_DataReceived(object sender, DataReceivedEventArgs e)
         {
             if (h <= Clients.Count + 1)
             {
                 for (int i = 0; i < Clients.Count; i++)
                 {
                     server.Send(Clients[i], $"{e.IpPort}: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}");
                 }
                    
    
             }
             else
             {
    
                 h = 0;
             }
                
             //string l = server.GetClients().ToString();
             //server.Send(l, $"{e.IpPort}: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}");
         }
    
         private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
         {
                
         }
    
         private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
         {
             //lstClients.Items.Add(e.IpPort);
             Clients.Add(e.IpPort);
         }
    
         private void textBox1_TextChanged(object sender, EventArgs e)
         {
    
         }
     }
 }

Client code:

 using SimpleTcp;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
    
 namespace Socil
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
         SimpleTcpClient client;
    
         private void Form1_Load(object sender, EventArgs e)
         {
             client = new(txtIP.Text);
             client.Events.Connected += Events_ClientConnected;
             client.Events.DataReceived += Events_DataReceived;
             client.Events.Disconnected += Events_ClientDisconnected;
         }
    
         private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
         {
                
         }
    
         private void Events_DataReceived(object sender, DataReceivedEventArgs e)
         {
             this.Invoke((MethodInvoker)delegate
             {
                 richTextBox1.Text += Encoding.UTF8.GetString(e.Data);
             });
                
         }
    
         private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
         {
                
         }
    
         private void richTextBox1_TextChanged(object sender, EventArgs e)
         {
    
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             try
             {
                 client.Connect();
                 button1.Enabled = false;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "messege", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
    
         private void btnSebd_Click(object sender, EventArgs e)
         {
             client.Send(txtMessege.Text);
         }
     }
 }









dotnet-csharpwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @YRK-33345,
Based on your code, what is your SimpleTcpServer?
I installed the SimpleTCP nuget and find some errors in your code.
81785-326.png
So please provide more information or code to explain it.
And for sending an image in tcp server, you can try to convert the picture to a stream on the server.
Here some related threads you can refer to.
TCP Client/Server Image Transfer
Transfer image from client to server through socket tcp/ip
Sending and receiving an image over sockets with C#
Best Regards,
Daniel Zhang


0 Votes 0 ·
326.png (20.9 KiB)

Oh, I forgot to mention that I work with super simple TCP

0 Votes 0 ·

Hi @YRK-33345,
Please provide more code or information about your super simple TCP to reproduce it.
Best Regards,
Daniel Zhang

0 Votes 0 ·

0 Answers