2013-07-22 21 views
0

我有一個服務器客戶端程序在VS2010中使用C#,其中客戶端正在向服務器發送文件,服務器用另一個文件響應客戶端。我要的是讓我的服務器去爲我使用功能如何識別客戶端是否使用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

你的代碼如何在服務器上運行?在IIS內?或作爲獨立的exe?無論如何,谷歌的「客戶端服務器C#」,你會被設置。 – Marcel

+0

你可能想澄清你的問題在那裏,但看起來你正在尋找關於編寫客戶端服務器程序從主機接收文件的主題的教程。繼承人之一: '[網站]:'http://socketprogramming.blogspot.co.uk/2010/01/send-file-from-server-to-client-using-c.html –

+0

不,不!沒有像MR那樣。約翰福克納休息我完成了它。感謝您的評論 – Rahul

回答

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; 
     } 
    } 
+0

謝謝.. @ selbie – Rahul

相關問題