gpt4 book ai didi

java - 客户端关闭后,Android TCP 服务器仅显示来自 Python 客户端的消息

转载 作者:行者123 更新时间:2023-11-30 01:31:44 29 4
gpt4 key购买 nike

我正在使用一些示例代码,它允许我从 Python 客户端向 Android 服务器 (TCP) 发送消息。但消息只会在客户端关闭后显示在 Android 模拟器上。

我可能缺少对 tcp 套接字背后的一些基本理解(第一次使用和实现)。

我的主要目的是让 Android 应用程序中的按钮在单击时向单独的 Linux 系统上的 Python 客户端发送不同的消息,Python 客户端在收到该消息后发回确认。

下面是我的代码

服务器.java

package test.server2;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;

public class Server {
MainActivity activity;
ServerSocket serverSocket;
String message = "";
static final int socketServerPORT = 8080;

public Server(MainActivity activity) {
this.activity = activity;
Thread socketServerThread = new Thread(new SocketServerThread());
socketServerThread.start();
}

public int getPort() {
return socketServerPORT;
}

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

private class SocketServerThread extends Thread {

int count = 0;

@Override
public void run() {
try {
// create ServerSocket using specified port
serverSocket = new ServerSocket(socketServerPORT);

while (true) {
// block the call until connection is created and return
// Socket object
Socket socket = serverSocket.accept();
count++;


InputStream message2 = socket.getInputStream();
final String messageReceived = convertToString(message2);

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.msg.setText(messageReceived);
}
});


//SocketServerReplyThread socketServerReplyThread =
// new SocketServerReplyThread(socket, count);
//socketServerReplyThread.run();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}
activity.runOnUiThread(new Runnable() {

@Override
public void run() {
activity.msg.setText(message);
}
});
}
}

private class SocketServerReplyThread extends Thread {

private Socket hostThreadSocket;
int cnt;

SocketServerReplyThread(Socket socket, int c) {
hostThreadSocket = socket;
cnt = c;
}

@Override
public void run() {
OutputStream outputStream;
String msgReply = "Hello from Server, you are #" + cnt;

try {
outputStream = hostThreadSocket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(msgReply);
printStream.close();

message += "replayed: " + msgReply + "\n";

activity.runOnUiThread(new Runnable() {

@Override
public void run() {
activity.msg.setText(message);
}
});


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}

activity.runOnUiThread(new Runnable() {

@Override
public void run() {
activity.msg.setText(message);
}
});
}

}

private String convertToString(InputStream message) {
BufferedReader reader = new BufferedReader(new InputStreamReader(message));
StringBuilder stringbuilder = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
//stringbuilder.append(line).append('\n');
stringbuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
message.close();
//reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return stringbuilder.toString();
}

public 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 += "Server running at : "
+ inetAddress.getHostAddress();
}
}
}

} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
}

我已将对 SocketServerReplyThread 的调用和相关注释掉,因为这会导致错误。注释掉会导致从 Python 客户端接收消息,但它只会在客户端套接字关闭后显示在 Android Emulator 上。我希望它是连续的,这样,每当客户端发送消息(并且 android 服务器正在监听)时,消息就会被打印出来。

MainActivity.java

package test2.server;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
Server server;
TextView infoip, msg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();


}
});

infoip = (TextView) findViewById(R.id.infoip);
msg = (TextView) findViewById(R.id.msg);
server = new Server(this);
infoip.setText(server.getIpAddress() + ":" + server.getPort());

}


@Override
protected void onDestroy() {
super.onDestroy();
server.onDestroy();


}
}

Python 客户端

#!/usr/bin/python

import socket
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = "127.0.0.1"
port = 4534
s.connect((host, port))


s.send("Thank you for connecting" + '\n')
time.sleep(10) #added to check if it gets displayed before closing or not
s.close()

最佳答案

套接字默认阻塞。所以当你调用 readline 时,它​​永远不会返回 null,直到套接字关闭。按照编码,您的代码将等到从客户端读取所有数据并且客户端关闭连接,然后将其转换为 1 个大字符串并从 convertToString 返回。您需要重新架构,以便处理每个 readline 调用的结果并显示它,而不是将所有内容都视为 1 个巨大的字符串。

关于java - 客户端关闭后,Android TCP 服务器仅显示来自 Python 客户端的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35660738/

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