我的服务器中存在黑洞(TcpClient、TcpListener)
我正在尝试构建一个服务器来接收客户端通过网络发送的文件。如果客户端决定一次发送一个文件,那么没有问题,我会按预期收到文件,但如果它尝试发送多个文件,我只会收到第一个文件。
这是服务器代码: 我为每个连接的客户端使用一个线程
public void ProcessClients()
{
while (IsListening)
{
ClientHandler clientHandler = new ClientHandler(listener.AcceptTcpClient());
Thread thread = new Thread(new ThreadStart(clientHandler.Process));
thread.Start();
}
}
以下代码是 ClientHandler 类的一部分
public void Process()
{
while (client.Connected)
{
using (MemoryStream memStream = new MemoryStream())
{
int read;
while ((read = client.GetStream().Read(buffer, 0, buffer.Length)) > 0)
{
memStream.Write(buffer, 0, read);
}
if (memStream.Length > 0)
{
Packet receivedPacket = (Packet)Tools.Deserialize(memStream.ToArray());
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Guid.NewGuid() + receivedPacket.Filename), receivedPacket.Content);
}
}
}
}
在第一次迭代中,我发送了第一个文件,但之后我没有收到任何内容。我尝试在每次迭代结束时使用 Thread.Sleep(1000) ,但没有任何运气。
另一方面,我有此代码(针对客户端)
.
.
client.Connect();
foreach (var oneFilename in fileList)
client.Upload(oneFilename);
client.Disconnect();
.
.
方法 Upload:
public void Upload(string filename)
{
FileInfo fileInfo = new FileInfo(filename);
Packet packet = new Packet()
{
Filename = fileInfo.Name,
Content = File.ReadAllBytes(filename)
};
byte[] serializedPacket = Tools.Serialize(packet);
netStream.Write(serializedPacket, 0, serializedPacket.Length);
netStream.Flush();
}
netStream (NetworkStream) 在 Connect 方法上打开,在 Disconnect 上关闭。
黑洞在哪里?我可以像我尝试的那样发送多个对象吗?
感谢您的宝贵时间。
I'm trying to build a server that will receive files sent by clients over a network. If the client decides to send one file at a time, there's no problem, I get the file as I expected, but if it tries to send more than one I only get the first one.
Here's the server code:
I'm using one Thread per connected client
public void ProcessClients()
{
while (IsListening)
{
ClientHandler clientHandler = new ClientHandler(listener.AcceptTcpClient());
Thread thread = new Thread(new ThreadStart(clientHandler.Process));
thread.Start();
}
}
The following code is part of ClientHandler class
public void Process()
{
while (client.Connected)
{
using (MemoryStream memStream = new MemoryStream())
{
int read;
while ((read = client.GetStream().Read(buffer, 0, buffer.Length)) > 0)
{
memStream.Write(buffer, 0, read);
}
if (memStream.Length > 0)
{
Packet receivedPacket = (Packet)Tools.Deserialize(memStream.ToArray());
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Guid.NewGuid() + receivedPacket.Filename), receivedPacket.Content);
}
}
}
}
On the first iteration I get the first file sent, but after it I don't get anything. I've tried using a Thread.Sleep(1000) at the end of every iteration without any luck.
On the other side I have this code (for clients)
.
.
client.Connect();
foreach (var oneFilename in fileList)
client.Upload(oneFilename);
client.Disconnect();
.
.
The method Upload:
public void Upload(string filename)
{
FileInfo fileInfo = new FileInfo(filename);
Packet packet = new Packet()
{
Filename = fileInfo.Name,
Content = File.ReadAllBytes(filename)
};
byte[] serializedPacket = Tools.Serialize(packet);
netStream.Write(serializedPacket, 0, serializedPacket.Length);
netStream.Flush();
}
netStream (NetworkStream) is opened on Connect method, and closed on Disconnect.
Where's the black hole? Can I send multiple objects as I'm trying to do?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测,如果客户端上传多个文件,您的循环将在服务器端将整个流作为单个文件读取。 “文件之间的分隔符”在哪里?服务器如何知道一个结束和另一个开始的位置?
I am guessing that if clients upload multiple files, your loop is reading the whole stream as a single file on the server side. Where's the "delimiter between files"? How does the server know where one ends and another begins?