尝试使用java套接字通过TCP连接到服务器,连接被拒绝。我应该发送密钥进行身份验证。代码:
Socket clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress("server.address.whatever", 123456));
System.out.println("Connected");
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String key = "key";
outToServer.writeBytes(key + "\r\n");
String response = inFromServer.readLine();
System.out.println("FROM SERVER: " + response);
clientSocket.close();
它永远不会使它试图打印出来#34;已连接"它会抛出
java.net.ConnectException: Connection refused
所以我从未发过钥匙。我在这里失踪了什么?如何在连接期间发送初始消息?我甚至应该这样做吗?
答案 0 :(得分:0)
听起来没有服务器进程在该套接字上侦听。检查您是否可以使用nmap(或只是telnet)等工具建立连接。