我编写了一个程序,客户端应该能够建立连接,并将som消息写入服务器。服务器应该打印此消息。
问题是,一旦客户端连接到服务器,我就会收到以下消息的异常:
java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at Client.<init>(Client.java:21)
at Server.startServer(Server.java:42)
at Server.main(Server.java:62)
我的服务器代码如下所示:
import java.io.*;
import java.net.*;
public class Server {
private int port = 5555;
public void printServerAddress() {
InetAddress i = null;
String host = null;
try {
i = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
host = i.getHostAddress();
System.out.println("Contact this server at address: " + host + " and port: " + port);
}
public void startServer() {
ServerSocket serverSocket = null;
BufferedReader reader = null;
Socket socket = null;
String fromClient = null;
try {
serverSocket = new ServerSocket(port);
while(true) {
socket = serverSocket.accept();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(socket != null) {
System.out.println("Connection from: " + socket);
}
Thread thread = new Thread(new Client(socket));
thread.start();
while((fromClient = reader.readLine()) != null) {
System.out.println("From client: " + fromClient);
}
reader.close();
socket.close();
}
} catch (IOException e) {
}
}
public static void main(String[] args) {
Server server = new Server();
server.printServerAddress();
server.startServer();
}
}
客户端的代码是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client implements Runnable{
private int port;
private String serverAddress;
private PrintWriter writer;
private BufferedReader reader;
public Client(Socket socket)
{
try {
socket = new Socket(serverAddress, port);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
System.out.println("Write message to server:");
Scanner scanner = new Scanner(System.in);
while(scanner.next() != "quit") {
writeToServer(scanner.next());
}
writer.close();
}
public void writeToServer(String message) {
writer.write(message);
}
public void readFromServer() {
String msg = null;
try {
while((msg = reader.readLine()) != null) {
System.out.println("From server: " + msg);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("localhost", 5555);
new Client(s);
}
}
知道我做错了什么,以及如何解决我的问题?
答案 0 :(得分:3)
socket = new Socket(serverAddress, port);
serverAddress为null,port为0。
尝试在调用new Socket(serverAddress, port)
或更改
socket = new Socket(serverAddress,port);
到
this.socket = socket;
您可以将构造函数更改为:
public Client(String serverAddress, int port)
{
try {
Socket socket = new Socket(serverAddress, port);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}