CrazyEngineers
  • Socket programming questions

    Manish Goyal

    Manish Goyal

    @manish-r2Hoep
    Updated: Oct 25, 2024
    Views: 876
    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?😕😕
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • MaRo

    MemberJan 14, 2010

    - 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 14, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJan 14, 2010

    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 ! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJan 14, 2010

    @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. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 14, 2010

    Thank you very much sookie for this..
    Now my program is running successfully
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 6, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 21, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • MaRo

    MemberJun 21, 2010

    Change localhost to the remote machine IP.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 21, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • MaRo

    MemberJun 22, 2010

    Check firewall settings.
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 22, 2010

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 22, 2010

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 22, 2010

    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. 😀​
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 22, 2010

    HEY have you Tried it through LAN or internet?

    I had tried to make connection through internet .
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 22, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register