从多个客户端向服务器发送视频文件
本文关键字:视频 文件 服务器 客户端 | 更新日期: 2023-09-27 18:16:56
我正在使用我在网上找到的客户端/服务器代码进行通信:
客户: public void Send(string name, string path)
{
try
{
IPAddress[] ipAddress = Dns.GetHostAddresses("address");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
string fileName = "somefile";
string filePath = path;
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] fileData = File.ReadAllBytes(System.IO.Path.Combine(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);
MessageBox.Show("file has been send: " + fileName);
clientSock.Close();
}
catch (Exception ex)
{
Console.WriteLine("File Sending fail." + ex.Message);
}
}
服务器:public void Receive()
{
try
{
lblInfo.Content = "That program can transfer small file. I've test up to 850kb file";
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();
// 1024 * 25.000 = 25mb max that can be received at once with this program.
byte[] clientData = new byte[1024 * 25000];
string receivedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "''";
int receivedBytesLen = clientSock.Receive(clientData);
int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
lblInfo.Content = "Client: connected & File started received.";
BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
lblInfo.Content = "File: received & saved at path: " + receivedPath;
bWrite.Close();
clientSock.Close();
}
catch (Exception ex)
{
lblInfo.Content = "File Receiving fail." + ex.Message;
}
}
对于将文件传输到服务器的一个客户机来说,这段代码工作得很好。我想知道我如何改变这段代码,使其与多个客户端通信,可以发送文件到服务器?
同样,服务器代码'挂起'在Socket clientSock = sock.Accept();
并等待,直到它接收到一些东西。如果有一个"监听器"来监听新传入的文件,然后循环遍历代码,而不是在那一行无休止地等待,那就太好了。
我是客户端/服务器编程的新手,希望大家多多指教。
下面是为新连接创建循环的说明:
http://msdn.microsoft.com/en-us/library/dz10xcwh.aspxIPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
listener.Bind(ipEnd);
listener.Listen(100);
Console.WriteLine("Waiting for a connection...");
Socket handler = listener.Accept();
while (true)
{
// 1024 * 25.000 = 25mb max that can be received at once with this program.
byte[] clientData = new byte[1024 * 25000];
string receivedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "''";
int bytesRec = handler.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, bytesRec - 4 - fileNameLen);
bWrite.Close();
}
//dont forget to close handler at server shutdown
//handler.Shutdown(SocketShutdown.Both);
//handler.Close();