我在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
{}
答案 0 :(得分: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;
}
}