aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes tcp chat application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "tcp chat application" Watch "tcp chat application" New topic
Author

tcp chat application

Rohan Tadphale
Greenhorn

Joined: Dec 28, 2013
Posts: 8

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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rohan Tadphale
Greenhorn

Joined: Dec 28, 2013
Posts: 8

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
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
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

Joined: Dec 28, 2013
Posts: 8

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
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
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

Joined: Dec 28, 2013
Posts: 8

thank you again.I tried it from cmd. still no luck. the server is still running. gives above errors.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
Then there is still some other process running that occupies that port. Try killing all Java processes.
Rohan Tadphale
Greenhorn

Joined: Dec 28, 2013
Posts: 8

yup, resetting connection worked. but still couldn't reset connection via code.
Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
What do you mean by "reset connection via code"? The connection is automatically terminated if the JVM quits.
Rohan Tadphale
Greenhorn

Joined: Dec 28, 2013
Posts: 8

like connection.close()
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
Are you talking about the server? If you shut down the server, all connections are closed anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tcp chat application