Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java连接套接字时有时会延迟2分钟?_Java_Linux_Sockets_Kotlin - Fatal编程技术网

Java连接套接字时有时会延迟2分钟?

Java连接套接字时有时会延迟2分钟?,java,linux,sockets,kotlin,Java,Linux,Sockets,Kotlin,我对下面的谜团感到困惑。在测试我的应用程序时,我经常(可能有25%的时间)在连接SSL套接字时得到两分钟的延迟。我使用的是Java8,Ubuntu 16.04。当我使用wireshark时,我可以看到没有任何东西通过网络发送到目标服务器。可能还有其他网络流量,如DNS解析。延迟始终为2分钟,不确定度不超过几秒钟。挂起前的最后一条日志消息是: DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Connecting socket

我对下面的谜团感到困惑。在测试我的应用程序时,我经常(可能有25%的时间)在连接SSL套接字时得到两分钟的延迟。我使用的是Java8,Ubuntu 16.04。当我使用wireshark时,我可以看到没有任何东西通过网络发送到目标服务器。可能还有其他网络流量,如DNS解析。延迟始终为2分钟,不确定度不超过几秒钟。挂起前的最后一条日志消息是:

DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Connecting socket to redacted:443 with timeout 0
暂停调试器将验证它是否在类PlainSocketImpl中的套接字连接代码中被阻止:

native void socketConnect(InetAddress address, int port, int timeout)
我很确定我已经注意到,如果这种挂起发生在其他应用程序上,我想它只会影响我的Java程序。我的(Kotlin)代码如下:

import org.apache.http.client.methods.HttpPost
import org.apache.http.config.RegistryBuilder
import org.apache.http.conn.socket.ConnectionSocketFactory
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.util.EntityUtils
import java.security.SecureRandom
import javax.net.ssl.*

fun main(args: Array<String>) {
  val client = SSLClient()

  val auth = AuthRequest()
  val ares = auth.post(client)
  val token = ares.value
  println(token)
}

class SSLClient {
    val client : CloseableHttpClient

    init {
        val config = Config
        config.loadCerts()
        val ctx = SSLContext.getInstance("TLSv1.2")
        val kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
        kmf.init(config.clientSSLCerts, config.keyPassword.toCharArray())
        val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
        tmf.init(config.serverSSLCerts)
        ctx.init(kmf.keyManagers, tmf.trustManagers, SecureRandom())
        val sslsf = SSLConnectionSocketFactory(ctx)

        val r = RegistryBuilder.create<ConnectionSocketFactory>()
                .register("https", sslsf)
                .build()
        val cm = PoolingHttpClientConnectionManager(r)
        client = HttpClients.custom()
                .setConnectionManager(cm)
                .build()
    }

    fun post(endpoint : String) : HttpPost {
        val post = HttpPost(endpoint)
        post.addHeader("Content-Type", "application/xml")
        return post
    }

    fun execute(post : HttpPost, xmlBody : String) : String {
        println(xmlBody)
        post.entity = StringEntity(xmlBody, Charsets.UTF_8)
        client.execute(post).use { response ->
            response.allHeaders.forEach { println(it.toString()) }
            println(response.statusLine.toString())
            if (response.statusLine.statusCode !in 200..299) {
                throw Exception(response.statusLine.toString())
            }
            val entity = response.entity
            if (entity == null || entity.contentLength == 0L) {
                throw Exception("No body content in HTTP response")
            }
            val result = EntityUtils.toString(entity)
            println(result)
            return result
        }
    }
}
import org.apache.http.client.methods.HttpPost
导入org.apache.http.config.RegistryBuilder
导入org.apache.http.conn.socket.ConnectionSocketFactory
导入org.apache.http.conn.ssl.SSLConnectionSocketFactory
导入org.apache.http.entity.StringEntity
导入org.apache.http.impl.client.CloseableHttpClient
导入org.apache.http.impl.client.HttpClients
导入org.apache.http.impl.conn.poolighttpclientconnectionmanager
导入org.apache.http.util.EntityUtils
导入java.security.SecureRandom
导入javax.net.ssl*
趣味主线(args:Array){
val client=SSLClient()
val auth=AuthRequest()
val ares=auth.post(客户端)
val令牌=战神值
println(令牌)
}
类SSLClient{
val客户端:CloseableHttpClient
初始化{
val config=config
config.loadCerts()
val ctx=SSLContext.getInstance(“TLSv1.2”)
val kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
init(config.clientSSLCerts,config.keyPassword.toCharArray())
val tmf=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
init(config.serverSSLCerts)
init(kmf.keyManagers、tmf.trustManagers、SecureRandom())
val sslsf=SSLConnectionSocketFactory(ctx)
val r=RegistryBuilder.create()
.注册(“https”,sslsf)
.build()
val cm=PoolghttpClientConnectionManager(r)
client=HttpClients.custom()
.setConnectionManager(cm)
.build()
}
有趣的帖子(端点:字符串):HttpPost{
val post=HttpPost(端点)
post.addHeader(“内容类型”、“应用程序/xml”)
回程站
}
乐趣执行(post:HttpPost,xmlBody:String):String{
println(xmlBody)
post.entity=StringEntity(xmlBody,Charsets.UTF_8)
client.execute(post).使用{response->
response.allHeaders.forEach{println(it.toString())}
println(response.statusLine.toString())
if(response.statusLine.statusCode!在200..299中){
抛出异常(response.statusLine.toString())
}
val entity=response.entity
if(entity==null | | entity.contentLength==0L){
抛出异常(“HTTP响应中没有正文内容”)
}
val结果=EntityUtils.toString(实体)
println(结果)
返回结果
}
}
}

我要提供的最后一条线索是,在调试器中暂停的时间不计入两分钟。我可以在暂停、取消暂停的情况下等待五分钟,但仍然需要整整两分钟。这说明它不是在等待外部进程完成,因为外部进程会在暂停的时间内取得进展。

您是否尝试过使用数据包嗅探器(如Wireshark)查看连接?我确实这样做了,并在第一段中逐字提及了结果。这只是我在WebStart中遇到的类似延迟的一个粗略猜测签名:如果SSL证书已被吊销,Java可能会尝试联系CA。只需扫描它,并在它等待时获得线程转储。您还应该跟踪所有网络和/或文件系统活动,而不仅仅是一个连接。默认情况下,DNS解析超时仅为5s。您是否尝试过使用数据包嗅探器(如Wireshark)查看连接?我确实这样做了,并在第一段中逐字提及了结果。这只是我在WebStart中遇到的类似延迟的一个粗略猜测签名:如果SSL证书已被吊销,Java可能会尝试联系CA。只需扫描它,并在它等待时获得线程转储。您还应该跟踪所有网络和/或文件系统活动,而不仅仅是一个连接。默认情况下,DNS解析超时仅为5s。