The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Create a socket connection via cPanel

Discussion in 'General Discussion' started by mankmanky1, Dec 18, 2009.

  1. mankmanky1

    mankmanky1 Registered

    Joined:
    Dec 18, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Hi I am having a problem. I need to create a socket connection with my home computer acting as a server and my web server using cPanel that acts as a client.
    I try to connect to the server on my home computer in php using the fsockopen command on port 5000 as my home server is waiting for a TCP connection on port 5000.
    However, I am unable to create a connection as the connection always times out...
    I have tried port forwarding on my firewall and my router but to no avail. I am wondering whether the web server or cPanel actuually blocks the connection request to the home computer server. Is that possible?

    I also type in the IP address of my home pc that I get from whatismyIP.com and use that to create the socket connection from my web server. Is that correct?

    If there are any ports that may be blocked by cPanel or if anyone knows of a way to create this connection, please let me know as i am lost for ideas...

    Thanks very much

    The code is below:

    PHP code on web server: (This code times out when i run it)
    <?php
    function create_connection()
    {

    $host = "IPaddress";
    $port = 5000;
    $timeout =30;

    $sk = @fsockopen($host, $port, &$errnum, &$errstr, $timeout);
    if(!is_resource($sk))
    {
    exit("connection fail: ".$errnum." ".$errstr);
    }
    else
    {
    fputs($sk, "hello world");
    $dati = "";
    while(!feof($sk))
    {
    $dati.=fgets($sk, 1024);
    }


    }

    }

    ?>

    The java code waiting for a connection on my home computer:

    class TCPServer
    {
    public static void main(String argv[]) throws Exception
    {
    String fromclient;
    String toclient;

    ServerSocket Server = new ServerSocket (5000);

    System.out.println ("TCPServer Waiting for client on port 5000");

    while(true)
    {
    //When the client connects to the server a socket is created for the client and
    //the rest of the code is run
    Socket connected = Server.accept();
    System.out.println( " THE CLIENT"+" "+
    connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");

    BufferedReader inFromUser =
    new BufferedReader(new InputStreamReader(System.in));

    BufferedReader inFromClient =
    new BufferedReader(new InputStreamReader (connected.getInputStream()));

    PrintWriter outToClient =
    new PrintWriter(
    connected.getOutputStream(),true);

    while ( true )
    {

    System.out.println("SEND(Type Q or q to Quit):");
    toclient = inFromUser.readLine();

    if ( toclient.equals ("q") || toclient.equals("Q") )
    {
    outToClient.println(toclient);
    connected.close();
    break;
    }
    else
    {
    outToClient.println(toclient);
    }

    fromclient = inFromClient.readLine();

    if ( fromclient.equals("q") || fromclient.equals("Q") )
    {
    connected.close();
    break;
    }

    else
    {
    System.out.println( "RECIEVED:" + fromclient );
    }

    }

    }
    }
    }
     
Loading...

Share This Page