万维网和互联网改变了我们的生活和相互交流的方式,以及我们进行科学、工程和商业的方式。现代生活完全是围绕着互联网来驱动或者以互联网为中心的。企业不断寻求新的方式来以引入创新的新方式生产和交流各种服务。在本文中,我们将讨论 Java 中的套接字编程。
套接字为 OSI 模型传输层的网络编程提供了接口。使用套接字的网络通信在互联网上随处可见。除此之外,用 Java 编写的套接字程序可以与用非 Java 语言(如 C、C++、Python 等)编写的套接字程序进行通信
广告 该类别中的热门课程 编程语言 - 专业化 | 54 课程系列 | 4 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Socket 类方法
Socket 类方法是在 Java 中找到的。套接字绑定了一个端口号,以便 TCP 识别要发送数据的端口号。 Java 提供了一组类,其中一个是 java.net。这用于网络应用的快速开发。 java.net 包中存在的关键类、接口和异常简化了创建客户端和服务器程序所涉及的复杂性:
课程有:
- 内容处理程序
- 数据报包
- 数据报套接字
- 数据报套接字 Imp 1
- HTTP URL 连接
- 我的网址
- 多播套接字
- 服务器套接字
- 插座
- 套接字小鬼 1
- 网址
- URL 连接
- URL 编码器
- URL 流处理程序
接口有:
- 内容处理工厂
- 文件名映射
- Socket Impl 工厂
- URL 流处理工厂
例外情况是:
- 绑定异常
- 连接异常
- 格式错误的 URL 异常
- 没有到主机的路由异常
- 协议异常
- 套接字异常
- 未知主机异常
- 未知服务异常
TCP/IP 套接字编程
java.net 包中使用了两个类,用于创建程序。
- 服务器套接字
- 插座
服务器程序通过输入和输出流进行通信。如果有连接请求,那么就会有一个新的套接字发挥作用,这里是与它建立的连接。
方法1 – 创建服务器Socket程序
用 Java 创建服务器套接字程序有多个步骤。
创建服务器套接字程序的简单步骤如下:
第 1 步:Socket 服务器已打开。
Server Socket General= new Server Socket(PO)
这里 PO 是端口号。
这里端口号被分配给服务器网络,通过该网络它将使用输入/输出流进行通信。
第2步:有客户请求,我们必须耐心等待。
Socket General= server. accept()
这是服务器。 accept()等待客户端,这里socket的名字是Client。
第 3 步:创建 I/O 流以便建立连接
数据输入流
is = new Data Input Stream(client. Get Input Stream());
数据输出流
os = new Data Output Stream(client. get Output Stream());
输入流和输出流被分配了它们的Get Input Stream(),并分别调用它们。
第 4 步:已创建与客户的联系。
收到来自客户的信息:
string hello = br. Read Line();
发送给客户:
br. Write Bytes("How are you\n");
以下代码与接收请求并向客户端发送请求的客户端进行通信。
第 5 步:最后,Socket 退出。
最后使用close socket函数来关闭并结束socket编程。
服务器套接字的简单示例如下所示:
一个用于连接服务器的简单程序。
代码:
import java.net.*; import java.io.*; public class SimpleMachine { public static void main(String args[]) throws IOException { // On port 1362 server port is registered ServerSocket soc = new ServerSocket(1362); Socket soc1=soc.accept(); // Link is accepted after waiting // Linked with the socket there should be a connection OutputStream s1out = soc1.getOutputStream(); DataOutputStream dosH = new DataOutputStream (s1out); // A string command is sent dosH.writeUTF("Hello how are you"); // The connection can be closed but the server socket cannot. dosH.close(); s1out.close(); soc1.close(); } }
方法2 – 创建一个简单的服务器套接字程序
现在我们将看到一个用 Java 编写的简单客户端程序。
用Java创建一个简单的客户端程序的步骤如下所示:
第 1 步:创建 Socket 对象。
Socket client= new Socket(server, port_id)
服务器和Port ID已连接;即服务器连接到该Port ID。
第 2 步:创建输入/输出流。
is = new Data Input Stream(client.getInputStream());
os = new Data Output Stream(client.getOutputStream());
输入流是,输出流os用于与客户端通信。
Step 3: Input/Output streams are made for talking to the Client.
Data is read from the server:
string hello = br. readLine();
Send data to the server:
br.writeBytes("How are you\n")
This step communicates with the server. The input stream and output stream both communicates with the server.
Step 4: Close the Socket when done.
This function will close the client when it is finally done.
An example of a simple server socket program is shown below.
A simple program for the client.
Code:
import java.net.*; import java.io.*; public class SimpleMachineClient { public static void main(String args[]) throws IOException { // At port 1325, connection to the server is opened Socket s1H = new Socket("host",1325); // Read the input stream by getting an input file from the socket Input Stream s1I = s1. getInputStream(); Data Input Stream disH = new Data Input Stream(s1In); String str = new String (disH.readUTF()); System.out.println(str); // After it is done, we can close the connection. disH.close(); s1I.close(); s1H.close(); } }
Conclusion
Socket programming is beneficial in Java and in any other programming language. A program written in Java can connect with a program written in C or C++. In other words, the language of the socket program doesn’t matter when there has to be a connection between the two. In this article, we have basically seen the Simple Server and the Simple Client example where there is a connection between the server socket and in the other, there is a connection between the server socket. We have used TCP/IP programming for the same. However, there are a lot of programming techniques like UDP programming techniques and URL programming techniques. We have not seen examples of such in this article. We have stressed on TCP/IP programming technique.
以上是Java 中的套接字编程的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器