我尝试了一周后从客户端pc制作屏幕截图并将其发送到服务器。
现在它有点工作,但不完全。我尝试了几个在互联网上找到的脚本,但没有一个像我想要的那样工作,所以我改变了它们,但是由于我对套接字的了解很少,我无法做到正确。你能帮助我吗?
屏幕截图功能正常但是当我将流发送到我的服务器时它只接收图像的顶部(例如:原始截图是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;
}
答案 0 :(得分:1)
我为客户端 - 服务器应用程序编写了一些示例代码,它似乎可以满足您的需求,作为另一个问题的答案。
请查看:https://stackoverflow.com/a/5575287/381588因为它有效,可能很容易适应您的要求。
答案 1 :(得分:0)
屏幕截图功能正常但是当我将流发送到我的 服务器它只接收图像的顶部(例如:原始 截图是150kb但我收到的文件只有40kb)试试 其他几个文件发送脚本,但最多我只收到6 字节..
当我尝试使用While功能时,它发送给我什么或1kb所以我 知道我做错了但是什么?
听起来像你循环并在前6个字节后发送接下来的6个字节。您的代码需要更复杂。我会重构它并使其更容易理解。
我要做的一件事就是在图像之前将文件大小发送到客户端,以便知道要读取多少字节。
听起来好像你没有读取足够的字节,也可能是你没有发送足够的字节,这取决于你要弄明白。