gpt4 book ai didi

android - 在客户端搜索服务器 IP 而不是输入

转载 作者:行者123 更新时间:2023-12-03 11:55:35 24 4
gpt4 key购买 nike

服务器:电脑

客户端:安卓

所以我的客户端/服务器应用程序包括打开网页和执行机器人,如果我只用于路由器,一切都很好,但我希望能够在不同的地方连接(不同的路由器/PC)。
我正在搜索“IP 的 Wi-fi 搜索”,但一无所获。
是否可以给服务器端一个修复IP?像往常一样 192.168.1.68?

客户代码

public class AndroidClient extends Activity 
{
EditText episode;
Spinner spinner1;
String result;
Button buttonConnect, buttonClear;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
episode = (EditText) findViewById(R.id.episode);
buttonConnect = (Button) findViewById(R.id.connect);
buttonClear = (Button) findViewById(R.id.clear);


spinner1 = (Spinner) findViewById(R.id.Animes);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Anime, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);

addListenerOnSpinnerItemSelection();


addListenerOnButton();
}

public void addListenerOnSpinnerItemSelection() {

spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}


public void addListenerOnButton() {

spinner1 = (Spinner) findViewById(R.id.Animes);

buttonConnect = (Button) findViewById(R.id.connect);

buttonConnect.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

MyClientTask myClientTask = new MyClientTask();

myClientTask.execute();
}

});

}


public class MyClientTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... arg0) {

Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;

try {
socket = new Socket("10.1.3.68", 8080);
dataOutputStream = new DataOutputStream(
socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(episode.getText() + "-" + String.valueOf(spinner1.getSelectedItem()));

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}

@Override
protected void onPostExecute(Void result) {

}

}

}

服务器代码
public class ServerSide extends Application 
{
TextField textTitle;
Label labelSys, labelPort, labelIp;
Label labelMsg;
CheckBox optWelcome;

ServerSocket serverSocket;

String message = "";
String result;

@Override
public void start(Stage primaryStage) {

textTitle = new TextField();
labelSys = new Label();
labelPort = new Label();
labelIp = new Label();
labelMsg = new Label();




labelIp.setText(getIpAddress());

VBox mainLayout = new VBox();
mainLayout.setPadding(new Insets(5, 5, 5, 5));
mainLayout.setSpacing(5);
mainLayout.getChildren().addAll(textTitle,
labelSys, labelPort, labelIp,
labelMsg);

StackPane root = new StackPane();
root.getChildren().add(mainLayout);

Scene scene = new Scene(root, 300, 400);

primaryStage.setTitle("One Piece");
primaryStage.setScene(scene);
primaryStage.show();

Thread socketServerThread = new Thread(new SocketServerThread());
socketServerThread.setDaemon(true);
socketServerThread.start();
}

public static void main(String[] args) {
launch(args);
}

private class SocketServerThread extends Thread {

static final int SocketServerPORT = 8080;
int count = 0;

@Override
public void run() {
try {
Socket socket = null;

serverSocket = new ServerSocket(SocketServerPORT);
Platform.runLater(new Runnable() {

@Override
public void run() {
labelPort.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
}
});

while (true) {
socket = serverSocket.accept();
count++;

//Start another thread
//to prevent blocked by empty dataInputStream
Thread acceptedThread = new Thread(
new ServerSocketAcceptedThread(socket, count));
acceptedThread.setDaemon(true); //terminate the thread when program end
acceptedThread.start();

}
} catch (IOException ex) {
Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
}

}

}

private class ServerSocketAcceptedThread extends Thread {

Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
int count;

ServerSocketAcceptedThread(Socket s, int c) {
socket = s;
count = c;
}

@Override
public void run() {
try {

dataInputStream = new DataInputStream(
socket.getInputStream());
dataOutputStream = new DataOutputStream(
socket.getOutputStream());


//If dataInputStream empty,
//this thread will be blocked by readUTF(),
//but not the others
String messageFromClient = dataInputStream.readUTF();


message += "#" + count + " from " + socket.getInetAddress()
+ ":" + socket.getPort() + "\n"
+ "Msg from client: " + messageFromClient + "\n";

Platform.runLater(new Runnable() {

@Override
public void run() {
labelMsg.setText(message);
}
});
String string = messageFromClient;
String[] parts = string.split("-");
String episode = parts[0];
String anime = parts[1];
String OneP = new String("One Piece");
String Naruto = new String ("Naruto");
String Bleach = new String ("Bleach");





int EPnumb = Integer.parseInt(episode);

if (EPnumb < 10) {
result = "00" + episode;
}
else if (EPnumb < 100 && EPnumb >= 10) {
result = "0" + episode;
}
else { result = episode; }



if (anime.equals(OneP)){
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://kissanime.com/Anime/One-Piece/Episode-"+ result);
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}}


else {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://kissanime.com/Anime/Naruto-Shippuuden/Episode-"+result);
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}

}





} catch (IOException ex) {
Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
} finally {




if (socket != null) {
try {
socket.close();
} catch (IOException ex) {
Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
}
}

if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException ex) {
Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
}
}

if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException ex) {
Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

}

private java.lang.String parseInt(java.lang.String episode) {
// TODO Auto-generated method stub
return null;
}

private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();

if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException ex) {
Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
}

return ip;
}

public DataInputStream String(String string) {



return null;
}

}

最佳答案

您可以使用 ServerSocket(int port, int backlog, InetAddress bindAddr)
构造一个绑定(bind)到给定 localAddress 和端口的新 ServerSocket 实例。

关于android - 在客户端搜索服务器 IP 而不是输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com