2011-11-27 83 views
0

我可以在StreamReader套接字C#上收到一個int嗎?我可以在StreamReader套接字C#上收到一個int嗎?

我開發了一個java數據應用程序客戶端通過套接字發送int到C#中的應用程序服務器,但我不知道我怎麼能收到一個int。因爲如果我把int mensagem = sr.ReadLine()不工作!

服務器應用程序的C#中的代碼:

//Some code not include. 

    public void Server() 
    { 

     Socket soc = listener.AcceptSocket(); 
     //Informa uma conecção 
     //MessageBox.Show("Conectado: " + soc.RemoteEndPoint); 

     try 
     { 
      Stream s = new NetworkStream(soc); 
      StreamReader sr = new StreamReader(s); 
      StreamWriter sw = new StreamWriter(s); 
      sw.AutoFlush = true; // enable automatic flushing 

      while (true) 
      { 
       string mensagem = sr.ReadLine(); //if i put int message not work why? 
       comando(mensagem); 
      } 
      //s.Close(); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show("Erro!" + e.Message); 
     } 
     //MessageBox.Show("Disconectado: " + soc.RemoteEndPoint); 
     //soc.Close(); 

    } //Fim Função Server 
+1

題外話,但你應該使用'與是一次性的('Stream','StreamReader','StreamWriter') –

回答

1

ReadLine返回一個字符串。您可以使用的TryParse讓你的整數:從你的問題

int fromClient; 

if (!int.TryParse(mensagem, out fromClient)) 
{ 
    // error parsing as integer 
} 

// fromClient is either the parsed value or 0 if TryParse was false 
+0

類型using'塊謝謝......這是工作。 – FredVaz

相關問題