gpt4 book ai didi

c# - 通过套接字发送数据不完整

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

对于我的项目,我试图在客户端中截取屏幕截图并将其通过套接字发送到服务器。我使用了一个循环来截取屏幕截图,直到手动停止为止。但是,尽管截取屏幕截图很好,但是在发送一些图像后,它仅占主图像的一半,而某些图像已满。
客户:
我正在与此截图

bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bmpScreenshot.Save(path + i + ".png");
ClientService.SendFile(path + i + ".png");
i++;

和SendFile方法是
public static void SendFile(string fileName)
{
try
{
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

string filePath = "";

fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -1)
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}

byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
if (fileNameByte.Length > 600 * 1024)
{
showMsg = "File size is more than 600kb, please try with small file.";
return;
}

byte[] fileData = File.ReadAllBytes(filePath + fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

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

clientSock.Connect(ipEnd);
clientSock.Send(clientData);
clientSock.Close();

}
catch (Exception ex)
{
if (ex.Message == "No connection could be made because the target machine actively refused it")
showMsg = "File Sending fail. Because server is not running.";
else
showMsg = "File Sending fail." + ex.Message;
}
}

服务器
IPEndPoint ipEnd;
Socket sock;
public ServerService()
{
ipEnd = new IPEndPoint(IPAddress.Any, 5656);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
}

public static string receivedPath;
internal void StartServer()
{
try
{
sock.Listen(100);

Socket clientSock = sock.Accept();

byte[] clientData = new byte[1024 * 5000];

int receivedBytesLen = clientSock.Receive(clientData);

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

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

bWrite.Close();
clientSock.Close();
}
catch (Exception ex)
{
ShowMsg = "Something is wrong." + ex.Message;
}
}

我怎么解决这个问题?

最佳答案

Receive仅保证用于(其中之一):

  • 返回至少1个字节
  • 表示EOF(非正字节)
  • 指示错误

  • 仅仅调用 Receive是不够的。您需要循环调用它,直到:
  • 您已经阅读了所有想要的数据
  • 您将获得一个EOF
  • 您收到错误

  • 对于您的情况,我建议您要:
  • 精确读取4个字节(可能需要循环)以获取名称长度n
  • 然后精确地读取n字节以读取名称
  • 然后读取,直到获得EOF(Receive的非肯定结果)

  • 另外:您可能想使用基于 Stream的API进行文件访问(读取和写入),而不是 File.ReadAllBytesBinaryWriter

    精确读取 n字节是一种类似情况:
    public void ReceiveExactly(Socket socket, byte[] buffer, int offset, int count)
    {
    int read;
    while(count > 0 && (read = socket.Receive(buffer, offset, count,
    SocketFlags.None)) > 0)
    {
    offset += read;
    count -= read;
    }
    if(count != 0) throw new EndOfStreamException();
    }

    关于c# - 通过套接字发送数据不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18656604/

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