java 如何使用java套接字通过同一网络连接两台计算机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29053357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:35:46  来源:igfitidea点击:

how to connect two computers over the same network using java sockets?

javasocketsnetworkingnetwork-programmingclient

提问by programeer

I am not able to connect two different machines over the same network using the following client server programs. the code however runs fine on the same machine. I think in the client program it goes in an infinite loop just before socket.accept(); please suggest a possible solution.

我无法使用以下客户端服务器程序通过同一网络连接两台不同的机器。但是代码在同一台机器上运行良好。我认为在客户端程序中,它就在之前进入无限循环socket.accept();请提出一个可能的解决方案。

server.java

服务器.java

import java.io.*;
import java.net.*;
import java.lang.*;

class server{
    public static void main(String args[]){
        try{
            int one,zero;
            one=zero=0;
            ServerSocket sock=new ServerSocket(2000);
            Socket soc=sock.accept();
            DataInputStream dis=new DataInputStream(soc.getInputStream());
            System.out.println("Connection Established");
            String msg =dis.readLine();
            System.out.println("MESSAGE : "+msg);
            for(int i=0;i<msg.length();i++){
                if(msg.charAt(i)=='0')
                    zero++;
                else
                    one++;
            }
            System.out.println("Ones are "+one);
            System.out.println("Zeros are "+zero);

            soc.close();
        }
        catch(IOException e){
            System.out.println(e);

        }
    }
}

client.java

客户端.java

    import java.io.*;
    import java.net.*;
    class client{
        public static void main(String args[]){
            try{
                Socket soc=new Socket("localhost",2000);//or ipv4 address for different computers
                BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
                PrintStream pr=new PrintStream(soc.getOutputStream());

                System.out.println("Enter message..");
                String msg =is.readLine();
                pr.println(msg);
                System.out.println("YOU ENTERED.."+msg);
                soc.close();
            }
            catch(IOException e){
                System.out.println(e);

            }
        }
    }

回答by Jean

I had the same problem and here is how I figured it out : UDP Broadcast. It will allow the client to connect to server regardless of its IP, so you don't have to hardcode the IP Address, only the port used for UDP (see below).

我遇到了同样的问题,这是我想出来的方法:UDP 广播。它将允许客户端连接到服务器而不管其 IP,因此您不必对 IP 地址进行硬编码,只需对用于 UDP 的端口进行硬编码(见下文)。

Here is how it works :

下面是它的工作原理 :

1.Server watch port n

1.服务器监听端口n

2.Client send message at all port n he can reach

2.Client在他能到达的所有端口n发送消息

3.When a message reach server's port, Server response with to the sender and include its IP address

3.当消息到达服务器的端口时,服务器响应发送者并包含其IP地址

4.Client create a socket and connect to the IP address he got from the server

4.Client创建一个socket并连接到他从服务器得到的IP地址

Here is the tutorial that helped me : http://michieldemey.be/blog/network-discovery-using-udp-broadcast/

这是帮助我的教程:http: //michieldemey.be/blog/network-discovery-using-udp-broadcast/