linux TCP socket program for java client to c server - Unix

This is a discussion on linux TCP socket program for java client to c server - Unix ; I want to make a java TCP socket client to communicate with a TCP server socket on linux. Are there some sample C unix server and java client socket programs available? The Richard Stevens' "Unix network programming" book described a ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: linux TCP socket program for java client to c server

  1. linux TCP socket program for java client to c server

    I want to make a java TCP socket client to communicate with a TCP
    server socket on linux.
    Are there some sample C unix server and java client socket programs
    available?

    The Richard Stevens' "Unix network programming" book described a TCP
    server (tcpcliserv04.c) and client (tcpcli04.c). I compiled and
    executed them successfully as follows:

    $ tcpcliserv04 &
    $ tcpcli04 172.20.11.211
    12
    12
    90
    90


    I start to write a java socket client program but it fail to connect.
    Please help.
    Thanks.

    import java.net.*;
    import java.io.*;

    public class socketClientWriteRead
    {
    public static final int PORT = 9877;

    public static void main(String[] args) throws IOException
    {
    String host = "";
    if (args.length >= 1)
    host = args[0];
    //String host = args[0];
    byte[] bytes = new byte[1024];
    int len = 0;
    int ch = 0;
    Socket sock = new Socket(host, PORT);
    InputStream in = sock.getInputStream();
    OutputStream out = sock.getOutputStream();
    System.out.println( "Hello World!" );

    while (len > 0 || ch != -1) {
    if ((len = in.read(bytes)) > 0)
    System.out.write(bytes, 0, len);

    if ((ch = System.in.read()) != -1)
    out.write(ch);
    } // while
    in.close();
    out.close();
    }
    }

    $ java socketClientWriteRead 172.20.11.66
    Exception in thread "main" java.net.ConnectException: Connection
    refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:
    333)
    at
    java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    at java.net.Socket.connect(Socket.java:518)
    at java.net.Socket.connect(Socket.java:468)
    at java.net.Socket.(Socket.java:365)
    at java.net.Socket.(Socket.java:179)
    at socketClientWriteRead.main(socketClientWriteRead.j ava:18)


  2. Re: linux TCP socket program for java client to c server

    TsanChung wrote:

    > I want to make a java TCP socket client to communicate with a TCP
    > server socket on linux.
    > Are there some sample C unix server and java client socket programs
    > available?
    >
    > The Richard Stevens' "Unix network programming" book described a TCP
    > server (tcpcliserv04.c) and client (tcpcli04.c). I compiled and
    > executed them successfully as follows:
    >
    > $ tcpcliserv04 &
    > $ tcpcli04 172.20.11.211

    [snip]
    >
    > $ java socketClientWriteRead 172.20.11.66


    Did you mean to connect to a different server with the Java client?

    The error below means that the server refused the connection, usually because
    there is nothing listening on the requested port.

    > Exception in thread "main" java.net.ConnectException: Connection
    > refused
    > at java.net.PlainSocketImpl.socketConnect(Native Method)
    > at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:
    > 333)
    > at
    > java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    > at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    > at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    > at java.net.Socket.connect(Socket.java:518)
    > at java.net.Socket.connect(Socket.java:468)
    > at java.net.Socket.(Socket.java:365)
    > at java.net.Socket.(Socket.java:179)
    > at socketClientWriteRead.main(socketClientWriteRead.j ava:18)


    --
    Nigel Wade

  3. Re: linux TCP socket program for java client to c server

    On Oct 10, 7:51*pm, TsanChung wrote:
    > I want to make a java TCP socket client to communicate with a TCP
    > server socket on linux.
    > Are there some sample C unix server and java client socket programs
    > available?
    >
    > The Richard Stevens' "Unix network programming" book described a TCP
    > server (tcpcliserv04.c) and client (tcpcli04.c). *I compiled and
    > executed them successfully as follows:
    >
    > $ tcpcliserv04 &
    > $ tcpcli04 *172.20.11.211
    > 12
    > 12
    > 90
    > 90
    >
    > I start to write a java socket client program but it fail to connect.
    > Please help.
    > Thanks.
    >
    > import java.net.*;
    > import java.io.*;
    >
    > public class socketClientWriteRead
    > {
    > * *public static final int PORT = 9877;
    >
    > * *public static void main(String[] args) throws IOException
    > * *{
    > * * * * *String host = "";
    > * * * * *if (args.length >= 1)
    > * * * * * * *host = args[0];
    > * * * * * * *//String host = args[0];
    > * * * * *byte[] bytes = new byte[1024];
    > * * * * *int len = 0;
    > * * * *int ch = 0;
    > * * * * *Socket sock = new Socket(host, PORT);
    > * * * * *InputStream in = sock.getInputStream();
    > * * * * *OutputStream out = sock.getOutputStream();
    > * * * *System.out.println( "Hello World!" );
    >
    > * * * * *while (len > 0 || ch != -1) {
    > * * * * * * if ((len = in.read(bytes)) > 0)
    > * * * * * * * *System.out.write(bytes, 0, len);
    >
    > * * * * * if ((ch = System.in.read()) != -1)
    > * * * * * * *out.write(ch);
    > * * * * *} // while
    > * * * * *in.close();
    > * * * *out.close();
    > * * }
    >
    > }
    >
    > $ java socketClientWriteRead 172.20.11.66
    > Exception in thread "main" java.net.ConnectException: Connection
    > refused
    > * * * * at java.net.PlainSocketImpl.socketConnect(Native Method)
    > * * * * at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:
    > 333)
    > * * * * at
    > java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    > * * * * at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    > * * * * at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    > * * * * at java.net.Socket.connect(Socket.java:518)
    > * * * * at java.net.Socket.connect(Socket.java:468)
    > * * * * at java.net.Socket.(Socket.java:365)
    > * * * * at java.net.Socket.(Socket.java:179)
    > * * * * at socketClientWriteRead.main(socketClientWriteRead.j ava:18)



    On which port your server is listening ?
    On which port your client is trying to connect to server ?



  4. Re: linux TCP socket program for java client to c server

    On 10 Oct 2008 at 14:51, TsanChung wrote:
    > $ tcpcliserv04 &

    [snip]
    > I start to write a java socket client program but it fail to connect.


    Your client program seems fine (it's working OK here at least) - as long
    as the server (actually called tcpserv04 in the books's provided source
    code?) is still alive then you shouldn't have any problem connecting.


+ Reply to Thread