473,545 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ 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 socketClientWri teRead
{
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.getInputSt ream();
OutputStream out = sock.getOutputS tream();
System.out.prin tln( "Hello World!" );

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

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

$ java socketClientWri teRead 172.20.11.66
Exception in thread "main" java.net.Connec tException: Connection
refused
at java.net.PlainS ocketImpl.socke tConnect(Native Method)
at java.net.PlainS ocketImpl.doCon nect(PlainSocke tImpl.java:
333)
at
java.net.PlainS ocketImpl.conne ctToAddress(Pla inSocketImpl.ja va:195)
at java.net.PlainS ocketImpl.conne ct(PlainSocketI mpl.java:182)
at java.net.SocksS ocketImpl.conne ct(SocksSocketI mpl.java: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 socketClientWri teRead.main(soc ketClientWriteR ead.java:18)

Oct 10 '08 #1
3 11642
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 socketClientWri teRead 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.Connec tException: Connection
refused
at java.net.PlainS ocketImpl.socke tConnect(Native Method)
at java.net.PlainS ocketImpl.doCon nect(PlainSocke tImpl.java:
333)
at
java.net.PlainS ocketImpl.conne ctToAddress(Pla inSocketImpl.ja va:195)
at java.net.PlainS ocketImpl.conne ct(PlainSocketI mpl.java:182)
at java.net.SocksS ocketImpl.conne ct(SocksSocketI mpl.java: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 socketClientWri teRead.main(soc ketClientWriteR ead.java: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 socketClientWri teRead
{
* *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.getInputSt ream();
* * * * *OutputStream out = sock.getOutputS tream();
* * * *System.out.pri ntln( "Hello World!" );

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

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

}

$ java socketClientWri teRead 172.20.11.66
Exception in thread "main" java.net.Connec tException: Connection
refused
* * * * at java.net.PlainS ocketImpl.socke tConnect(Native Method)
* * * * at java.net.PlainS ocketImpl.doCon nect(PlainSocke tImpl.java:
333)
* * * * at
java.net.PlainS ocketImpl.conne ctToAddress(Pla inSocketImpl.ja va:195)
* * * * at java.net.PlainS ocketImpl.conne ct(PlainSocketI mpl.java:182)
* * * * at java.net.SocksS ocketImpl.conne ct(SocksSocketI mpl.java: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 socketClientWri teRead.main(soc ketClientWriteR ead.java: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
5424
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 server works OK. I've heard, that P2.3 has SSL support included in himself and also, I was trying P2.2 with pyOpenSSL wrappers and extensions, but...
7
3350
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 communicate directly with the database. Do I need a separate db2 connect on each such machine. Please advice.
2
5174
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 sends the message "Ping" to the server, which reads it and answers with a "Pong". The game is really simple and the coding should be also very simple!...
2
6484
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 Socket("localhost", 27015); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
0
1818
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. - the client should send this message (after the connection established): my password "anypassword" and the server will check, if it's not the same...
2
4085
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 server machine IP for client and server) please guide me? server : #include <winsock2.h> #include <iostream> #include <stdio.h> #include...
4
5488
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 connect to the server. This will generate a real- time signal at the server, who will create a client socket for this client.
10
4206
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 active again and displays the messages. Here is my server code: import java.io.*; import java.net.*; public class serverForm extends...
1
2311
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 package just a different ip address ofcourse. The point is i have a server that broadcast udp packages and clients that respond to that broadcast,...
0
7470
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7405
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7659
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7760
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.