0

Can i recevied a int on StreamReader Sockets C#?

I am developed a app client in java data send a int by sockets to app server in C# but i don't know how can i recevied a int. Because if i put a int mensagem = sr.ReadLine() not work !

The code of server app in 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
FredVaz
  • 423
  • 3
  • 8
  • 14
  • 1
    Off-topic from your question but you should be using `using` blocks with the types that are disposable (`Stream`, `StreamReader`, `StreamWriter`) – Austin Salonen Nov 28 '11 at 00:05

1 Answers1

1

ReadLine returns a string. You can use TryParse to get your integer:

int fromClient;

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

// fromClient is either the parsed value or 0 if TryParse was false
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139