importjava.net.*;importjava.io.*;publicclass Client {publicstaticvoid main(String[] args){// Open your connection to a server, at port 1254Socket s1 =newSocket("localhost",1254);// Get an input file handle from the socket and read the inputInputStream s1In = s1.getInputStream();DataInputStream dis =newDataInputStream(s1In);String st =newString(dis.readUTF());System.out.println(st);// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();}}

Try using a different port number. Make sure to close the server thread before starting a new one, as it is possible an old instance of the project is holding the port open in the background.

July 30th, 2013, 11:13 PM

Jedi khan

Re: problem using Sockets

Also tell me please how to run both programs(Server,Client) on one and the same machine.

July 31st, 2013, 10:38 AM

jps

Re: problem using Sockets

Install both on the machine.
Launch them both. Probably the server first.
Exactly what are you stuck on? The ip? Use localhost.
Well I could keep guessing...
Please ask a more specific question and try to explain what you have tried and where you are stuck

August 1st, 2013, 02:15 AM

Jedi khan

Re: problem using Sockets

Quote:

Originally Posted by jps

Install both on the machine.
Launch them both. Probably the server first.
Exactly what are you stuck on? The ip? Use localhost.
Well I could keep guessing...
Please ask a more specific question and try to explain what you have tried and where you are stuck

Well if you don't mind plz tell me what u mean by install....i m not getting ...did u mean to run them separately or what i should take ???

August 1st, 2013, 03:17 AM

syedbhai

Re: problem using Sockets

Can you also change your server code class name to Server. Don't take Socket because we alread have Socket class in jdk.

Syed.

August 1st, 2013, 04:26 AM

Jedi khan

Re: problem using Sockets

I will but its not the solution Syed Sahab..

August 1st, 2013, 11:26 AM

jps

Re: problem using Sockets

Quote:

Originally Posted by Jedi khan

Well if you don't mind plz tell me what u mean by install....i m not getting ...did u mean to run them separately or what i should take ???

Here "Install" simply means make the jar(s) available to the machine you wish to execute them on.
I'm not going to give a step by step process that covers every possible scenario. Explain what you are doing and what problem you have, specifically. If there is an error message, copy paste the full text to the forum

August 1st, 2013, 12:47 PM

Jedi khan

Re: problem using Sockets

Quote:

Originally Posted by jps

Here "Install" simply means make the jar(s) available to the machine you wish to execute them on.
I'm not going to give a step by step process that covers every possible scenario. Explain what you are doing and what problem you have, specifically. If there is an error message, copy paste the full text to the forum

I am intending to run both programs on the same machine
The error i am encountering on every run of the client program 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(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:9)

August 1st, 2013, 01:36 PM

derekxec

Re: problem using Sockets

works fine for me...you also might want to do s.close(); in the server to close the server socket too and in the client you might want to use a try catch or throws or something

you sure you dont have something already using port 1254? if you are on windows try running a command prompt and doing netstat -a
see if your port is there being used

edit: now i ran into the problem you are having...as jps said its because you still have an instance of it running using that port

August 2nd, 2013, 02:57 AM

Jedi khan

Re: problem using Sockets

Quote:

Originally Posted by derekxec

works fine for me...you also might want to do s.close(); in the server to close the server socket too and in the client you might want to use a try catch or throws or something

you sure you dont have something already using port 1254? if you are on windows try running a command prompt and doing netstat -a
see if your port is there being used

edit: now i ran into the problem you are having...as jps said its because you still have an instance of it running using that port

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)

What happened when you tried a different port number as suggested in post #2?
I did not look at or run the code, but looking at post #10 it would seem derekxec may have, and suggested the same thing.
No one is going to hand over working code, if you need more help, try what was suggested and report the results if you still need help.

August 2nd, 2013, 05:37 PM

derekxec

Re: problem using Sockets

yeah i ran the code and only could get that bindexception if i tried running it again with 1 instance of it already running...because of course the first instance was listening on the port already

August 2nd, 2013, 05:44 PM

jps

Re: problem using Sockets

That would verify what you were told in post #2 and 10.
Please mark the thread as solved if you have no further questions