0
我用一個簡單的程序來連接到服務器:連接拒絕與Java套接字?
這裏是:
public static void main(String[] args) throws Exception {
String sentence;
String modsent;
//input Stream
BufferedReader fromclient = new BufferedReader(new InputStreamReader(System.in));
//client Socket
Socket clientsock = new Socket("stockholm.serverdhse2.se", 9000);
//output Stream attached to socket
DataOutputStream serverout = new DataOutputStream(clientsock.getOutputStream());
//input from Server
BufferedReader fromserver = new BufferedReader(new InputStreamReader(clientsock.getInputStream()));
sentence = fromclient.readLine();
serverout.writeBytes(sentence + "\n");
modsent = fromserver.readLine();
System.out.println("From Server: " + modsent);
clientsock.close();
}
}
但現在我得到異常:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at register.main(register.java:22)
可能是什麼問題呢?
你有服務器在'「stockholm.serverdhse2.se」,9000' –
我在想同樣的路線。這臺服務器是否活躍? – Less
是的!該服務器肯定存在,並已上漲!我的另一個問題是:如何通過套接字發送字符串到服務器? – maximus