gpt4 book ai didi

java - 套接字超时问题 : android client reading data from pc server

转载 作者:行者123 更新时间:2023-12-01 04:40:57 25 4
gpt4 key购买 nike

我对 android、java 和套接字编程很陌生,所以我当然正在尝试合并这三者!

我正在创建一个基于桌面的java服务器,它将简单地发送一个短字符串到android应用程序(客户端)。

我的 Android 应用程序运行良好,它会将字符串发送到服务器,读取这些字符串没有任何问题。

我的服务器正在运行,它接收字符串没有任何问题。

但是,每当我尝试读取从服务器返回的字符串时,应用程序(客户端)就会出现套接字超时。我使用相同的代码,所以我无法理解这个问题。

无论如何,这是代码:

//SERVER//

import java.io.*;
import java.net.*;
import java.util.*;

public class GpsServer {

ServerSocket serversocket = null;
Socket socket = null;

public GpsServer()
{
try
{
serversocket = new ServerSocket(8189);
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out.println("Timeout while attempting to establish socket connection.");
} catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}

public void refreshServer() {

try
{
socket = serversocket.accept();

InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);

System.out.println("socket read successful");
printwriter.println("Send Bye to disconnect.");

String lineread = "";
boolean done = false;
while (((lineread = bufferedreader.readLine()) != null) && (!done)){
System.out.println("Received from Client: " + lineread);
printwriter.println("You sent: " + lineread);
if (lineread.compareToIgnoreCase("Bye") == 0) done = true;
}

System.out.println("Closing connection");

socket.close();
bufferedreader.close();
inputstreamreader.close();
printwriter.close();
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out
.println("Timeout while attempting to establish socket connection.");
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}

public void nullify() {
try
{
socket.close();
serversocket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}

还有客户...

//CLIENT

import java.io.*;
import java.net.*;
import java.util.*;

public class GpsClient {

public String lastMessage;
Socket socket = null;

String serverurl;
int serverport;

public GpsClient() {
lastMessage = "";

serverport = 8189;
serverurl = "192.168.10.4";

try
{
socket = new Socket(serverurl, serverport);
socket.setSoTimeout(10000);
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out.println("Timeout while attempting to establish socket connection.");
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}

public void retrieveNew() {

try {
socket = new Socket(serverurl, serverport);
socket.setSoTimeout(10000);

lastMessage = "connected!";

InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
printwriter.println("Request");

lastMessage = "Request sent";

// Get error when I uncomment this block, i.e. try to read the response from the server
// "Timeout while attempting to establish socket connection."

// String lineread = "";
// while ((lineread = bufferedreader.readLine()) != null) {
// System.out.println("Received from Server: " + lineread);
// lastMessage = "Received from Server: " + lineread;
// }

lastMessage = "closing connection!";
bufferedreader.close();
inputstreamreader.close();
printwriter.close();
socket.close();

}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out.println("Timeout while attempting to establish socket connection.");
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
finally
{
try
{
socket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}

public void nullify() {
try
{
PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
printwriter.println("Bye");
printwriter.close();
socket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}

提前致谢!

乔什

最佳答案

除了我评论中提到的所有问题之外,您正在客户端中创建两个套接字,而您的服务器仅被写入处理一个连接。因此,它写入第一个并尝试从中读取,同时您的客户端正在写入第二个并尝试从中读取。

当你解决这个问题时,你就会陷入僵局,因为双方都在尝试互相读取。

此外,您不应该通过网络使用 PrintWriterPrintStream,因为它们会吞掉您需要了解的异常。使用 BufferedWriter。

关于java - 套接字超时问题 : android client reading data from pc server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555299/

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