Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用套接字在Java中进行基本客户机-服务器编程_Java_Eclipse_Sqlite_Sockets_Websocket - Fatal编程技术网

使用套接字在Java中进行基本客户机-服务器编程

使用套接字在Java中进行基本客户机-服务器编程,java,eclipse,sqlite,sockets,websocket,Java,Eclipse,Sqlite,Sockets,Websocket,在我的项目中,我制作了一个基本的客户机-服务器通信系统。客户端是一个android应用程序,下面是我在eclipse中编程的.jar文件形式的服务器。只要连接是本地或本地主机,客户端和服务器之间的通信就可以完美地工作——但现在我想扩展类功能,以便它可以连接到特定的IP地址和特定的端口号——然后应该支持多个连接!!!我真的不知道在哪里做改变这里 public class MinimalServer { ServerSocket s; public static void main(String[

在我的项目中,我制作了一个基本的客户机-服务器通信系统。客户端是一个android应用程序,下面是我在eclipse中编程的.jar文件形式的服务器。只要连接是本地或本地主机,客户端和服务器之间的通信就可以完美地工作——但现在我想扩展类功能,以便它可以连接到特定的IP地址和特定的端口号——然后应该支持多个连接!!!我真的不知道在哪里做改变这里

public class MinimalServer {
ServerSocket s;

public static void main(String[] args) {
    try {           
        Database database = new Database();
        MinimalServer server = new MinimalServer();
        server.listAvailableInterfaces();
        InetAddress address;
        do{
            address = server.getAvailableAddress();
        } while(address==null);
        server.initialize(address);
        server.serverRoutine(database);
    } catch (IOException | SQLException e) {}
}

InetAddress getAvailableAddress() throws IOException{
    int result=0;
    NetworkInterface resultName=null;
    @SuppressWarnings("resource")
    Scanner in = new Scanner(System.in);
    do{
        System.out.println("Please type in the interface index you would like to start the server onto:");
        result = Integer.parseInt(in.nextLine());
        if(NetworkInterface.getByIndex(result)!=null){
            System.out.println("Your choice was "+ NetworkInterface.getByIndex(result)+"\n");
            resultName = NetworkInterface.getByIndex(result); 
        }
    } while(resultName==null);
    Enumeration<InetAddress> e1 = resultName.getInetAddresses();
    InetAddress adr = null;
    while(e1.hasMoreElements()){
        InetAddress elem = e1.nextElement();
        if(elem.getAddress().length==4)
            adr=elem;
    }
    if(adr==null)
        System.err.println("No available IPv4 Address found on selected Interface!!");
    return adr;
}
void serverRoutine(Database database) throws IOException, SQLException{
    Socket socket = null;
    while(true) {
        try {
            socket = s.accept();
            System.out.println("Socket: "+socket.toString());
        } catch(SocketTimeoutException e1){}
        if(socket!=null){
            BufferedReader dis = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
            while(dis.ready()){
                database.addCommand(dis.readLine());
            }
            database.executeTransaction();
        }
    }
}
void initialize(InetAddress address) throws IOException{
    s = new ServerSocket(50000,10,address);
    s.setSoTimeout(200);
    System.out.println("Server here : "+s.getInetAddress().getHostAddress()+":"+s.getLocalPort());
}
void listAvailableInterfaces() throws SocketException{
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    int total=0;
    System.out.println("Available Network interfaces: \nID\t--------\tIndex");
    while(e.hasMoreElements()){
        total++;
        NetworkInterface a = e.nextElement();
        System.out.println(a.getName()+"\t---------\t"+total);
        Enumeration<InetAddress> adr = a.getInetAddresses();
        while(adr.hasMoreElements())
            System.out.println("\tIP: "+adr.nextElement().toString());
    }
}
}
正如您所看到的,我可以指定端口号-意味着将其设置为常量!如何设置IP地址tho(非本地)?
谢谢

您无法明确设置服务器或数据库的IP地址,因为内部IP由路由器设置,而外部IP由ISP设置。为了连接到本地网络,您需要在连接时在客户端应用程序中指定服务器的IP

在客户端代码中查找与以下类似的行:

Socket socket = new Socket(server_IP, server_Port);
然后用
server\u IP
参数替换服务器的IP

看看堆栈溢出问题,它可能会有一些帮助

Socket socket = new Socket(server_IP, server_Port);