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 8 of 8

Thread: Socket connection problem

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Socket connection problem

    Hi,

    I have been working on an application which basically involves client to server socket communication. I was first testing the program from two terminal prompts (server and client) where they have been successfully connecting via locahost, which was OK.

    Now I started moving on from localhost by using a virtual machine using Virtualbox where the physical host being the server and the virtual machine being the client. The VM is bridged to the Local Network and has it's own IP address (they both can ping each other).

    When I try to create a socket connection from the client (virtual machine VM) to the server (physical host), I get a ' ConnectionException - Connection timed out: connect ' error.

    The snippets of code below show's their socket creation.

    //Create a socket to listen on port 3002
    ServerSocket ss = new ServerSocket(3002);
     
    //create input streams, read data etc...

    //Create a socket connection to the physical host 'doug-pc'
    Socket sClient = new Socket("doug-PC", 3002);
     
    //create output streams, write data etc...

    Can anyone identify the problem?

    Thanks, much appreciated


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket connection problem

    //open the connection
    InetAddress hostAddr = InetAddress.getByName(IPADDRESS);
    int hostPort = PORT;
    Socket soc = new Socket(hostAddr,hostPort);
    //Send value to server
    PrintWriter theWriter = new PrintWriter(soc.getOutputStream(),true);
    theWriter.println("Hi from client ");
    //receive the data from the server
    String input = "NoValue";
    BufferedReader theReader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    while((input = theReader.readLine()) != null)
    System.out.println("SERVER REPLIED : " + input + "\n");
    //close the socket ,I/O stream
    theReader.close();
    theWriter.close();
    soc.close();

  3. #3
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Socket connection problem

    If I'm not mistaken, the error is in
    Socket sClient = new Socket("doug-PC", 3002);
    If "doug-PC" is not mapped to an IP in your hosts file, than it may be treated as an IP itself. You should try to either map "doug-PC" to that host's real IP in your hosts file, or write a real IP itself instead of "doug-PC".

    I'm not sure about my answer, but I think it worth trying.

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Socket connection problem

    Thanks for the replies.

    I invoked the getByName() method using the host's IP address (192.168.1.12) on InetAddress as instructed. But again I still get the same error?

    Yes it does seem very much so that the client socket is causing the problem. There would be no point mapping "doug-PC" to "192.168.1.12" because I can ping the host by it's hostname, and it returns the correct IP address to the client's terminal?

    Could there be some other obvious problem here? Can Virtual Machines be troublesome when applying java networking?

    Thanks again

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket connection problem

    Try another port.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Socket connection problem

    Yes I could try another port, but the two terminals on the host are connecting fine. So I wouldn't need to worry about the ports, I don't think.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket connection problem

    Your choice.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Socket connection problem

    Firstly I know coordinates is probably the wrong terminology? I'd assume i'd simply be passing the x and y variables, but coordinates describe it better I feel.

    Now I need to have a Server which can be accessed by 2 clients, it is a racing game and it requires each client to be able to maneuver a racecar simultaniously, each using a different control scheme but that's neither here nor there.

    I was hoping someone would be able to assist me when it came to sending the x and y positions of a racecar to the server and having the server send them onto the next player and vice versa to allow both racecars to move at the same time on each clients window. So far i've only done the simple server stuff, such as the knock knock server on the sun website, and a simple echo server which repeats a string I send to the server.

    When I tried to use int instead of string I recieved an error that the int I wanted to pass was dynamic (obviously changes with each movement) and cannot be passed as static (using readInt and writeInt).

    So any help on how to create the wanted movement on both client windows through the server would be appreciated.

    Thanks

Similar Threads

  1. one java progarm handle two socket connection
    By chuikingman in forum Java Theory & Questions
    Replies: 5
    Last Post: April 6th, 2012, 09:31 AM
  2. Replies: 12
    Last Post: September 3rd, 2011, 07:07 AM
  3. Creating an API to do socket connection J2ME
    By mikeotieno in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 13th, 2011, 02:45 AM
  4. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  5. problem with closing connection to client socket
    By sunitha in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2010, 04:28 AM