【问题标题】:Basic Client-Server Programming in Java using sockets使用套接字的 Java 基本客户端-服务器编程
【发布时间】:2017-02-02 16:35:28
【问题描述】:

在我的项目中,我制作了一个基本的客户端-服务器通信系统。客户端是一个 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());
    }
}
}

这是数据库;

public class Database {
Connection connection = null;
Statement statement;   
public Database(){
    try {
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
        statement = connection.createStatement();
        connection.setAutoCommit(false);
    } catch (ClassNotFoundException | SQLException e1){
        e1.printStackTrace();
    }
}
public Statement addCommand(String command) throws SQLException{
    System.out.println(command);
    Statement statement = connection.createStatement();
    statement.setQueryTimeout(300);
    statement.executeUpdate(command);
    return statement;
}
public void executeTransaction() throws SQLException{
    connection.commit();
}
}

如您所见,我可以指定端口号 - 意味着将其设置为常量!如何设置IP地址(不是本地的)? 谢谢

【问题讨论】:

    标签: java eclipse sqlite sockets websocket


    【解决方案1】:

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

    在您的客户端代码中找到类似于以下内容的行:

    Socket socket = new Socket(server_IP, server_Port);
    

    然后将server_IP 参数替换为服务器的IP。

    看看this堆栈溢出问题可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多