📜  Java的.net.SocketException在Java中与实例

📅  最后修改于: 2022-05-13 01:55:15.097000             🧑  作者: Mango

Java的.net.SocketException在Java中与实例

SocketExceptionIOException的子类,所以它是一个检查异常。这是在尝试打开或访问套接字时发出问题的最常见异常。此错误的完整异常层次结构是:

java.lang.Object
     java.lang.Throwable
         java.lang.Exception
             java.io.IOException
                 java.net.SocketException

您可能已经知道,强烈建议使用最具体的套接字异常类,以便更准确地指出问题。还值得注意的是,SocketException 通常带有一条错误消息,该消息非常详细地说明了导致异常的情况。

Implemented Interfaces: Serializable
Direct Known Subclasses: BindException, ConnectException, NoRouteToHostException, PortUnreachableException

什么是套接字编程?

它是一种编程概念,它利用套接字建立连接并使多个程序能够使用网络相互交互。套接字提供了使用网络协议栈建立通信的接口,并使程序能够通过网络共享消息。套接字是网络通信中的端点。套接字服务器通常是可以接受套接字连接请求的多线程服务器。套接字客户端是发起套接字通信请求的程序/进程。



Java.net.SocketException:连接重置

当客户端在通过套接字返回响应之前关闭套接字连接时,此SocketException发生在服务器端。例如,在检索到响应之前退出浏览器。连接重置只是意味着收到了 TCP RST。 TCP RST 数据包是远程端告诉您发送前一个 TCP 数据包的连接未被识别,可能连接已关闭,可能端口未打开,诸如此类。重置数据包只是一个没有有效载荷并且在 TCP 标头标志中设置了 RST 位的数据包。

现在就实现而言,很明显我们需要两个程序,一个处理客户端,另一个处理服务器。它们如下:

示例 1:服务器端

Java
// Java Program to Illustrate SocketException
// Server Side App
 
// Importing required classes
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
 
// Main class
public class SimpleServerApp {
 
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
 
        new Thread(new SimpleServer()).start();
    }
 
    static class SimpleServer implements Runnable {
 
        // run() method for thread
        @Override public void run()
        {
 
            ServerSocket serverSocket = null;
 
            // Try block to check for exceptions
            try {
 
                serverSocket = new ServerSocket(3333);
                serverSocket.setSoTimeout(0);
 
                // Till condition holds true
                while (true) {
 
                    try {
                        Socket clientSocket
                            = serverSocket.accept();
 
                        // Creating an object of
                        // BufferedReader class
                        BufferedReader inputReader
                            = new BufferedReader(
                                new InputStreamReader(
                                    clientSocket
                                        .getInputStream()));
 
                        System.out.println(
                            "Client said :"
                            + inputReader.readLine());
                    }
 
                    // Handlig the exception
                    catch (SocketTimeoutException e) {
 
                        // Print the exception along with
                        // line number
                        e.printStackTrace();
                    }
                }
            }
 
            // Catch block to handle the exceptions
            catch (IOException e1) {
 
                // Display the line where exception occurs
                e1.printStackTrace();
            }
 
            finally {
 
                try {
                    if (serverSocket != null) {
                        serverSocket.close();
                    }
                }
                catch (IOException e) {
 
                    e.printStackTrace();
                }
            }
        }
    }
}


Java
// Java Program to Illustrate SocketException
// Client Side App
 
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
 
// Class 1
// Main class
public class SimpleClientApp {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling inside main()
        new Thread(new SimpleClient()).start();
    }
 
    // Class 2
    // Helper class
    static class SimpleClient implements Runnable {
 
        // run() method for the thread
        @Override public void run()
        {
 
            // Initially assign null to our socket to be
            // used
            Socket socket = null;
 
            // Try block to e=check for exceptions
            try {
 
                socket = new Socket("localhost", 3333);
 
                // Creating an object of PrintWriter class
                PrintWriter outWriter = new PrintWriter(
                    socket.getOutputStream(), true);
 
                // Display message
                System.out.println("Wait");
 
                // making thread to sleep for 1500
                // nanoseconds
                Thread.sleep(15000);
 
                // Display message
                outWriter.println("Hello Mr. Server!");
            }
            // Catch block to handle the exceptions
 
            // Catch block 1
            catch (SocketException e) {
 
                // Display the line number where exception
                // occurred using printStackTrace() method
                e.printStackTrace();
            }
 
            // Catch block 2
            catch (InterruptedException e) {
                e.printStackTrace();
            }
 
            // Catch block 3
            catch (UnknownHostException e) {
                e.printStackTrace();
            }
 
            // Catch block 4
            catch (IOException e) {
                e.printStackTrace();
            }
 
            finally {
 
                try {
 
                    // If socket goes NULL
                    if (socket != null)
 
                        // Close the socket
                        socket.close();
                }
                catch (IOException e) {
 
                    e.printStackTrace();
                }
            }
        }
    }
}



示例 2:客户端



Java

// Java Program to Illustrate SocketException
// Client Side App
 
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
 
// Class 1
// Main class
public class SimpleClientApp {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling inside main()
        new Thread(new SimpleClient()).start();
    }
 
    // Class 2
    // Helper class
    static class SimpleClient implements Runnable {
 
        // run() method for the thread
        @Override public void run()
        {
 
            // Initially assign null to our socket to be
            // used
            Socket socket = null;
 
            // Try block to e=check for exceptions
            try {
 
                socket = new Socket("localhost", 3333);
 
                // Creating an object of PrintWriter class
                PrintWriter outWriter = new PrintWriter(
                    socket.getOutputStream(), true);
 
                // Display message
                System.out.println("Wait");
 
                // making thread to sleep for 1500
                // nanoseconds
                Thread.sleep(15000);
 
                // Display message
                outWriter.println("Hello Mr. Server!");
            }
            // Catch block to handle the exceptions
 
            // Catch block 1
            catch (SocketException e) {
 
                // Display the line number where exception
                // occurred using printStackTrace() method
                e.printStackTrace();
            }
 
            // Catch block 2
            catch (InterruptedException e) {
                e.printStackTrace();
            }
 
            // Catch block 3
            catch (UnknownHostException e) {
                e.printStackTrace();
            }
 
            // Catch block 4
            catch (IOException e) {
                e.printStackTrace();
            }
 
            finally {
 
                try {
 
                    // If socket goes NULL
                    if (socket != null)
 
                        // Close the socket
                        socket.close();
                }
                catch (IOException e) {
 
                    e.printStackTrace();
                }
            }
        }
    }
}


输出:

现在为了摆脱Java.net.SocketException 要获得正确的输出,则可以通过好像您是客户端并在连接到服务器端应用程序时收到此错误来感知它,然后按如下方式附加以下更改:

  1. 首先,通过在服务器运行的主机端口上执行 telnet 来检查服务器是否正在运行。
  2. 检查服务器是否重新启动
  3. 检查服务器是否故障转移到不同的主机
  4. 记录错误
  5. 向服务器团队报告问题