gpt4 book ai didi

java - Android 2-Way TCP/IP ServerSocket/套接字通信

转载 作者:可可西里 更新时间:2023-11-01 02:54:28 29 4
gpt4 key购买 nike

使用这个 great tutorial通过 Java Code Geeks,我可以很容易地创建一个客户端 Activity ,使用以下代码通过 TCP 将数据发送到服务器的端口 4000:

public class Client extends Activity {

private Socket socket;

private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

new Thread(new ClientThread()).start();
}

public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

class ClientThread implements Runnable {

@Override
public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}

}
}

然后将他们的其他 fragment 用于服务器 Activity ,我可以在该端口上使用 TCP 捕获消息:

public class Server extends Activity {

private ServerSocket serverSocket;

Handler updateConversationHandler;

Thread serverThread = null;

private TextView text;

public static final int SERVERPORT = 6000;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text = (TextView) findViewById(R.id.text2);

updateConversationHandler = new Handler();

this.serverThread = new Thread(new ServerThread());
this.serverThread.start();

}

@Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

class ServerThread implements Runnable {

public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {

try {

socket = serverSocket.accept();

CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();

} catch (IOException e) {
e.printStackTrace();
}
}
}
}

class CommunicationThread implements Runnable {

private Socket clientSocket;

private BufferedReader input;

public CommunicationThread(Socket clientSocket) {

this.clientSocket = clientSocket;

try {

this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {

while (!Thread.currentThread().isInterrupted()) {

try {

String read = input.readLine();

updateConversationHandler.post(new updateUIThread(read));

} catch (IOException e) {
e.printStackTrace();
}
}
}

}

class updateUIThread implements Runnable {
private String msg;

public updateUIThread(String str) {
this.msg = str;
}

@Override
public void run() {
text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
}
}
}

我的问题是我怎样才能让这 2 个可以来回通信?

Android -> Server(4000) -> Android(4001)?

换句话说,我如何让我的应用程序帮助设备同时充当客户端(向端口 4000 上的另一台设备发送数据)和服务器(在端口 4001 上监听数据)?

最佳答案

在服务器端将端口更改为 5000(与客户端相同)而不是 6000:

并且您需要更新以下类,因为您不想创建新套接字,您应该使用已经创建的套接字(在客户端部分)。注意:作为 CommunicationThread 参数给出的套接字是您应该已经创建的套接字(客户端部分)。

class ServerThread implements Runnable {

public void run() {
while (!Thread.currentThread().isInterrupted()) {
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();

}
}
}

关于java - Android 2-Way TCP/IP ServerSocket/套接字通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26445243/

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