【发布时间】: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