I have some code here to connect to a server and I'm getting the error java.net.ConnectException: Connection Refused.
I tried using ping on the command line and I received packets of data so I know that it's working.
I also tried using telnet to connect to my IP Address and it gave me the same Connection Refused error. Is there something wrong with my code? I'm unsure as to why my connection is getting refused.
Client:
public class InstantMessagingClient
{
private Socket socket;
public InstantMessagingClient(String serverName, int port)
{
new Thread() {
public void run()
{
try
{
System.out.println("Connecting to " + serverName + " and port: " + port);
//socket.setSoTimeout(10000);
socket = new Socket(serverName, port);
System.out.println("Client: connected to server at address: " + socket.getLocalAddress() + " and port: " + socket.getPort());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}.start();
}
}
Server:
public class InstantMessagingServer
{
private int port;
private Socket clientSocket;
private ServerSocket serverSocket;
public InstantMessagingServer(int port)
{
try
{
this.port = port;
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
System.out.println("Server: Connected to port");
new Thread()
{
public void run()
{
while(true)
{
try
{
System.out.println("Server: Waiting for port " + serverSocket.getLocalPort());
System.out.println(serverSocket.getLocalSocketAddress());
clientSocket = serverSocket.accept();
System.out.println("Server: Just connected to " + clientSocket.getRemoteSocketAddress());
}
catch(Exception e)
{
System.out.println(e.toString());
break;
}
}
}
}.start();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Main:
public void main()
{
InstantMessagingServer server = new InstantMessagingServer(18638);
InstantMessagingClient client = new InstantMessagingClient("(My IP Address)", 18638);
}