Socket programming questions

I am trying Programs related to Socket Programming...
whenever i tries to make client and server as my own PC..it refuses connection...I think it is because of fire wall....can anyone help me how to allow some programs that can access my pc?๐Ÿ˜•๐Ÿ˜•

Replies

  • MaRo
    MaRo
    - How you set client-server connection?
    - What language?
    - Using third-party Sockets libraries or the default libraries of the language?
    - Provide information as much as you can.
  • Manish Goyal
    Manish Goyal
    I am using Java for this and I am using default libraries of the language

    import java.io.*;
    import java.net.*;
    class Client
    {
    public static void main(String args[])throws IOException
    {
    Socket con=null;
    PrintWriter out=null;
    BufferedReader in=null;
    try
    {
    con=new Socket("localhost",32);//Developed a socket connection by client
    out=new PrintWriter(con.getOutputStream(),true);//Write user input to socket
    in=new BufferedReader(new InputStreamReader(con.getInputStream()));//Retrieve input from socket
    }
    catch(UnknownHostException e)
    {
    System.err.println("Sorry host can't be found");
    System.exit(1);
    }
    catch(IOException ef)
    {
    System.err.println("Sorry Input/output fails");
    System.exit(1);
    }
    try
    {
    BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
    String inp;
    while((inp=std.readLine())!=null)
    {
    out.println("User input:"+inp);//It will send user input to PrintWriter
    System.out.println(in.readLine());
    }
    in.close();
    std.close();
    out.close();
    con.close();
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    }
    }
    
    For server side
    import java.net.*;
    import java.io.*;
    class Server
    {
    public static void main(String args[])throws IOException
    {
    ServerSocket ss=null;
    try
    {
    ss=new ServerSocket(4445);
    }
    catch(Exception e)
    {
    System.out.print("Sorry can't listen on port 4445");
    System.exit(1);
    }
    Socket cs=null;
    PrintWriter out=null;
    BufferedReader in=null;
    try
    {
    cs=ss.accept();
    out=new PrintWriter(cs.getOutputStream(),true);
    in=new BufferedReader(new InputStreamReader(cs.getInputStream()));
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    System.exit(1);
    }
    try
    {
    BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
    String input,serve;
    while((input=in.readLine())!="Bye")
    {
    System.out.println("Client says:"+input);
    serve=std.readLine();
    out.println(serve);
    }
    }
    catch(Exception e)
    {
    System.out.println("second:"+e.getMessage());
    }
    }
    }
    The Program is successfull compiled but when i try execute client it simply refuses connection and I got the exception
    connection refused connect
    but when i try to execute this program in some one else computer it executes successfully
  • sookie
    sookie
    99.9% I am sure, if your code is working on 1 machine and not in your own machine then definitely the problem is with IP and port no. Are the port nos. you are using in your program free in your machine. Please confirm this.

    Thanks and All the Best ! ๐Ÿ˜€
  • sookie
    sookie
    @goyal420 I tried your program in mymachine, in the starting I got error as following in "Client.java" program
    init:
    deps-jar:
    compile-single:
    run-single:
    Sorry Input/output fails
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    But now I replaced
    ss=new ServerSocket(4445);
    line in your Server.java with
    ss=new ServerSocket(32);
    , it worked like below

    At server window
    init:
    deps-jar:
    Compiling 1 source file to D:\Java Application\PRACTICE_JAVA\build\classes
    compile-single:
    run-single:
    hi
    Client says:User input:hi
    hello
    Client says:User input:yo !
    At client window
    init:
    deps-jar:
    compile-single:
    run-single:
    hi
    hi
    yo !
    hello
    Hope you got the point, if not please read once thoroughly about Socket Programming. I am sure, few articles are here on this forum itself. ๐Ÿ˜€
  • Manish Goyal
    Manish Goyal
    Thank you very much sookie for this..
    Now my program is running successfully
  • Manish Goyal
    Manish Goyal
    GUys now i want to extend this program between two remote computers

    i mean i want to establish connection between two via internet

    Can you please explain me what changes i should made to my program for this?

    I have also searched on google about this a lot but no satisfactory results
  • Manish Goyal
    Manish Goyal
    Come on Guys Please help me in this

    I am badly stuck to this

    How to establish communication between two computers via internet through socket programming?

    Please help me
  • MaRo
    MaRo
    Change localhost to the remote machine IP.
  • Manish Goyal
    Manish Goyal
    I have already changed it but still it is not working
    import java.net.*;
    import java.io.*;
    class Server
    {
    public static void main(String args[])throws IOException
    {
    ServerSocket ss=null;
    InetAddress is=InetAddress.getLocalHost();
    try
    {
    ss=new ServerSocket(8051,1,is);
    }
    catch(Exception e)
    {
    System.out.print(e.getMessage());
    System.exit(1);
    }
    Socket cs=null;
    PrintWriter out=null;
    BufferedReader in=null;
    try
    {
    cs=ss.accept();
    out=new PrintWriter(cs.getOutputStream(),true);
    in=new BufferedReader(new InputStreamReader(cs.getInputStream()));
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    System.exit(1);
    }
    try
    {
    BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
    String input,serve;
    while((input=in.readLine())!="Bye")
    {
    System.out.println("Client says:"+input);
    serve=std.readLine();
    out.println(serve);
    }
    }
    catch(Exception e)
    {
    System.out.println("second:"+e.getMessage());
    }
    }
    }
    import java.io.*;
    import java.net.*;
    class Client
    {
    public static void main(String args[])throws IOException
    {
    Socket con=null;
    PrintWriter out=null;
    BufferedReader in=null;
    //InetAddress is=null;
    try
    {
    //is=InetAddress.getLocalHost();
    con=new Socket("117.205.32.1",8051);//Developed a socket connection by client
    out=new PrintWriter(con.getOutputStream(),true);//Write user input to socket
    in=new BufferedReader(new InputStreamReader(con.getInputStream()));//Retrieve input from socket
    }
    catch(UnknownHostException e)
    {
    System.err.println("Sorry host can't be found");
    System.exit(1);
    }
    catch(IOException ef)
    {
    System.err.println("Sorry Input/output fails");
    System.exit(1);
    }
    try
    {
    BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
    String inp;
    while((inp=std.readLine())!=null)
    {
    out.println("User input:"+inp);//It will send user input to PrintWriter
    System.out.println(in.readLine());
    }
    in.close();
    std.close();
    out.close();
    con.close();
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    }
    }
    These are changes that i have made
  • MaRo
    MaRo
    Check firewall settings.
  • sookie
    sookie
    goyal420
    Come on Guys Please help me in this

    I am badly stuck to this

    How to establish communication between two computers via internet through socket programming?

    Please help me
    What problem or exceptions you are getting with your current code?
  • Manish Goyal
    Manish Goyal
    no exception ,no errors at all

    when i try to communicate with my friends through this ..
    It doesn't work and shows me message "Sorry input/output fails"

    I don't understand why?

    May be due to wrong port no

    But how can i find the port no suitable for this?
  • sookie
    sookie
    goyal420
    no exception ,no errors at all

    when i try to communicate with my friends through this ..
    It doesn't work and shows me message "Sorry input/output fails"

    I don't understand why?

    May be due to wrong port no

    But how can i find the port no suitable for this?
    Hi, just now I tried running your code in my machine and my colleague's machine. It worked perfectly fine. Just please make sure
    1. Both the machines are in network.
    2. Server Program is running before making Client program to run.
    3. The ip used in below statement of Client.java is of Server machine's ip.
    con=
    new Socket([IP of machine where Server.java is running],8051);//Developed a socket connection by clientโ€‹
    Otherwise your entire program is working fine. Problem may be because of network. Just check once if you are able to access your friend's machine through your machine or vice-versa.โ€‹

    Hope it helps ! Revert back if still problem persists. ๐Ÿ˜€โ€‹
  • Manish Goyal
    Manish Goyal
    HEY have you Tried it through LAN or internet?

    I had tried to make connection through internet .
  • sookie
    sookie
    goyal420
    HEY have you Tried it through LAN or internet?

    I had tried to make connection through internet .
    LAN & internet should not matter. The only thing is both the machines should be in network. Tonight I will try it in internet also. Then will let you know.

You are reading an archived discussion.

Related Posts

DAEMON Tools Lite Size:- 8.16 MB An advanced application for Microsoft Windows which provides one of the best optical media emulation in the industry. DAEMON Tools enables you to convert...
fan speed is controlled by dimmer ,..does it save power when it is rotating at lower speed or it consume same power as running in full speed?? i think when...
how transformer invert(out of phase) a signal .... which parameters tells that the output of a transformer is INphase or out of phase.....
just found this on youtube!! Very interesting. Enjoy!! ๐Ÿ˜€ [YOUTUBE][/YOUTUBE]
dear CE ans, I want to know how the freezing point of water and meting point of ice can ba same 0 degree Celsius.how can both happen at the same...