首页  >  文章  >  Java  >  Java 中的套接字编程

Java 中的套接字编程

WBOY
WBOY原创
2024-08-30 16:27:37897浏览

万维网和互联网改变了我们的生活和相互交流的方式,以及我们进行科学、工程和商业的方式。现代生活完全是围绕着互联网来驱动或者以互联网为中心的。企业不断寻求新的方式来以引入创新的新方式生产和交流各种服务。在本文中,我们将讨论 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 包中使用了两个类,用于创建程序。

  • 服务器套接字
  • 插座

服务器程序通过输入和输出流进行通信。如果有连接请求,那么就会有一个新的套接字发挥作用,这里是与它建立的连接。

Java 中的套接字编程

方法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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn