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.
I'm going to check what happens if I manually add a cr. Thank you - LugaidVandroiy 2012-04-04 19:21
StreamWriter
with the incoming stream
, and use one of the WriteLine
methods - srkavin 2012-04-04 19:21
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.
new Thread
and start it? If it does, then there is something REALLY strange happening here - Kiril 2012-04-04 19:10