wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes problem in Client ServerSockets 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 "problem in Client ServerSockets" Watch "problem in Client ServerSockets" New topic
Author

problem in Client ServerSockets

Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Hi coders!
Im new to Network programming and m currently exploring java.net ...... i am building a simple client server comm.. for that my code for server is:


and for client is:




the error returned is:



Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at Socket.main(Socket.java:9)

i need to know that y this so...moreover how should i manage to run both programs on same machine to get client-server comm....
Any help will be appreciated.
thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Jeedi Jed wrote:
i need to know that y this so...moreover how should i manage to run both programs on same machine to get client-server comm....


First, the issue is the server, and not the client. You can tell simply by looking at the stacktrace provided... as for what is wrong...

Question. Does this happen every time? Or does it work sometimes? If it happens every time, it is because there is another service using that port. Pick another port.

Otherwise, it is because you are running your service too quick after the previous run. It takes time for the OS to timeout sockets that are used. You can fix this by either setting the socket reuse option, or just have your program wait a minute or so, and try again. Personally, I prefer the later, as socket reuse have somewhat different meanings between unix and windows.

Henry


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

Joined: Jul 30, 2013
Posts: 19
Also tell me how to run both server and client program on the one and the same machine....Please!!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, the way you've written the client, you have to run both programs on the same machine. That's what "localhost" means. So just run the server in one shell and then open another shell to run the client.
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
I have just changed the port to 9999 and the rest of the code same....It was not compiling succesfully nor now....Also it is returning an error for the s1.close() and dis.readUTF() and it states an errors now when just running the client code:
Exception in thread "main" java.lang.NullPointerException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at Client.main(Client.java:11)
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Sorry to say THE count of VIEWS is increasing minutes to minutes but no suggestion/guidance as i am stuck and unable to proceed on....Please do guide me
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Jeedi Jed wrote:Sorry to say THE count of VIEWS is increasing minutes to minutes but no suggestion/guidance


Maybe nobody is responding because, like me, when they test your code it works with no exception thrown.

P.S. It is not the cause of your problem but you just need

Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Richard Tookey wrote:
Jeedi Jed wrote:Sorry to say THE count of VIEWS is increasing minutes to minutes but no suggestion/guidance


Maybe nobody is responding because, like me, when they test your code it works with no exception thrown.

P.S. It is not the cause of your problem but you just need


I dont know how to make a connection b/w these two programs on the same machine....also i want to remove this exception it comes again and again
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Jeedi Jed wrote:
Richard Tookey wrote:
I dont know how to make a connection b/w these two programs on the same machine....also i want to remove this exception it comes again and again


Start the server running in one command window. Run the client in a second command window. Worry about the exception when you have the client-server pair basically working.
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Richard Tookey wrote:
Jeedi Jed wrote:
Richard Tookey wrote:
I dont know how to make a connection b/w these two programs on the same machine....also i want to remove this exception it comes again and again


Start the server running in one command window. Run the client in a second command window. Worry about the exception when you have the client-server pair basically working.

But when i ran the client part it returned an error pointint to:

also returning NullPointerException
For running Server part it returns:
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unkno wn Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at Socket.main(Socket.java:13)



Its driving me mad now as i am too slow now because of this bug!!!
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Jeedi Jed wrote:
For running Server part it returns:
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind




As the exception indicates and has already been said by others, port 1254 is already being used by another process! Use a different port! Choose a port above 1023 since 0-1023 are reserved for 'well known' services. Until you stop getting that server exception there is no point in running your client.

Note - the NullPointerException from your client adds additional evidence that port 1254 is already in use since it means your client is connecting with it but not able to read the string.
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Richard Tookey wrote:
Jeedi Jed wrote:
For running Server part it returns:
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind




As the exception indicates and has already been said by others, port 1254 is already being used by another process! Use a different port! Choose a port above 1023 since 0-1023 are reserved for 'well known' services. Until you stop getting that server exception there is no point in running your client.


Note - the NullPointerException from your client adds additional evidence that port 1254 is already in use since it means your client is connecting with it but not able to read the string.


Well,,, I am running both as different programs from the same project name and first i ran server it didnt return any error but now when i ran client it returned
Exception in thread "main" java.lang.NullPointerException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at Client.main(Client.java:12)

Client.java.12 points to String st = new String (dis.readUTF());
which means something like unable to read the string is going on......???
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Post the latest client and server code!
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19

SERVER PART:

CLIENT PART:

one thing more the methods such as InputStream getInputStream() & close() in server code are auto-generated methods ..
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Your code works for me but I have taken it as a starting point and removed the redundant parts, changed the server class name, tried to make sure it binds to the correct interface ( localhost ), picks up the correct library classes and made the server loop forever accepting connections.. The exception handling is at best rudimentary but it works for me on both Linux and Windows 7 .





If this does not work on your system then I have to assume you have a network configuration problem.
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Richard Tookey wrote:Your code works for me but I have taken it as a starting point and removed the redundant parts, changed the server class name, tried to make sure it binds to the correct interface ( localhost ), picks up the correct library classes and made the server loop forever accepting connections.. The exception handling is at best rudimentary but it works for me on both Linux and Windows 7 .





If this does not work on your system then I have to assume you have a network configuration problem.


It worked for me now... Thanks !! Tookey
Kamal Chopra
Greenhorn

Joined: Jul 30, 2013
Posts: 19
Bear Bibeault wrote:"Jeedi Jed", please check your private messages for an important administrative matter. Thanks.


I did responded..Thanks for intimation here..A novice user of this forum .Hope will soon be used to comply with the site regulations.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in Client ServerSockets