Forums Register Login

tcp chat application

+Pie Number of slices to send: Send
Hey guys im new to coderanch.. please help me with something.
I have written this code for tcp server and client but im having some problems.help me out please.
And also tellme how to use this to chat on diffrent computers (like what to enter at "inetaddress.getlocalhost").
thanks

*************************************Code of servert side


***********CONSOLE MSG
***after runnning server side code

java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:376)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at tcp.ChatServer.<init>(ChatServer.java:22)
at tcp.ChatServer.main(ChatServer.java:63)
java.lang.NullPointerException
at tcp.ChatServer.talk(ChatServer.java:51)
at tcp.ChatServer.main(ChatServer.java:64)


*************************************Code of client side


}
***on running client side

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at tcp.ChatClient.<init>(ChatClient.java:17)
at tcp.ChatClient.main(ChatClient.java:51)
1
+Pie Number of slices to send: Send
 

Rohan Tadphale wrote:
***********CONSOLE MSG
***after runnning server side code

java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:376)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at tcp.ChatServer.<init>(ChatServer.java:22)
at tcp.ChatServer.main(ChatServer.java:63)
java.lang.NullPointerException
at tcp.ChatServer.talk(ChatServer.java:51)
at tcp.ChatServer.main(ChatServer.java:64)



An address already in use means that the server socket could not be created because the port is already in use...

From your code, it looks like you are trying to use port 2000... and some other program is already using that port. The answer is simple, find a free port to use. Or if that is the port assigned to your program, find out who is using it, and get them to stop their program.

Henry
+Pie Number of slices to send: Send
THANK YOU ALOT! this helped. but even after changing the port number im getting the same error on running the program 2nd time. i guess im not closing the connection properly. can anyone help me with that as well?
Thank you.
+Pie Number of slices to send: Send
What exactly do you mean by "running the program 2nd time"? Tell us the precise sequence of starting server and client(s), and on which machines you do each (assuming they're not the same). There should only ever be a single instance of the server running on a machine, otherwise this error is unavoidable.
+Pie Number of slices to send: Send
I am using eclipse. whenever i run the program on eclipse, it is fine. 'server created' and messages are sent and received. but when i close eclipse and open it again and run the program, it gives above error.
Now i tried changing the port number and it works. but then i have to change the port number everytime i run the program.
+Pie Number of slices to send: Send
That suggests to me that Eclipse starts the server in some way so that it does not get stopped when Eclipse shuts down. Try running the server outside of Eclipse; that way, you will always know whether or not it's running, and can start and stop it independently of Eclipse.
+Pie Number of slices to send: Send
thank you again.I tried it from cmd. still no luck. the server is still running. gives above errors.
1
+Pie Number of slices to send: Send
Then there is still some other process running that occupies that port. Try killing all Java processes.
+Pie Number of slices to send: Send
yup, resetting connection worked. but still couldn't reset connection via code.
Thank you.
+Pie Number of slices to send: Send
What do you mean by "reset connection via code"? The connection is automatically terminated if the JVM quits.
+Pie Number of slices to send: Send
like connection.close()
+Pie Number of slices to send: Send
Are you talking about the server? If you shut down the server, all connections are closed anyway.
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1756 times.
Similar Threads
Server and Multiclient application
new Socket(host,port) does not throw ConnectException: Connection refused ?
Question as follows
Help with Chat software
Server and Multiclient application
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:35:26.