Dear all,
I am a very novice Java programmer as you will realise by the content
of my e-mail.
I am currently taking the first steps on creating client-server
applications.
However, I experience the following problem; which must be probably
associated with parameter setting.
I try to run a very simple client-server application; where a server
grabs a port and waits
for a client to be connected. After a connection is established; it is
immediately closed.
I try to test this application on my localhost. Both of the programs
compile successfully. The server part of the program is running
successfully and is awaiting for a connection to be made with a client
(client=sock.accpet();). However, the client fails to connect with the
server and a connection exception is thrown. Alternatively I tried to
test my Server program by running telnet with the following command:
telnet localhost 5000(number of port that the server has grabbed) but
again a connection is failed to be established. Any ideas why?
Many thanks
Dora
Gordon Beaton - 30 Jan 2006 19:09 GMT
> I try to run a very simple client-server application; where a server
> grabs a port and waits for a client to be connected. After a
[quoted text clipped - 9 lines]
> server has grabbed) but again a connection is failed to be
> established. Any ideas why?
Here you write "fail to be established" but earlier you suggested that
the connection succeeded and was then immediately closed. Please
clarify what you mean.
You have neglected to provide two pieces of important information: the
text of the actual exception that you get, and relevant sections of
the server and client code.
However I'll *guess* you are getting "connection refused". This
indicates that your client isn't connecting where the server is
listening. (Either the server isn't listening where you think it is,
or your client isn't connecting where you think it is).
Perhaps your ServerSocket has bound not only the port number but also
the listening address. If that's the case, clients will succeed in
connecting only through the specific network interface the
ServerSocket is bound to, while connection attempts arriving on other
interfaces will fail. Try binding just the port number when you create
the ServerSocket, or use the servers real IP address when connecting.
Note that a unix host has typically at least two network interfaces:
"localhost" and another one that corresponds to the IP address you
normally associate with the host. They are not the same!
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
zero - 30 Jan 2006 19:14 GMT
"dora" <tpolenta@hotmail.com> wrote in news:1138647075.751166.162290
@g47g2000cwa.googlegroups.com:
> Dear all,
>
[quoted text clipped - 20 lines]
> Many thanks
> Dora
What is the exception? And the code that generates it? Not all of your
code please, make it a short but readable (perhaps compilable, if that
doesn't make it too long). Don't leave out any declarations of objects
or variables you use - those are often the biggest clues.
It is quite possible that your code is fine, but the connection is being
blocked by a firewall. You should check that first. It is also
possible that another connection already exists on port 5000. The
exception should tell you if that's the case.
dora - 30 Jan 2006 20:02 GMT
Hello again,
The exception error is the following:
java.net.ConnectException:Connection refused: connect
The code of my program is the following:
MyServer.java
//The following program has the server sending a String to the client
//using the DataOutputStream class
//Demonstrates a ServerSocket listening for requests
//The server uses the SeverSocket class to grab a port and listen
//for requests
import java.io.*;
import java.net.*;
public class MyServer implements Runnable
{
ServerSocket server;
Socket client;
public MyServer(int port) throws IOException
{
server= new ServerSocket(port);
}
public void run()
{
while (true)
{
try {
//communicate with client, then close the connection
client=server.accept();
System.out.println("Made one connection");
OutputStream clientOutputStream=client.getOutputStream();
DataOutputStream out=new DataOutputStream(clientOutputStream);
out.writeUTF("Hello from " + server.getInetAddress());
clientOutputStream.close();
client.close();
} catch (IOException e) {
System.out.println(e);
return;
}
}
}
}
The code of MyServerProgram is:
import java.io.*;
public class MyServerProgram
{
public static void main(String [] args)
{
try {
MyServer server=new MyServer(5000);
Thread t=new Thread(server);
t.start();
System.out.println("Server is ready for a client");
} catch (IOException e) {
e.printStackTrace();
}
}
}
And finally the code of MyClient program is:
//The client uses the Socket class to connect to a server.The
constructor
//needs the name of the server and the port to connect to
//The following program demonstrates a client connecting to the server
//in the earlier example.
//The client will read in the String sent from the server by using the
//DataInputStream class
import java.io.*;
import java.net.*;
public class MyClient
{
public static void main(String[] args)
{
String server="localhost";
//args[0];
int port=5000;
//Integer.parseInt(args[1]);
try
{
Socket host=new Socket(server,port);
//System.out.println("Connected to " +
// host.getInetAddress() );
//InputStream serverInStream=host.getInputStream();
//DataInputStream in=new DataInputStream(serverInStream);
//System.out.println(in.readUTF());
//serverInStream.close();
//communicate with the server, then close the socket
host.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Many Thanks
Dora
> "dora" <tpolenta@hotmail.com> wrote in news:1138647075.751166.162290
> @g47g2000cwa.googlegroups.com:
[quoted text clipped - 33 lines]
> possible that another connection already exists on port 5000. The
> exception should tell you if that's the case.
Gordon Beaton - 31 Jan 2006 09:59 GMT
> The exception error is the following:
> java.net.ConnectException:Connection refused: connect
I don't know what you're doing, but the code you've posted works for
me. Are you running some kind of firewall software on the machine?
In one shell:
[gordon]~/slask$ java MyServerProgram
Server is ready for a client
Made one connection
Made one connection
Made one connection
Made one connection
Made one connection
Made one connection
Made one connection
Made one connection
[gordon]~/slask$
Meanwhile, in another shell:
[gordon]~/slask$ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello from 0.0.0.0/0.0.0.0Connection closed by foreign host.
[gordon]~/slask$ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello from 0.0.0.0/0.0.0.0Connection closed by foreign host.
[gordon]~/slask$ java MyClient
[gordon]~/slask$ java MyClient
[gordon]~/slask$ java MyClient
[gordon]~/slask$ java MyClient
[gordon]~/slask$ java MyClient
[gordon]~/slask$ java MyClient
[gordon]~/slask$
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 30 Jan 2006 21:38 GMT
>I try to run a very simple client-server application; where a server
>grabs a port and waits
>for a client to be connected. After a connection is established; it is
>immediately closed.
see http://mindprod.com/products1.html#ECHOSERVER
for an extremely simple server. It just waits for a connection, and
prints out the contents of one message.
Compare that with your code to get the basics working.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Paulus de Boska - 31 Jan 2006 10:44 GMT
Your code ran fine with me on 1.4.2_10, WinXP
Try 127.0.0.1 as server string, instead of 'localhost'. I remember this
as making a difference, a long time ago.
(A 'flush' helps too, sometimes, but only if there's a connection.)
socket example:
http://javalessons.com/cgi-bin/adv/java-j2ee-jdbc.cgi?subject=java-sockets&1cd=s
ok&sid=ao789
---
Paul Hamaker, SEMM
http://javalessons.com
dora - 31 Jan 2006 13:11 GMT
Hello again,
I do not know what it means firewall software. However, I have recently
instaleed the Norton Antivirus; would that make any difference?
I run the MyServerProgram and the MyClient programs in two MS-DOS
windows.
Secondly, the use of 127.0.0.1 as server string instead of localhost
does not make any difference and the same exception is thrown.
Thanks
Dora
dora - 31 Jan 2006 13:37 GMT
Actually I am running windows firewall. But by setting it off it does
not make any difference.
I still cannot connect. Is it anyway that I can bypass the firewall in
order to establish the connection?
Many Thanks
Dora
zero - 31 Jan 2006 18:37 GMT
"dora" <tpolenta@hotmail.com> wrote in news:1138713119.604954.171110
@g14g2000cwa.googlegroups.com:
> Hello again,
> I do not know what it means firewall software. However, I have recently
[quoted text clipped - 7 lines]
> Thanks
> Dora
try opening a dos box (click start, run, type: "cmd" or "command"), and
type "ping 127.0.0.1" <enter>, "ping localhost" <enter>, and "ping
localhost.localdomain" <enter>. If any of these work, use that one in the
client program.
You can also try to print the local address to stdout by adding the
following line to your server code:
System.out.println(server.getInetAddress().toString());
Then make sure that is indeed the one your client is trying to connect to.
I don't know if Norton Antivirus includes a firewall, but it's very
possible. Today's antivirus software does way more than anyone who's a
little computer savvy needs. Try looking at the program's main window and
see if there is anything about a firewall, and how to turn it off.
Anyway, you can at least be sure it's not your code, which is always nice
to know :-)
dora - 31 Jan 2006 23:04 GMT
Hello again,
Finally good news. I managed to establish a client-server connection.
I had to temporarily set off the Personal Firewall of Norton Internet
Security.
Thanks for all your help
Dora