gpt4 book ai didi

c# - C#在套接字中发送屏幕截图

转载 作者:行者123 更新时间:2023-12-03 12:01:27 24 4
gpt4 key购买 nike

自一周以来,我一直在尝试从客户端PC制作屏幕截图并将其发送到服务器。

好吧,它现在工作了一点,但还不完全。我尝试了几种在Internet上找到的脚本,但是没有一个能按我的意愿工作,所以我更改了它们,但是由于我对套接字的了解很少,所以我无法正确使用它。你们能帮我吗?

屏幕截图功能可以正常工作,但是当我将流发送到服务器时,它仅接收图像的顶部(例如:原始屏幕截图为150kb,但是我接收的文件仅为40kb)请尝试其他几种文件发送脚本,但是我最多只能收到6个字节。

当我尝试使用While函数时,它什么也不发送给我或1kb,所以我知道我做错了什么,但是呢?

这是我的服务器代码:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(100);
Socket clientSock = sock.Accept();

byte[] clientData = new byte[1024 * 5000];
string receivedPath = "C:/";

int receivedBytesLen = clientSock.Receive(clientData);

int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

konjo = "Client: " + clientSock.RemoteEndPoint + "connected & File: " + fileName + " started received.";

BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

status = "File: " + fileName + "Received & Saved at path: " + receivedPath;

bWrite.Close();
clientSock.Close();
}
catch (Exception ex)
{
status = "Failed: " + ex.Message;
}
}

private void bgWorker_Complete(object sender, RunWorkerCompletedEventArgs e)
{
label1.Text = status;

}

这是我的客户代码:
        private void bgsender_work(object sender, DoWorkEventArgs e)
{
try
{
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
MemoryStream test = new MemoryStream();
bmpScreenShot.Save(test, ImageFormat.Jpeg);
bmpScreenShot.Save("C:\\Clienttest.jpeg", ImageFormat.Jpeg);

IPAddress[] ipAddress = Dns.GetHostAddresses(Dns.GetHostName());
IPEndPoint ipEnd = new IPEndPoint(ipAddress[2], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


string fileName = "screenshot.Jpeg";
string filePath = "Your File Path";
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] Data = test.ToArray();
byte[] clientData = new byte[4 + fileNameByte.Length + Data.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
Data.CopyTo(clientData, 4 + fileNameByte.Length);

clientSock.Connect("192.168.1.15", 5656);
clientSock.Send(clientData);

status = "File: " + fileName + " has been sent.";
clientSock.Close();
Console.ReadLine();
}
catch (Exception ex)
{
status = "It failed: " + ex.Message;
}

}

private void bgsender_complete(object sender, RunWorkerCompletedEventArgs e)
{
label1.Text = status;
}

最佳答案

我为客户端-服务器应用程序编写了一些示例代码,似乎可以满足您对另一个问题的回答。

看一下:https://stackoverflow.com/a/5575287/381588,它可以正常工作,并且很容易适应您的需求。

关于c# - C#在套接字中发送屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8403560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com