We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,382 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Simple client server chat program using socket

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.

2
Contributors
4
Replies
3 Days
Discussion Span
6 Months Ago
Last Updated
7
Views
Question
Answered
1bung100
Newbie Poster
13 posts since Aug 2011
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

You have 4 connections throiugh one ServerSocket, but only one per Socket - the difference is important.
It's always a bit messy clsing a Socket connection when you have input/output streams going in both directions. Whichever end closes first will trigger a SocketException at the other end. ALl you can do AFAIK is to send 999 and close the socket. At the other end set a flag when you get the 999, then catch the exception and then ignore it if the flag is set.

JamesCherrill
... trying to help
Moderator
8,526 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Thanks James, I will try to handle the exception. But before doing that I want to know whether there is any way that I can assign value to standard inputstream.
e.g. somthing like

System.in = "999";
1bung100
Newbie Poster
13 posts since Aug 2011
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

I don't know any way to do that, someone else may know?

JamesCherrill
... trying to help
Moderator
8,526 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

We can assign value to Standard Inputstream using System.setIn(). But again a new problem comes up when I used this. Anyway for this thread I use exception handling, and for now chatting program is working fine. But I still want to find a solution whithout handling the exception i.e. closing the socket smoothly. So I will create a new Thread explaining my problem

1bung100
Newbie Poster
13 posts since Aug 2011
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 6 Months Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0875 seconds using 2.67MB