Server-client connection; what I'm missing?

Go To StackoverFlow.com

0

I got a working server in C# and a working client in Java (Android). It's totally working, but the connection itself is only one-wayed. Only the client can send data to the server. I tried to make the connection two-sided, but the code not working by an unknown reason.

Where I do it wrong?

C#- server

public void SendBack(string message, NetworkStream stream)
{
  if (stream.CanWrite)
  {
    byte[] candy = Encoding.ASCII.GetBytes(message + ";");

    stream.Write(candy, 0, candy.Length);
    stream.Flush();

    Console.WriteLine("[SENT] " + message);
  }
  else { Console.WriteLine("ERROR! CANNOT WRITE ON NETWORKSTREAM!"); }
}

Java- client

//Creating the stream in the constructor
bReader = new BufferedReader(new InputStreamReader(gSocket.getInputStream()));

new Thread (new Runnable(){
  public void run() {
    Log.d(TAG, "Started listerner...");
    ListenServer();
  }
}).start();

//The function itself
private void ListenServer(){
  try
  {
    String inputLine = "";
    while (true){
      inputLine = bReader.readLine();
      if (inputLine != null) Log.d(TAG, "Got: " + inputLine);
    }
  }
  catch (IOException e){ Log.d(TAG, "Could not listen to sever!"); }
}

Edit: forget to mention whats the actual problem... I start the server, behaves like usual, client can send data, which the server can interpret the message. Hoverwer, if the server sends something, the client do nothing. I mean, it does not execute the

Log.d(TAG, "Got: " + inputLine);

code.

2012-04-04 18:59
by LugaidVandroiy
Does the code actually reach the point where you create the new Thread and start it? If it does, then there is something REALLY strange happening here - Kiril 2012-04-04 19:10
It could be that the server is not sending the line-feed and/or carriage-return. BufferedReader.readLine()(link) expects it - srkavin 2012-04-04 19:15
The new thread (and the loop) runs great, but INSIDE the loop the code not runs as I tought.

I'm going to check what happens if I manually add a cr. Thank you - LugaidVandroiy 2012-04-04 19:21

You can construct a StreamWriter with the incoming stream, and use one of the WriteLine methods - srkavin 2012-04-04 19:21
@srkavin I'm now yelling a BIG THANK YOU to you. The carriage return was the problem. I only needed to add a "\n" to the sent string. Please, take all my respect, you saved my night - LugaidVandroiy 2012-04-04 19:26
@LugaidVandroiy I'm glad it helped. Shall I go ahead, post my comment as an answer? ;- - srkavin 2012-04-04 19:27


0

It could be that the server is not sending the line-feed and/or carriage-return. BufferedReader.readLine()(link) expects it. You can construct a StreamWriter with the incoming stream, and use one of the WriteLine methods.

2012-04-04 19:28
by srkavin
Ads