0

I'm trying to implement a TCP send and receive program between a client and a server. They exchange info. continuously by using a loop. The client send a message first. Without the loop the program work but with the loop, it doesn't. Below are the programs:

public static void main(String[] args)  {
    // SERVER

    try
    {
        ServerSocket s = new ServerSocket(11);
        Socket s1 = null;
        int i=0;
        while (true)
        {
            s1 = s.accept();
            BufferedReader b = new BufferedReader(new InputStreamReader(s1.getInputStream()));
            PrintWriter p = new PrintWriter(new OutputStreamWriter(s1.getOutputStream()));
            String m;
            while ((m=b.readLine())!=null)
            {
                i++;
                System.out.print(m);
                p.print("reply"+i);
            }
        }

    }catch(IOException e){}

}

public static void main(String[] args) throws IOException {
    // CLIENT

    Socket s = new Socket("192.168.100.3",11);
    int i = 0;

    PrintWriter p = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

    p.print("msg"+i);
    BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));

    while (true){
        PrintWriter p1 = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

        BufferedReader b1 = new BufferedReader(new InputStreamReader(s.getInputStream()));

        String m;
        while((m=b1.readLine())!=null)
        {               
            System.out.print(m);
            p1.print("msg"+i);
        }
        i++;
        p1.flush();
    }
}
3
  • 1
    Have you tried to flush after p.print("reply"+i); ?
    – andrucz
    Feb 25, 2016 at 19:50
  • nope, i'll try it and let u know
    – ali
    Feb 27, 2016 at 19:47
  • i think the problem is with the socket being closed.. the socket is closed after each iteration in the while(true) loop, thats why the msg are not sent and received..if I close the socket after the loop, eclipse give me an error
    – ali
    Feb 27, 2016 at 20:11

3 Answers 3

0

When you use readLine() it reads up to the end of line.

However, print(x) doesn't write a newline.

If you write and flush a println(x) there is be a new line at the end of your text to read.

1
  • the problem is that flow of control just doesnt go in the loop : while(br.readline!=null) of the server.. maybe the problem is with the client
    – ali
    Feb 27, 2016 at 19:51
0

Does it make sense to loop and accept on the same socket? as in:

Socket s1 = null;
while (true) {
  s1 = s.accept();

Also, I don't think it would work on port 11 as you have in your code there. Try port numbers above 1024, those are not privileged.

1
  • yes, this socket can be a different socket depending on the different clients that want to connect to the server..
    – ali
    Feb 27, 2016 at 19:53
0

This code will just deadlock. Both ends are reading. Somebody needs to send something first, ensuring that it is line-terminated so that the peer's readLine() call will return.

1
  • yes, the client sends a message first when the connection is accepted by the server using s.accept() and I think the message (msg+i) is line terminated...
    – ali
    Feb 27, 2016 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.