I have a little problem making servers and sockets connect. I made sure IPs are valid, so it's not an issue of the addresses not matching. Nor is it the port that doesn't match(I've tested this, as well).
The following is the server handler
class ServerConnect extends Thread
{
// ...
public ServerConnect(int port, String pass) throws IOException
{
// ...
this.start();
}
public void run()
{
try
{
// ...
while (! server.isClosed()) // Make Connections
{
Socket newClient = server.accept();
System.out.println("Connection Accepted!");
// ...
}
}
// ...
}
}
And this is the socket connector
class ClientConnect extends Thread
{
// ...
public ClientConnect(InetAddress ip, int port, String pass)
{
try
{
System.out.println("Connecting to host:\t" + ip.toString() + ":" + port);
client = new Socket(ip, port);
output = new PrintWriter(client.getOutputStream(), true);
input = new Scanner(client.getInputStream());
// ...
}
// ...
}
// ...
}
First, I tried testing locally. I entered my IP, and the IP 127.0.0.1, and both connected me to the server I had set up locally. But, then I tried from another IP, and here's what happened. The thread that was making the connection froze for a short while, then simply skipped down to the exception and told me that it couldn't find a host, and the host never says that it accepted a connection. If it helps, I'm using the port 4424 to connect with the server. Is there anything that could cause this in my code, or is there something I'm not doing to prevent this?
Also, please let me know if I'm not providing enough information. I've done plenty of te开发者_运维问答sting, and can do more to help figure out what is wrong.
Sounds like a network problem. Did you try to ping the server from your client machine? Did you test whether the port was reachable (by using telnet or an online port tester)?
Just type those two commands in a console of your choice, should work in most OS's, and see if a connection can be established:
ping <hostname>
telnet <hostname> <port>
Sounds like the server is not reachable from the client. Can you ping the server's IP address from the client? Is there a firewall between the server and client? Does the server run a local firewall?
精彩评论