【问题标题】:Connection Timed out while connecting two systems on same network via sockets in java通过java中的套接字连接同一网络上的两个系统时连接超时
【发布时间】:2013-12-01 15:03:57
【问题描述】:

我的代码有 2 个类,SocketDemo 和 ServerSocketDemo,当客户端 (SocketDemo) 尝试连接到服务器 (ServerSocketDemo) 时,它会等待几秒钟然后抛出

java.net.ConnectionException : 连接超时

在那个特定时间,服务器显示连接已建立,但客户端现在已重置连接并抛出异常

首先告诉我,是否可以通过套接字在同一连接上连接两个不同的系统?

请考虑此代码片段并提供帮助!

客户端代码

import java.net.*;
import java.io.*;
class SocketDemo
{
    public static void main(String...arga) throws Exception
    {
        Socket          s = null;
        PrintWriter    pw = null;
        BufferedReader br = null;
        System.out.println("Enter a number one digit");
        int i=(System.in.read()-48); // will read only one character
        System.out.println("Input number is "+i);
        try
        {
            s  = new Socket("192.168.1.5",40000);
            System.out.println(s);
            pw = new PrintWriter(s.getOutputStream());
            System.out.println(pw);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            System.out.println(br);
            System.out.println("Connection established, streams created");
        }
        catch(Exception e)
        {
            System.out.println("Exception in Client "+e);
        }
        pw.println(i);
        pw.flush();
        System.out.println("Data sent to server");
        String str = br.readLine();
        System.out.println("The square of "+i+" is "+str);
    }
}

服务器代码:

import java.io.*;
import java.net.*;
class ServerSocketDemo
{
    public static void main(String...args)
    {
        ServerSocket ss=null;
        PrintWriter    pw = null;
        BufferedReader br = null;
        int i=0;
        try
        {
            ss = new ServerSocket(40000);
        }
        catch(Exception e)
        {
            System.out.println("Exception in Server while creating connection"+e);
            e.printStackTrace();
        }
        System.out.print("Server is ready");
        while (true)
        {
            System.out.println ("  Waiting for connection....");
            Socket s=null;
            try
            {
                System.out.println("connection "+s+ "\n printwriter "+pw+"\n bufferedreader "+br);
                s = ss.accept();
                System.out.println("Connection established with client");
                pw = new PrintWriter(s.getOutputStream());
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                System.out.println("connection "+s+ "\n printwriter "+pw+"\n bufferedreader "+br);
                i = new Integer(br.readLine());
                System.out.println("i is "+i);
            }
            catch(Exception e)
            {
                System.out.println("Exception in Server "+e);
                e.printStackTrace();
            }
            System.out.println("Connection established with "+s);
            i*=i;
            pw.println(i);
            try
            {
                pw.close();
                br.close();
            }
            catch(Exception e)
            {
                System.out.println("Exception while closing streams");
            }
        }
    }
}

【问题讨论】:

    标签: java connectionexception


    【解决方案1】:

    我可以毫无问题地使用您的示例代码。可能是某些本地防火墙规则阻止您的客户端完成与服务器的连接。尝试在同一主机上运行您的客户端和服务器,在客户端连接中使用“localhost”或“127.0.0.1”。

    请参阅Why would a "java.net.ConnectException: Connection timed out" exception occur when URL is up? 中的最佳答案以获取更多信息。

    另外,我注意到您没有为连接或读取代码设置套接字超时。由于您没有在客户端套接字超时中设置超时,因此默认超时为零,这是永远的,或者更可能是您的操作系统默认套接字超时。一般来说,尤其是在生产代码中,不为连接或读取设置套接字超时是个坏主意,因为这会导致资源消耗问题,从而备份整个系统。

    尝试使用连接和读取超时设置客户端套接字,如下所示:

            //use a SocketAddress so you can set connect timeouts
            InetSocketAddress sockAddress = new InetSocketAddress("127.0.0.1",40000);
            s  = new Socket();
            //set connect timeout to one minute
            s.connect(sockAddress, 60000);
            //set read timeout to one minute
            s.setSoTimeout(60000);            
            System.out.println(s);
            pw = new PrintWriter(s.getOutputStream());
            ...
    

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 1970-01-01
      • 2015-05-17
      • 2013-05-23
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多