我學習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