473,463 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:179)
at socketClientWriteRead.main(socketClientWriteRead.j ava:18)

Oct 10 '08 #1
3 11624
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.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:179)
at socketClientWriteRead.main(socketClientWriteRead.j ava:18)
--
Nigel Wade
Oct 10 '08 #2
On Oct 10, 7:51*pm, TsanChung <tsanchung.w...@gmail.comwrote:
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.<init>(Socket.java:365)
* * * * at java.net.Socket.<init>(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 ?
Oct 18 '08 #3
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.

Oct 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
7
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
2
by: happyvalley | last post by:
Hi, don't know if it is the place to ask this question I have a c++ server and a java client communicate via socket in Java client // create a client socket Socket clientSocket = new...
0
by: mhetfield | last post by:
Hi, I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client. ...
2
by: nsaffary | last post by:
hi I hava a client/server program that run correctly when i run it in one computer(local) but when I run client on a one computer and run server run on another, connection does not stablish.(I set...
4
by: The Doctor | last post by:
Hey people, I have two applications: the server, which creates a server socket, waits for a real-time signal, and if it receives one, it creates a client socket. The client, which will...
10
by: Elaine121 | last post by:
Hi i've been batteling for hours and can't seem to find the problem. When my server runs and I press the connect button the gui freezes until the client gui is terminated.. only then the gui becomes...
1
by: EmilKTH | last post by:
Hello everybody, Maybe someone from you can help me. I am writing a server/clients in java, and i need to store the request IP addresses from the clients and respond only to them with a same udp...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.