gpt4 book ai didi

android - android和windows通过socket通信

转载 作者:行者123 更新时间:2023-11-30 02:27:13 25 4
gpt4 key购买 nike

我想让我的 android 应用程序打开到我的 Windows 控制台应用程序的套接字,并且它们相互通信。套接字已打开并在 Windows 应用程序中发送和接收数据,但我的 Android 应用程序未收到 Windows 发送的应答。我在我的 android 中观察数据包,我看到数据包正在发送,但我不知道为什么我的应用程序没有收到它!

Windows 应用服务器类:

class Server
{
private TcpListener tcpListener;
private Thread listenThread;

public Server()
{
Console.WriteLine("\nStarting server...");

this.tcpListener = new TcpListener(IPAddress.Any, 1234);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}

private void ListenForClients()
{
Console.WriteLine("\nWaiting for clients to connect...");

this.tcpListener.Start();

while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();

//create a thread to handle communication with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}

private void HandleClientComm(object client)
{
Console.WriteLine("\nIncoming from client...");

TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();

byte[] message = new byte[4096];
int bytesRead;

try
{
while (true)
{
bytesRead = 0;

try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}

if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}

//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine("\nReceived: \n\n" + encoder.GetString(message, 0, bytesRead));

//By FMR
string response = "random responsive: " + new Random().Next(1000).ToString() + "\n";//"\r\n";
//writeData(clientStream, response);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);

// Send back a response.
clientStream.Write(msg, 0, msg.Length);
clientStream.Flush();

Console.WriteLine("\nResponed ..." + response);
}
}
catch (Exception ex)
{
Console.WriteLine("\nException while: " + ex.Message);
}
tcpClient.Close();
}
}

我的安卓线程:

Thread thread = new Thread(new Runnable() {

@Override
public void run() {
Socket socket = null;
ServerSocket serverSocket = null;
Boolean bRun = true;
try {
socket = new Socket(ip, port);

if(outputStream == null) {
outputStream = new DataOutputStream(socket.getOutputStream());
}


// become server
serverSocket = new ServerSocket(port);
Log.i(G.TAG, "before serverSocket.accept");
socket = serverSocket.accept();
Log.i(G.TAG, "response recieve: ");

inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (Exception e) {
try {
serverSocket.close();
} catch (IOException e1) {
Log.e(G.TAG, "serverSocket.close() e: " + e1.getMessage());
}
try {
socket.close();
} catch (IOException e1) {
Log.e(G.TAG, "socket.close() e: " + e1.getMessage());
}
}
Log.i(G.TAG, "after start recieve: ");
while (bRun) {
try {
Log.i(G.TAG, "while start: ");
String message = inputStream.readLine();
Log.i(G.TAG, "response message: " + message);
if (message != null) {
setListMessage(false, message);
}
}
catch (IOException e) {
bRun = false;
Log.e(G.TAG, "while bRun e: " + e.getMessage());
}
}
}
});
thread.start();

// in another function, my message is sent successfully from android and receive in windows

最佳答案

我发现问题了,这一行

socket = serverSocket.accept();

当我评论该行时出现问题,android应用程序收到了响应!

有人知道为什么吗?

关于android - android和windows通过socket通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783276/

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