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

Thread: client- server program not working

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation client- server program not working

    import java.io.*;
    import java.net.*;
    public class Client1
    {
    public static void main(String args[])throws IOException
    {
    try
    {
    Socket socket=new Socket("59.177.97.229",5001);
    DataInputStream dis=new DataInputStream(socket.getInputStream());
    String a=dis.readUTF();
    System.out.println("msg from server"+a);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }
    import java.io.*;
    import java.net.*;
    public class Server1
    {
    public static void main(String args[])throws IOException
    {
    ServerSocket l=new ServerSocket(5001);
    try{
    Socket socket=l.accept();
    try
    {
    OutputStream sout=socket.getOutputStream();
    DataOutputStream dos=new DataOutputStream(sout);
    dos.writeUTF("\n hello this is server");
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }

    ERROR :
    connection time out in client


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: client- server program not working

    Please wrap your code with [code] tags so that it is easier to read, e.g.,
    [code]
    // your code here
    [/code]

    E.g.,
        // your code here

    Also, do wrap Client1 code separately from Server1 code so that it can be easily seen at a glance that they're separate classes.

    There's nothing functionally wrong with your code. When I ran them, the client received:
    msg from server
     hello this is server

    Are you certain that 59.177.97.229 is the correct IP address of the machine where Server1 is running? I used localhost since I had both Client1 and Server1 running on the same machine.

    Also, check that the server machine does not have a firewall that's preventing connections to port 5001.

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

    vivek rajput (March 18th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: client- server program not working

    thanks..was inserting the wrong ip..but still its giving wrong output.. and will wrap the programs in future.thanks

    --- Update ---

    thanks..was inserting the wrong ip..but still its giving wrong output : changing int values into string ..
    and will wrap the programs in future.thanks

Similar Threads

  1. Client / Server connection not working except localhost
    By Bingo90 in forum Java Networking
    Replies: 3
    Last Post: March 6th, 2014, 10:05 AM
  2. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  3. Help with JAVA Server-Client Program
    By GodLeoBouki in forum Java Theory & Questions
    Replies: 3
    Last Post: April 12th, 2011, 04:35 PM
  4. Client Server Program Not Working
    By ROHIT C. in forum Java Networking
    Replies: 3
    Last Post: September 3rd, 2010, 02:30 PM
  5. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM