当前位置: 首页 > 编程语言 > C#

套接字无法正常工作,应该有所帮助!分享

时间:2023-04-10 17:41:40 C#

socketnotworkingasitshouldhelp!我制作了一个基于客户端和服务器套接字的gui应用程序,在该应用程序中捕获屏幕并通过套接字将其传输到所需的列表,但我在接收方或服务器中的客户端图像上获得黑色输出图像但无法显示我的客户端应用程序只显示黑色图片?代码是:服务器使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用系统数据;使用系统绘图;使用System.Linq;使用系统文本;使用System.Windows.Forms;使用System.Drawing.Imaging;使用System.Net.Sockets;使用System.IO;使用System.Net;namespaceLanMonitoring{publicpartialclassForm1:Form{privatestaticBitmapbmpScreenshot;布尔开始=假;私有静态图形gfxScreenshot;publicForm1(){InitializeComponent();button2.Enabled=false;}privatevoidbutton1_Click(objectsender,EventArgse){button1.Enabled=false;button2.Enabled=true;开始=真;填充图片();}publicvoidfillpic(){bmpScreenshot=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb);gfx截图t=Graphics.FromImage(bmpScreenshot);gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y,0,0,Screen.PrimaryScreen.Bounds.Size,CopyPixelOperation.SourceCopy);pictureBox1.Image=bmpScreenshot;sendbmp(bmpScreenshot);}privatevoidbutton2_Click(objectsender,EventArgse){button1.Enabled=true;button2.Enabled=false;开始=假;}publicvoidsendbmp(Bitmapbmp){Socketmm=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPEndPointremoteEP=newIPEndPoint(IPAddress.Parse(textBox1.Text),5002);尝试{mm.Connect(remoteEP);}catch(异常){sendbmp(bmpScreenshot);}图像温度=bmp;byte[]buf=imageToByteArray(temp);mm.Send(buf);mm.Close();}publicbyte[]imageToByteArray(System.Drawing.ImageimageIn){MemoryStreamms=newMemoryStream();imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);返回ms.ToArray();}publicImagebyteArrayToImage(byte[]byteArrayIn){MemoryStreamms=newMemoryStream(byteArrayIn);图像returnImage=Image.FromStream(ms);返回返回图像;}}}客户为:usingSystem;使用System.Collections.Generic;使用System.ComponentModel;使用系统数据;使用系统绘图;使用System.Linq;使用系统文本;使用System.Windows.Forms;使用System.Net.Sockets;使用System.IO;使用System.Net;namespaceLanReciver{publicpartialclassClient:Form{byte[]buf=newbyte[5000];publicClient(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){backgroundWorker1.RunWorkerAsync();}publicvoidcall(){Socketmm=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);mm.Bind(新IPEndPoint(0,5002));mm.Listen(100);套接字acc=mm.Accept();buf=newbyte[acc.SendBufferSize];intbyteread=acc.Receive(buf);byte[]rev=newbyte[byteread];for(inti=0;i