使用tcpclient c#将客户端的桌面映像发送到服务器。 [英] Sending desktop images of the client to server using tcpclient c#.
本文介绍了使用tcpclient c#将客户端的桌面映像发送到服务器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我有一个项目需要将客户端的桌面映像发送到服务器。服务器必须处理所有传入连接,并且每个客户端都会出现一个新的窗体,其中包含一个图片框,而此图片框包含该客户端的图像。请帮帮我......提前致谢。
我尝试过的事情:
我已经尝试过搜索这个问题,但大多数解决方案都使用服务器和客户端之间的同步通信。
解决方案
我发布了客户端代码示例。它可能会有用。
流imageFileStream = File.OpenRead( YourFullFilePath跨度>);
byte [] byteArray = new byte [imageFileStream.Length];
imageFileStream.Read(byteArray, 0 ,( int )imageFileStream.Length);
TcpClient client = new TcpClient( YourServer , 8080 ); // 8080是运行服务器的端口。
NetworkStream network = client.GetStream( );
network.Write(byteArray, 0 ,byteArray.GetLength( 0 ));
network.Close();
嗨!希望你通过我的例子得到想法。在这里我给你服务器端示例。
服务器端代码可以是:
TcpListener ServerforTCP = new TcpListener( 8080 );
ServerforTCP.Start();
while ( true )
{
Socket socketToHandleRequest = ServerforTCP。 AcceptSocket(); // 接受客户请求。
NetworkStream network = new NetworkStream(socketToHandleRequest);
int readPoint = 0 ;
int chunkSize = 1024 ;
byte [] data = new byte < /跨度> [CHUNKSIZE];
lock (此) // 处理多个客户端调用问题
{
流fileForWrite = File.OpenWrite( FilePathWithFileNameWhereYouWantTosaveYourJPGFILE); // ex:D :\\File.jpg
while ( true )
{
readPoint = network.Read(data, 0 ,chunkSize);
fileForWrite.Write(data, 0 ,readPoint);
if (readPoint == 0 )
{
// 从客户端读取的文件的结尾。你可以打破循环。
break ;
}
}
fileForWrite.Close(); // 必须关闭。
}
socketToHandleRequest = 空跨度>; // 指定null以释放记忆。
System.Threading.Thread.Sleep( 1000 跨度>);
}
谢谢!。
>
嗨!这是我的第一个答案,所以请帮助我更好地回答我的答案。我只是试着简单地表达一下。
你试试过流吗?
总之我想问你有没有尝试将你的图像文件流读入byte [] 并将该文件发送到服务器?
相同,你可以在服务器端以相反的顺序执行此操作。
得到 byte []并保存带有图像文件扩展名的字节[] 。 />
你试过这个吗?如果没有,请告诉我,我会在这里发布演示。
谢谢。
Hi everyone, I have a project that requires sending desktop images of the client to the server. The server must handle all incoming connection and to every client, a new windows form will appear that has a picture box in it whereas this picture box contain the images of that client. Please help me... Thanks in advance.
What I have tried:
I already tried searching to this problem, but most solution uses a synchronous communication between server and client.
解决方案
I posted Client Side Code For an Example. may it would be useful.
Stream imageFileStream = File.OpenRead("YourFullFilePath"); byte[] byteArray=new byte[imageFileStream.Length]; imageFileStream.Read(byteArray, 0, (int)imageFileStream.Length); TcpClient client = new TcpClient("YourServer",8080); //8080 is the port where server is running. NetworkStream network = client.GetStream(); network.Write(byteArray,0,byteArray.GetLength(0)); network.Close();
Hi! hope you getting idea by my example. here I am giving you server side example.
Server side code can be like :
TcpListener ServerforTCP = new TcpListener(8080); ServerforTCP.Start(); while (true) { Socket socketToHandleRequest = ServerforTCP.AcceptSocket(); //accept client request. NetworkStream network = new NetworkStream(socketToHandleRequest); int readPoint = 0; int chunkSize = 1024; byte[] data=new byte[chunkSize]; lock (this) //to handle multiple client call issue { Stream fileForWrite = File.OpenWrite("FilePathWithFileNameWhereYouWantTosaveYourJPGFILE");// ex :D:\\File.jpg while (true) { readPoint = network.Read(data, 0, chunkSize); fileForWrite.Write(data,0,readPoint); if(readPoint==0) { //end of the file read from client. you can break the loop. break; } } fileForWrite.Close(); // Must Close. } socketToHandleRequest = null; // assign null to release memeory. System.Threading.Thread.Sleep(1000); }
Thanks!.
Hi! this is my first answer so please help to make my answer skill better. I just try to express things in brief.
Have you try with stream ?
in short I want to ask you that have you try to read stream of your image file into byte[] and send that file to server?
same you can do this in reverse order at Server side.
get byte[] and save that byte[] with image file extension.
have you tried this? if not then let me know I will post demo here.
Thanks.
这篇关于使用tcpclient c#将客户端的桌面映像发送到服务器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文