java socket programming problem
client:
import java.io.*;
import java.net.*;
public class Requester
{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Requester(){}
void run() throws IOException
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("172.16.3.219", 2004);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.get开发者_如何学PythonInputStream());
//3: Communicating with the server
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
message=br.readLine();
sendMessage(message);
}
catch(ClassNotFoundException classNot)
{
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost)
{
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client1>" + msg);
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
public static void main(String args[]) throws IOException
{
Requester client = new Requester();
client.run();
}
}
server:
import java.io.*;
import java.net.*;
public class Provider
{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from" + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do
{
try
{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
{
sendMessage("bye");
}
else
{
message=br.readLine();
sendMessage(message);
}
}
catch(ClassNotFoundException classnot)
{
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try
{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
public static void main(String args[])throws IOException
{
Provider server = new Provider();
while(true)
{
server.run();
}
}
}
Server program is at 172.16.3.219. If both server and client are with in one system(172.16.3.219) programs are working.If a place my client program in some other system let us say 172.16.3.30 i am getting Exceptions(still server is at 172.16.3.219). Should i change socket() constructor. If the problem is with constructor suggest me suitable constructor when client and server are in different systems
The code looks OK; do you have a firewall on either the client or the server?
I was trying to test code which sent mail. For that, I installed a local mail server but couldn't connect: The admins had disabled the SMTP port to stop spammers.
[EDIT] "Connection refused" means that no server is running on that port on that server. Run netstat -tan
(Linux) or netstat -tn
(Windows) and look for lines with LISTEN
. These are the server processes running. The output will look like this:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
The interesting part is "Local Address". It contains the network interface (IP address) on which the server is listening. The first entry listens on all network interfaces while the second is attached to "localhost" (also called "local loopback"). It will ignore any connection attempts from the LAN.
Make sure that your server is listed with 0.0.0.0:2004
. If it isn't, try:
providerSocket = ServerSocket(2004, 10, InetAddress.getByName("0.0.0.0"));
If that doesn't work, try the public IP address of the server network card.
I guess (for some reason) the server is only listening the loopback ip address. Try binding on a specific ip address, something like:
//1. creating a server socket
providerSocket = ServerSocket(2004, 10, InetAddress.getByName("172.16.3.219"));
精彩评论