热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java中的java.net.ServerSocket类

java中的java.net.ServerSocket类原

java 中的 java.net.ServerSocket 类

原文:https://www . geesforgeks . org/Java-net-server socket-class-in-Java/

服务器套接字类用于提供客户机/服务器套接字连接的服务器端的独立于系统的实现。如果服务器套接字的构造函数无法侦听指定的端口(例如,该端口已被使用),它将引发异常。

所以 java.net.ServerSocket 类的应用被广泛使用,具体如下:


  1. 在 java.nio 通道中,serverSocket 类用于检索与该通道关联的 ServerSocket。

  2. 在 java.rmi.Server 中,ServerSocket 类用于在指定端口上创建服务器套接字(端口 0 表示匿名端口)。

  3. 在 javax.net,服务器套接字被广泛使用,以便:

    • 返回未绑定的服务器套接字。

    • 返回绑定到指定端口的服务器套接字。

    • 返回绑定到指定端口的服务器套接字,并使用指定的连接积压。

    • 返回绑定到指定端口的服务器套接字,具有指定的监听积压和本地 IP。



让我们来看一下这个类的方法,如下所示:

| 【 Method 】 | describe |
| --- | --- |
| accept | Listen for the connection to be established with this socket and accept it. |
| 绑定(SocketAddress 端点) | Bind the server socket to a specific address (IP address and port number). |
| -你好(套接字地址什么事,int backlog) | Bind the server socket to a specific address (IP address and port number). |
| 关闭() | This close socket |
| getChannel() | Returns the unique ServerSocketChannel object associated with this socket, if any. |
| getnetaddress() | Returns the local address of this server socket. |
| getLocalPort() | Returns the port number on which the socket is listening. |
| getLocalSocketAddress() | Returns the address of the endpoint to which this socket is bound, or null if it is not already bound. |
| getReceiveBufferSize() | Gets the value of the SO_RCVBUF option of this server socket, that is, the suggested buffer size to be used for sockets accepted from this server socket. |
| getReuseAddress() | Test whether SO_REUSEADDR is enabled. |
| getSoTimeout() | Retrieve settings for SO_TIMEOUT. |
| 植入插座 | A subclass of ServerSocket uses this method to override Accept () and return its own subclass [T0】 Socket. |
| isBound() | Returns the binding status of the server socket. |
| isClosed() | Returns the closed state of the ServerSocket. |
| 设置性能首选项(内部连接时间、内部延迟、内部带宽) | Set performance preferences for this server socket. |
| Set performance preferences for this server socket. | Sets the default recommended value of the SO_RCVBUF option for sockets accepted from this server socket. |
| setReuseAddress(boolean on) | Enable/disable the SO_REUSEADDR socket option. |
| setsxmlsocket factory(socket implfactory fac) | Set the server socket implementation factory for the application. |
| int time out(内部时间输出) | Enable/disable SO_TIMEOUT with the specified timeout in milliseconds. |
| toString() | Returns the implementation address and implementation port of this socket as a String. |

实施:

示例 1 服务器端

Java 语言(一种计算机语言,尤用于创建网站)


// Java Program to implement ServerSocket class
// Server Side
// Importing required libraries
import java.io.*;
import java.net.*;
// Main class
public class MyServer {
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
            // Creating an object of ServerSocket class
            // in the main() method  for socket connection
            ServerSocket ss = new ServerSocket(6666);
            // Establishing a connection
            Socket soc = ss.accept();
            // Invoking input stream via getInputStream()
            // method by creating DataInputStream class
            // object
            DataInputStream dis
                = new DataInputStream(s.getInputStream());
            String str = (String)dis.readUTF();
            // Display the string on the console
            System.out.println("message= " + str);
            // Lastly close the socket using standard close
            // method to release memory resources
            ss.close();
        }
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Display the exception on the console
            System.out.println(e);
        }
    }
}

输出:

示例 2 客户端

Java 语言(一种计算机语言,尤用于创建网站)


// Java Program to implement ServerSocket class
// Client - side
// Importing required libraries
import java.io.*;
import java.net.*;
// Main class
public class MyClient {
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check if exception occurs
        try {
            // Creating Socket class object and
            // initializing Socket
            Socket soc = new Socket("localhost", 6666);
            DataOutputStream d = new DataOutputStream(
                soc.getOutputStream());
            // Message to be displayed
            d.writeUTF("Hello GFG Readers!");
            // Flushing out internal buffers,
            // optimizing for better performance
            d.flush();
            // Closing the connections
            // Closing DataOutputStream
            d.close();
            // Closing socket
            soc.close();
        }
        // Catch block to handle exceptions
        catch (Exception e) {
            // Print the exception on the console
            System.out.println(e);
        }
    }
}

输出:


推荐阅读
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • Question该提问来源于开源项目:react-native-device-info/react-native-device-info ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文详细介绍了使用C#实现Word模版打印的方案。包括添加COM引用、新建Word操作类、开启Word进程、加载模版文件等步骤。通过该方案可以实现C#对Word文档的打印功能。 ... [详细]
author-avatar
一抺晚霞_769
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有