如何使用IP地址和端口号连接到服务器套接字(客户端在与服务器不同的机器上运行)

编程入门 行业动态 更新时间:2024-10-28 08:26:13
本文介绍了如何使用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); }

如何使用ip地址和端口号连接到服务器套接字(客户端运行在与服务器不同的机器上)当我将客户端的server_IP更改为本地主机时,它完美地运行任何帮助都将受到赞赏。

how do i connect to the server socket using the ip address and port number( client is running on a different machine than server) when i change the server_IP in client to "local host" it works perfectly any help would be appreciated.

推荐答案

要使用以下代码连接:

Socket socket = new Socket(server_IP,server_Port);

所以你可以使用:

Socket socket = new Socket("192.168.1.4", 5555);

看起来你的代码中有这个,所以我不确定你有什么问题。

It looks like you have this in your code so I'm not sure what problem you're having.

不要忘记,如果路由器位于本地网络之外,则必须将路由器设置为转发端口。

Don't forget that you have to setup your router to forward ports if it is located outside of your local network.

www.wikihow/在路由器上设置端口转发

不要忘记,如果您正在运行防火墙,这也会干扰连接。

Don't forget that if you are running a firewall, this can also interfere with the connection.

更多推荐

如何使用IP地址和端口号连接到服务器套接字(客户端在与服务器不同的机器上运行)

本文发布于:2023-08-07 03:30:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   在与   连接到   如何使用   客户端

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!