java.net.ConnectException:连接超时:无进一步信息
今天我在不同的机器上测试服务器和客户端代码。 他们都在同一个Wi-fi网络上 我使用以下代码创建了客户机,并在许多线程中出现了此异常:java.net.ConnectException:连接超时:无进一步信息,java,nio,Java,Nio,今天我在不同的机器上测试服务器和客户端代码。 他们都在同一个Wi-fi网络上 我使用以下代码创建了客户机,并在许多线程中出现了此异常: java.net.ConnectException:连接超时:无进一步信息 在sun.nio.ch.socketchannel.checkConnect(本机方法) 位于sun.nio.ch.socketchannel.finishConnect(未知来源) 在SocketTest.connect(Client.java:188) 在SocketTest.run
java.net.ConnectException:连接超时:无进一步信息
在sun.nio.ch.socketchannel.checkConnect(本机方法)
位于sun.nio.ch.socketchannel.finishConnect(未知来源)
在SocketTest.connect(Client.java:188)
在SocketTest.run(Client.java:73)
在java.lang.Thread.run(未知源代码)
第73行是connect(键)
第188行是if(!(channel.finishConnect())
所以客户端线程无法连接,因为服务器没有回复?对吧?
问题)当我在同一台机器localhost上运行服务器和客户端时,不会出现此异常。原因可能是什么?(网络问题?)
此外,我还在publicvoidbind(SocketAddress端点,intbacklog)中使用Backlog队列参数作为2000。虽然确切的大小未知(大约200?),但我使用了一个较大的值,以便使用最大值。(对吗?或者Java将生成一个队列?)
这是否可能是一个原因:服务器将请求放入待办事项队列,直到有时间提供请求,超时可能发生在客户端
客户:
public class Client {
public static void main(String[] args) {
int n=100;
SocketTest [] st= new SocketTest[n];
for(int i=0;i<n;i++)
st[i]= new SocketTest("hi");
for(int i=0;i<n;i++)
{
if(i%50 == 0)
try{
Thread.sleep(5000);
}
catch(InterruptedException ie)
{
System.out.println(""+ie);
}
new Thread(st[i]).start();
}
}
}
class SocketTest implements Runnable {
private String message = "";
ByteBuffer readBuffer = ByteBuffer.allocate(1000);
private Selector selector;
private int i;
public static AtomicInteger cnt= new AtomicInteger(0);
public SocketTest(String message){
this.message = message;
}
@Override
public void run() {
SocketChannel channel;
try {
selector = Selector.open();
channel = SocketChannel.open();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_CONNECT);
channel.connect(new InetSocketAddress("192.168.1.10", 8511));
while (!Thread.currentThread().isInterrupted()){
selector.select();
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()){
SelectionKey key = keys.next();
keys.remove();
if (!key.isValid()) continue;
if (key.isConnectable()){
connect(key);
System.out.println("I am connected to the server");
}
if (key.isWritable()){
write(key);
}
if (key.isReadable()){
read(key);
}
}
}
}
catch(ClosedByInterruptException e)
{
// let go of thread
}
catch(CancelledKeyException e){
e.printStackTrace();
}
catch (IOException e1) {
System.out.println("IOE Occured|maybe Server died");
e1.printStackTrace();
} finally {
close();
}
}
private void close(){
try {
if(selector!=null)
selector.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void read (SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
readBuffer.clear();
int length;
try{
length = channel.read(readBuffer);
} catch (IOException e){
System.out.println("Reading problem, closing connection for : "+channel.getLocalAddress());
key.cancel();
channel.close();
return;
}
if (length == -1){
System.out.println("Nothing was read from server");
channel.close();
key.cancel();
return;
}
readBuffer.flip();
byte[] buff = new byte[1024];
readBuffer.get(buff, 0, length);
//length=buff.length;
String fromserver = new String(buff,0,length,"UTF-8");
length = fromserver.length();
System.out.println("Server said: "+fromserver);
key.interestOps(SelectionKey.OP_WRITE);
}
private void write(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
i++;
message = "location now "+i;
if(i==2)
{
cnt.addAndGet(1);
System.out.println("****"+cnt.get()+"****");
}
try{
Thread.sleep(5000);
}
catch(InterruptedException ie)
{
System.out.println(""+ie);
//Thread.currentThread().interrupt();
}
//assuming all goes in one shot
channel.write(ByteBuffer.wrap(message.getBytes()));
key.interestOps(SelectionKey.OP_READ/*|SelectionKey.OP_WRITE*/);
}
private void connect(SelectionKey key){
SocketChannel channel= (SocketChannel) key.channel();
try
{
if(!(channel.finishConnect())){
//System.out.println("* Here *");
return;
}
}
catch(ConnectException e){
System.out.println("Conect Exception");
e.printStackTrace();
try{channel.close();}
catch(IOException ie){ie.printStackTrace();key.cancel();return;}
return;
}
catch(IOException e)
{
System.out.println("BP 1"+e);
e.printStackTrace();
try{channel.close();}
catch(IOException ie){ie.printStackTrace();key.cancel();return;}
return;
}
//channel.configureBlocking(false);
//channel.register(selector, SelectionKey.OP_WRITE);
key.interestOps(SelectionKey.OP_WRITE);
}
}
公共类客户端{
公共静态void main(字符串[]args){
int n=100;
SocketTest[]st=新SocketTest[n];
对于(int i=0;i连接超时,因为服务器没有应答
当我在同一台本地主机上运行服务器和客户端时,不会出现此异常。原因可能是什么?(网络问题?)
为什么会出现这种情况?服务器在那里,客户端在那里,两者之间没有网络问题。这个问题没有意义
此外,我还在public void bind(SocketAddress endpoint,int Backlog)中使用Backlog队列参数作为2000。虽然确切大小未知(大约200?),但我使用了一个较大的值,以便使用最大值。对吗
对
或者Java将生成一个队列
我不知道这意味着什么。Java不使用backlog参数做任何事情。它直接进入TCP
这是否可能是一个原因:服务器将请求放入待办事项队列,直到有时间提供请求,超时可能发生在客户端
否。只有已完成的连接才会进入待办事项队列。可能重复的@wforums此问题与该问题不同。在该问题中,连接被拒绝是此处的问题连接超时。@cruxioneffux然而,当您发布与前一个问题完全相同、未更正的代码时,人们往往会将其视为一份副本,给你所有更正和评论的人很容易生气,想知道他们为什么这么麻烦,也想知道如果你只是想忽略所有告诉你的事情,为什么你会在这里发布。如果你费心修改代码,所有这些都可以避免。为什么你没有呢?@EJP对此表示抱歉。我有很多版本在所有执行相同的客户端代码中,粘贴了所有人(我正在使用的)。我做了您建议的大部分更改,代码现在粘贴在上面。请参阅。我读到:连接超时发生在以下情况:1.成功找到请求服务器的IP地址2.将连接建立数据包发送到IP地址3.目标地址故意忽略或不接收这些数据包,类似于con连接超时是指连接被拒绝,但在这种情况下,目标系统实际上是在发送回数据包,说“走开”没有可供您使用的服务。是否正确?所以这里的服务器没有收到请求?它没有回复。这就是我们所知道的。可能它没有收到请求:可能回复丢失。所以这不是在本地主机上发生的,而是在网络上发生的。所以这是一个网络问题。正确吗?现在原因可能是拥塞?路由器问题?