I have created a simple client server chat program using socket. I used to run Client program and Server program separately in two eclipse and chat is done through console only. The logic is as below:
System1Class (Server) | System2Class (Client)
1) SendThreadSys1 Class | 1) SendThreadSys2 Class
2) ReceivedThreadSys1 Class | 2) ReceivedThreadSys2 Class
Here I used 2 threads for each client and server classes. One thread is for sending and one thread is for receiving.
For sending I used Scanner.nextLine() to get the input from console. The program is working properly i.e. the 2 eclipses can communicate with each other through their console, but the problem comes while closing the socket.
I want to closed the socket by sending the string as "999" from SendThreadSys2 class. I tried to put Socket.closed()
in SendThreadSys2 class just after sending "999" but it gave
java.net.SocketException: Socket closed
Same problem occurs if I send "999" from SendThreadSys1 class. I think it is because all the four threads are connecting through a single socket. So how can I put Socket.closed() so as to make the socket closed successfully?
Thanks in advance.