0

我在 VS2010 中有一个使用 C# 的服务器客户端程序,其中客户端将文件发送到服务器,服务器用另一个文件响应客户端。我想要的是让我的服务器继续我正在使用的功能

IPAddress[] ipAddress = Dns.GetHostAddresses("MRD044");
for (int i = 0; i < ipAddress.Length; i++)
{
    if (ipAddress[i].AddressFamily == AddressFamily.InterNetwork)
    {
        ipEnd = new IPEndPoint(ipAddress[i], 5656);
        sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        sock.Bind(ipEnd);
     }
 }
 curMsg = "Starting...";
 Console.WriteLine(curMsg);
 sock.Listen(10);
 curMsg = "Running and waiting to receive file.";

现在我想运行执行服务器端接收功能的 getFile() 函数,仅在客户端将文件发送到服务器时运行。喜欢 :

if(clientSendingFile())
{
    getFile();
}
else
{}
4

1 回答 1

1

调用 后sock.Listen(10),调用 Accept 方法等待传入连接:

Socket clientsocket = sock.Accept();

那么您的 clientsocket 实现可能如下所示:

// pseudo-code - I might have a missed a C# thing or two...
void getFile()
{

  byte [] buffer = new byte[1000];


  while (true)
  {
      int count = -1;
      try
      {
          count = clientsock.Receive(buffer);
          // write count bytes into the file - however you are doing that
      }
      catch(Exception e)
      {
           // error
           count = -1;
           break;
      }
  }
于 2013-07-22T05:56:26.377 回答