我学习socket编程,并在书中的示例所示:为什么客户端套接字将localhost作为参数?
// SimpleClient.java: A simple client program.
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1254
Socket s1 = new Socket(“localhost”,1254);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
我的问题是,在行新的Socket(“localhost”的,1254),为什么地址,而不是服务器的IP本地主机,地址?
因为服务器必须在本地主机上运行。 – EJP