The following code is correct when compiled, but when run it says: ConnectException
the error image:

import java.net.*;
import java.io.*;
class TcpChat
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("Ip",20000);
ServerSocket ss = new ServerSocket(20000);
new Thread(new TcpClient(s)).start();
new Thread(new TcpServer(ss)).start();
}
}
class TcpClient implements Runnable
{
Socket s;
TcpClient(Socket s)
{
this.s = s;
}
public void run()
{
try
{
OutputStream out = s.getOutputStream();
out.write("hello javaserver".getBytes());
s.close();
}
catch (Exception e)
{
}
}
}
class TcpServer implements Runnable
{
ServerSocket ss;
TcpServer(ServerSocket ss)
{
this.ss = ss;
}
public void run()
{
try
{
Socket s = ss.accept();
InputStream in = s.getInputStream();
byte[] buf =new byte[1024];
int length =in.read(buf);
String ip =s.getInetAddress().getHostAddress();
String data = new String(buf,0,length);
System.out.println(ip+":::"+data);
s.close();
ss.close();
}
catch (Exception e)
{
}
}
}
Additionally, there is no error about the IP address I use, in my PC I use my own IP.
You didn’t post your whole code, so it’s hard to check, but it seems like you try to connect to a port (i.e. open the client socket) before you actually open the server socket. That won’t work, of course, since there’s nothing listening at that port yet (thus: connection refused).