File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tcp chat application

 
Rohan Tadphale
Greenhorn
Posts: 8
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rohan Tadphale
Greenhorn
Posts: 8
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rohan Tadphale
Greenhorn
Posts: 8
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rohan Tadphale
Greenhorn
Posts: 8
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you again.I tried it from cmd. still no luck. the server is still running. gives above errors.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then there is still some other process running that occupies that port. Try killing all Java processes.
 
Rohan Tadphale
Greenhorn
Posts: 8
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup, resetting connection worked. but still couldn't reset connection via code.
Thank you.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "reset connection via code"? The connection is automatically terminated if the JVM quits.
 
Rohan Tadphale
Greenhorn
Posts: 8
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like connection.close()
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about the server? If you shut down the server, all connections are closed anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic