• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in Client ServerSockets

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Kamal Chopra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also tell me how to run both server and client program on the one and the same machine....Please!!!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the latest client and server code!
 
Kamal Chopra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SERVER PART:

CLIENT PART:

one thing more the methods such as InputStream getInputStream() & close() in server code are auto-generated methods ..
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic