Java SocketTimeoutException

Java SocketTimeoutException,java,android,Java,Android,在我的应用程序中,我使用套接字与另一个设备通信。一直以来,我都有一个SocketTimeoutException。这是我在服役期间的通信 while ((bytesRead = inputStream.read(content)) != -1) { 这是一个日志: 11-09 08:44:05.029 5458-6647/pl.teminalmobile W/System.err: java.net.SocketTimeoutException: Read timed out 11-09

在我的应用程序中,我使用套接字与另一个设备通信。一直以来,我都有一个SocketTimeoutException。这是我在服役期间的通信

  while ((bytesRead = inputStream.read(content)) != -1) {
这是一个日志:

11-09 08:44:05.029 5458-6647/pl.teminalmobile W/System.err: java.net.SocketTimeoutException: Read timed out
11-09 08:44:05.030 5458-6647/pl.teminalmobile W/System.err:     at java.net.SocketInputStream.socketRead0(Native Method)
11-09 08:44:05.031 5458-6647/pl.teminalmobile W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:151)
11-09 08:44:05.031 5458-6647/pl.teminalmobile W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:120)
11-09 08:44:05.031 5458-6647/pl.teminalmobile W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:106)
11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err:     at pl.teminalmobile.Service.Service22.start1(Service22.java:256)
11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err:     at pl.teminalmobile.Service.Service22.access$000(Service22.java:75)
11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err:     at pl.teminalmobile.Service.Service22$19.run(Service22.java:963)
11-09 08:44:05.033 5458-6647/pl.teminalmobile W/System.err:     at java.util.TimerThread.mainLoop(Timer.java:555)
11-09 08:44:05.033 5458-6647/pl.teminalmobile W/System.err:     at java.util.TimerThread.run(Timer.java:505)
  while ((bytesRead = inputStream.read(content)) != -1) {
此行显示SocketTimeOutException:

  while ((bytesRead = inputStream.read(content)) != -1) {

您的套接字超时意味着从其他设备获取响应所需的时间太长,并且您的请求在获得响应之前过期。要解决此问题,您需要提供手动套接字超时,如搜索如何提供您正在使用的库的套接字超时。如果使用socket.io,则需要添加

  while ((bytesRead = inputStream.read(content)) != -1) {
socket.timeout(12000);
这里12000是毫秒,或者如果您使用的是OkHttp客户端,则可以添加:

  while ((bytesRead = inputStream.read(content)) != -1) {
clientBuilder.connectTimeout(60, TimeUnit.SECONDS);
或者,无论您使用哪种套接字库,都有一种方法可以手动指定超时,因此只需键入创建套接字时使用的套接字名称并放置“.”之后,您就可以在搜索超时的过程中对可用方法提出建议,并使用该方法

  while ((bytesRead = inputStream.read(content)) != -1) {
以下是使用库演示的链接:

  while ((bytesRead = inputStream.read(content)) != -1) {

我也有同样的问题,但我用这些方法解决了它

  while ((bytesRead = inputStream.read(content)) != -1) {

将字符编码设置为UTF-8和字符集

虽然我不能完全确定,但我认为您没有正确使用Socket.setSortimeout。
  while ((bytesRead = inputStream.read(content)) != -1) {

对于Socket requestSocket,只能使用一次。在处理套接字内容之前,请尝试在代码的开头使用它,或者通过将代码传递给函数而不是三次调用函数,使代码模块化。根据我的个人经验,这使得使用Socket.setSoTimeout更容易

我向您展示您粘贴的代码

  while ((bytesRead = inputStream.read(content)) != -1) {
我发现您在117和125行设置了3000ms的超时时间,您需要将其最大更改为15秒

  while ((bytesRead = inputStream.read(content)) != -1) {
requestSocket.connect(new InetSocketAddress(a, 6666), 3000);
希望能有所帮助

  while ((bytesRead = inputStream.read(content)) != -1) {

快乐编码

快乐编码:我不使用OkHttp客户端发送和获取数据,我使用流,这是个好主意吗?我设置了一个timout requestSocket.setSoTimeout7000;但它不能正常工作。对于socket,我建议您使用socket.io库,我在几个项目中使用过它,使用起来非常好。您不必使用socket.io来面对问题。使用socket.io是一种经过精心设计和优化的方式。所以,兄弟,如果你很容易切换到socket.io,那就试试吧。你能告诉我你推荐给我的是哪个socket吗?我必须要做的是什么use@KrzysztofPokrywka兄弟,我更新了我的答案,并提供了一个链接,请遵循该链接,说明也在那里,你会很好地去做。我在许多项目中使用它,它的作品像一个魅力。为进一步的帮助,请发短信给我。若这篇文章对你们有帮助,别忘了把它标为答案。快乐编码:你为什么不提到行号?Service22.java:256 11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err:at pl.teminalmobile.Service.Service22.access$000Service22.java:75 11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err:at pl.teminalmobile.Service.Service22$19.runService22.java:963。那些线是什么?他们导致了暂停,不是吗?
  while ((bytesRead = inputStream.read(content)) != -1) {