我如何使用IP地址和端口号连接到服务器套接字(客户端与服务器运行在不同的计算机上)
客户程序
public class client implements Runnable {
protected static String server_IP = "141.117.57.42";
private static final int server_Port = 5555 ;
protected static String client_IP ;
public static void main(String[] args) throws IOException{
final String host = "localhost";
int init = 0 ;
try {
InetAddress iAddress = InetAddress.getLocalHost();
client_IP = iAddress.getHostAddress();
System.out.println("Current IP address : " +client_IP);
} catch (UnknownHostException e) {
}
try {System.out.println("hello1");
Socket socket = new Socket(server_IP,server_Port);
System.out.println("hello3");
init = initialize(socket);
}catch (SocketException e) {
System.out.println("Error: Unable to connect to server port ");
}
if (init == 0 ){
System.out.println("error: Failed to initialize ");
System.exit(0);
}
//Thread init_Thread = new Thread();
}
private static int initialize(Socket socket ) throws IOException{
System.out.println("hello");
int rt_value = 0 ;
OutputStream os = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(os, true);
System.out.println("server: " + br.readLine());
pw.println("192.343.34.321");
// BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
//String userInput = userInputBR.readLine();
//out.println(userInput);
socket.close();
return rt_value = 1 ;
}
public void run(){
}
}
服务器端程序
public class server {
protected static String server_IP ;
public static void main(String[] args) throws IOException {
int server_Port = 5555 ;
try {
InetAddress iAddress = InetAddress.getLocalHost();
server_IP = iAddress.getHostAddress();
System.out.println("Server IP address : " +server_IP);
} catch (UnknownHostException e) {
}
ServerSocket serverSocket = new ServerSocket(server_Port);
while (true) {
Socket socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
pw.println("Connection confirmed ");
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
pw.println("your ip address is " + str);
pw.close();
//socket.close();
//System.out.println("Just said hello to:" + str);
}
当我将客户端中的server_IP更改为“本地主机”时,如何使用ip地址和端口号(客户端在与服务器不同的计算机上运行)连接到服务器套接字,所以它非常有用。
-
要连接您的代码,请使用:
Socket socket = new Socket(server_IP,server_Port);
因此,您可以使用:
Socket socket = new Socket("192.168.1.4", 5555);
看起来您的代码中有这个,所以我不确定您遇到什么问题。
如果路由器位于本地网络之外,请不要忘记将路由器设置为转发端口。
http://www.wikihow.com/Set-Up-Port-Forwarding-on-a-
Router不要忘记,如果您正在运行防火墙,这也会干扰连接。
-
是否可以在同一台计算机上运行套接字服务器和套接字客户端?
2021-01-30 关注 0 浏览109 1答案
-
如何查找连接到服务器的客户端的IP地址?
2021-01-30 关注 0 浏览80 1答案
-
使用套接字的客户端-服务器-客户端通信
2021-01-30 关注 0 浏览120 1答案
-
立即检测客户端与服务器套接字的断开连接
2021-02-02 关注 0 浏览84 1答案
-
套接字Java客户端-Python服务器
2021-01-29 关注 0 浏览92 1答案
-
Java套接字。服务器-客户端通信
2021-01-29 关注 0 浏览112 1答案
-
如何从服务器获取客户端套接字的确认?
2021-01-30 关注 0 浏览89 1答案
-
使用套接字的文件传输服务器/客户端
2021-02-01 关注 0 浏览99 1答案
-
使用套接字的多线程客户端-服务器聊天
2021-01-30 关注 0 浏览77 1答案
-
使用asyncio的Python简单套接字客户端/服务器
2021-01-29 关注 0 浏览125 1答案