Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: problem using Sockets

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post problem using Sockets

    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:
     
     
     
    import java.net.*;
    import java.io.*;
    public class Socket {
    public static void main(String args[]) throws IOException {
    // Register service on port 1254
    ServerSocket s = new ServerSocket(1254);
    java.net.Socket s1=s.accept(); // Wait and accept a connection
    // Get a communication stream associated with the socket
    OutputStream s1out = s1.getOutputStream();
    DataOutputStream dos = new DataOutputStream (s1out);
    // Send a string!
    dos.writeUTF("Hi there");
    // Close the connection, but not the server socket
    dos.close();
    s1out.close();
    s1.close();
     
     
     
     
     
     
    	}
     
    public InputStream getInputStream() {
    	// TODO Auto-generated method stub
    	return null;
    }
     
    }

    and for client is:
     
     
     
     
     
     
     
    import java.net.*;
    import java.io.*;
    public class Client {
     
    	public static void main(String[] args) {
    		// Open your connection to a server, at port 1254
    		Socket s1 = new Socket("localhost",1254);
    		// Get an input file handle from the socket and read the input
    		InputStream s1In = s1.getInputStream();
    		DataInputStream dis = new DataInputStream(s1In);
    		String st = new String (dis.readUTF());
    		System.out.println(st);
    		// When done, just close the connection and exit
    		dis.close();
    		s1In.close();
    		s1.close();
     
     
     
    	}
     
    }



    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(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)

    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


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: problem using Sockets

    Quote Originally Posted by Jedi khan View Post
    Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
    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.

  3. The Following User Says Thank You to jps For This Useful Post:

    derekxec (August 1st, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem using Sockets

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

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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

  6. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem using Sockets

    Quote Originally Posted by jps View Post
    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 ???

  7. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default 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.

  8. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem using Sockets

    I will but its not the solution Syed Sahab..

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: problem using Sockets

    Quote Originally Posted by Jedi khan View Post
    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

  10. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem using Sockets

    Quote Originally Posted by jps View Post
    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)

  11. #10
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default 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

  12. #11
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem using Sockets

    Quote Originally Posted by derekxec View Post
    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
    But when i ran the client part it returned an error pointint to:
     
    String st = new String (dis.readUTF());
    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)

    ServerSocket s = new ServerSocket(1254);

    Its driving me mad now as i am too slow now cz of this bug!!!

  13. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: problem using Sockets

    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.

  14. #13
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default 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

  15. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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

Similar Threads

  1. Working With Sockets
    By andyk in forum Java Networking
    Replies: 1
    Last Post: November 27th, 2012, 06:53 PM
  2. Sockets and TCP packets
    By Raadush in forum Java Networking
    Replies: 1
    Last Post: May 14th, 2012, 05:58 PM
  3. Sockets in JApplet
    By shrey.haria in forum Java Networking
    Replies: 4
    Last Post: September 5th, 2011, 09:45 AM
  4. [SOLVED] Problems with Sockets
    By Samthere in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2011, 06:02 AM
  5. Java sockets help
    By ma05k1 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 05:52 AM