using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.Threading;using System.Net;using System.IO;namespace sop_client{public partial class main : Form{private TcpClient client;private NetworkStream stream;private Thread receiveThread;private bool continueReceiving;private TcpListener listener;public main(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){if (client == null || !client.Connected){ConnectToServer();button1.Text = "停止连接";}else{Disconnect();button1.Text = "开始连接";}string localIP = GetLocalIPAddress();textBox1.Text = localIP;//SendIPAddress(localIP);}private string GetLocalIPAddress(){string localIP = "";var host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());foreach (var ip in host.AddressList){if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){localIP = ip.ToString();break;}}return localIP;}private void ConnectToServer(){try{string serverIP = textBox3.Text;int serverPort = 1024;client = new TcpClient();client.Connect(serverIP, serverPort);stream = client.GetStream();string localIP = GetLocalIPAddress();byte[] ipBytes = Encoding.ASCII.GetBytes(localIP);stream.Write(ipBytes, 0, ipBytes.Length);byte[] responseBytes = new byte[client.ReceiveBufferSize];int bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);string response = Encoding.ASCII.GetString(responseBytes, 0, bytesRead);string response1 = "Connected successfully";if (response == response1){label3.Text = response1;label3.ForeColor = Color.DarkGreen;}else{label3.Text = response;label3.ForeColor = Color.Red;}Thread receiveThread = new Thread(ReceiveImages);receiveThread.Start();}catch (Exception ex){MessageBox.Show(ex.ToString());}}private void Disconnect(){try{continueReceiving = false;stream.Close();client.Close();}catch (Exception ex){MessageBox.Show(ex.ToString());}}private void ReceiveImages(){continueReceiving = true;TcpListener listener = new TcpListener(IPAddress.Parse(GetLocalIPAddress()), 1025);listener.Start();while (continueReceiving){try{TcpClient imageClient = listener.AcceptTcpClient();NetworkStream imageStream = imageClient.GetStream();byte[] lengthBytes = new byte[4];imageStream.Read(lengthBytes, 0, 4);int length = BitConverter.ToInt32(lengthBytes, 0);byte[] imageData = new byte[length];int totalBytesRead = 0;int bytesRead = 0;while (totalBytesRead < length &&(bytesRead = imageStream.Read(imageData, totalBytesRead, length - totalBytesRead)) > 0){totalBytesRead += bytesRead;}if (totalBytesRead > 0){using (MemoryStream ms = new MemoryStream(imageData)){Image receivedImage = Image.FromStream(ms);pictureBox1.Image = receivedImage;}}catch (Exception ex){MessageBox.Show(ex.ToString());}}}private void ShowImage(string imagePath){pictureBox1.Invoke((MethodInvoker)delegate{pictureBox1.Image = Image.FromFile(imagePath);});}private void main_FormClosed(object sender, FormClosedEventArgs e){try{if (client != null && client.Connected){client.Close();}if (listener != null){listener.Stop();}if (receiveThread != null && receiveThread.IsAlive){receiveThread.Abort();}}catch (Exception ex){MessageBox.Show(ex.ToString());}}

C# TCP 客户端程序 - 接收服务器图片并显示

原文地址: https://www.cveoy.top/t/topic/qfRT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录