我正在使用我在网上找到的客户端/服务器代码进行通信:
客户端:
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;
}
}
此代码适用于将文件传输到服务器的1个客户端。我想知道如何更改此代码以使其与可以将文件发送到服务器的多个客户端进行通信?
此外,服务器代码'挂起'在Socket clientSock = sock.Accept();
并等待直到它收到一些东西。如果有一个'监听器'来监听新的文件然后循环遍历代码,而不是在那条线上无休止地等待,那就太好了。
我是客户/服务器编程的新手,并且非常关注其他建议。
答案 0 :(得分:0)
这里有解释为新连接创建循环:
http://msdn.microsoft.com/en-us/library/dz10xcwh.aspx
IPEndPoint 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();