Java 教程

Java 控制语句

面向对象编程

Java 内置类

Java 文件处理

Java 错误和异常

Java 多线程

Java 同步

Java 网络

Java 集合

Java 接口

Java 数据结构

Java 集合算法

高级 Java

Java 杂项

Java API 和框架

Java 类参考

Java 有用资源

Java - Socket shutdownOutput() 方法



描述

Java Socket shutdownOutput() 用于禁用此套接字的输出流。对于 TCP 套接字,任何先前写入的数据将在 TCP 的正常连接终止序列后发送。如果在套接字上调用 shutdownOutput() 后写入套接字输出流,则该流将抛出 IOException。

声明

以下是 java.net.Socket.shutdownOutput() 方法的声明。

public void shutdownOutput() throws IOException

参数

返回值

异常

IOException − 如果在关闭此套接字时发生 I/O 错误。

示例

以下示例演示了如何使用 Java Socket shutdownOutput() 方法来禁用此套接字的输出流。第一步,我们创建了一个 GreetingClient 类,其中我们使用命令行参数读取服务器名称和端口。使用提供的服务器名称和端口,创建一个作为客户端的 Socket 实例。使用 client.getInputStream(),我们读取服务器发送的内容。然后将 InputStream 实例转换为 DataInputStream,并使用 DataInputStream.readUTF() 方法打印。由于我们在 writeUTF() 方法调用之前调用了 shutdownOutput(),因此此代码引发异常,如程序输出所示。

以下 GreetingClient 程序是客户端应用程序的示例,该应用程序使用 Socket 类在命令行参数指定的端口号上侦听服务器输出 -

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class GreetingClient {

   public static void main(String [] args) {
      String serverName = args[0];
      int port = Integer.parseInt(args[1]);
      try {
         System.out.println("Connecting to " + serverName + " on port " + port);
         Socket client = new Socket(serverName, port);
         
         System.out.println("Just connected to " + client.getRemoteSocketAddress());
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);
         
         out.writeUTF("Hello from " + client.getLocalSocketAddress());
         InputStream inFromServer = client.getInputStream();
         DataInputStream in = new DataInputStream(inFromServer);
         
         System.out.println("Server says " + in.readUTF());
         client.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

以下 GreetingServer 程序是服务器应用程序的示例,该应用程序使用 Socket 类在命令行参数指定的端口号上侦听客户端 -

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class GreetingServer extends Thread {
   private ServerSocket serverSocket;
   
   public GreetingServer(int port) throws IOException {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
   }

   public void run() {
      while(true) {
         try {
            System.out.println("Waiting for client on port " + 
               serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            
            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStream());
            
            System.out.println(in.readUTF());
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            server.shutdownOutput();
            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
               + "\nGoodbye!");
            server.close();
            
         } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
         } catch (IOException e) {
            e.printStackTrace();
            break;
         }
      }
   }
   
   public static void main(String [] args) {
      int port = Integer.parseInt(args[0]);
      try {
         Thread t = new GreetingServer(port);
         t.start();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

编译客户端和服务器,然后按如下方式启动服务器 -

Waiting for client on port 6066...
Just connected to /127.0.0.1:60982
Hello from /127.0.0.1:60982
java.net.SocketException: Cannot send after socket shutdown: socket write error
	at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
	at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
	at java.base/java.io.DataOutputStream.write(DataOutputStream.java:107)
	at java.base/java.io.DataOutputStream.writeUTF(DataOutputStream.java:401)
	at java.base/java.io.DataOutputStream.writeUTF(DataOutputStream.java:323)
	at com.tutorialspoint.GreetingServer.run(GreetingServer.java:31)

按如下方式检查客户端程序 -

输出

Connecting to localhost on port 6066
Just connected to localhost/127.0.0.1:6066
java.io.EOFException
	at java.base/java.io.DataInputStream.readUnsignedShort(DataInputStream.java:345)
	at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:594)
	at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:569)
	at com.tutorialspoint.GreetingClient.main(GreetingClient.java:28)
	at com.tutorialspoint.GreetingClient.main(GreetingClient.java:28)
java-socket.htm
广告