jolyTimePopCorn
jolyTimePopCorn

Reputation: 23

My application (sever/client) is blocked after using Readline

Im trying to make a tcp connection between server and client . server is programmed on c# and cliend on java... server is working fine... my problem is in this code:

try {
  InetAddress address = InetAddress.getByName("127.0.0.1");
  connection = new Socket(address, port);        
  BufferedReader inFromServer = new BufferedReader(
         new InputStreamReader(connection.getInputStream()));
  loginInfo = inFromServer.readLine();
  System.out.println("username/pass are  received");
  System.out.println(loginInfo);
  connection.close();
} catch (IOException f) {
  System.out.println("IOException: " + f);
} catch (Exception g) {
  System.out.println("Exception: " + g);
}

The application is blocked and I can't close it any more... until i finish debug from java . I guess the problem is in loginInfo because im not getting username/pass are received in output .. so any help?

this is the thread that send message from c# :

Thread listener_service = new Thread((ThreadStart)delegate
{
  listener.Start();
  while (true)
  {
    s = listener.AcceptSocket();
    Console.WriteLine("Connected to !" + s.RemoteEndPoint);
    ASCIIEncoding asen = new ASCIIEncoding();
    s.Send(asen.GetBytes("The string was recieved by the server. \n"));
    Console.WriteLine("\nSent Acknowledgement");
    continue;
  }
});

Upvotes: 1

Views: 284

Answers (1)

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

calling readLine() is blocking call, means your code execution will be blocked until and unless you receive any line from server communication.

use System.Environment.NewLine instead of \n to terminate your line in C# code.

Upvotes: 2

Related Questions