socket编程实验(每周分享三 进程与线程实验)

管理员 2024-11-25 13:26:55 0
每周分享(三)| 进程与线程实验

分享兴趣,传播快乐,增长见闻,留下美好,亲爱的您,这里是Learning Yard新学苑,今天小编为大家带来"进程线程与网络编程实验",欢迎您的访问。

Share interests, spread happiness, increase knowledge, and leave beauty. Dear you, this is the learning yard new school. Today, Xiaobian brings you "Process and thread experiment". Welcome to visit.

实验内容

编写程序,在linux下进行tcp/udp通信。

Programming, tcp/udp communication under Linux.

实验步骤

1.在服务端程序中建立tcp连接:

1) 调用函数 socket( )创建流式套接字,并获得套接字描述符;

2) 通过函数 bind( )将套接字与服务器地址绑定;

3) 通过 listen( )设定监听的端口数

4) 最后通过 accept( )来接收连接请求

5) 收到连接请求后,只用 read/write 或 recu/send 函数,通过从 accept 返回的客户端套接字与客户端进行通信。

6) 使用 close 函数关闭套接字,断开 TCP 连接

2.在客户端程序中建立tcp连接:

1) 调用函数 socket( )创建流式套接字,并获得套接字描述符;

2) 通过函数 connect( ),套接字向服务器地址发起连接请求。

3) 连接建立成功,使用 read()/.write()来接收和发送数据(也可用 send 和 recv),,通过之前的套接字与服务端进行通信。

4) 使用 close()函数关闭套接字,断开 TCP 连接。

1. establish a TCP connection in the server program:

1) Call the function socket () to create a streaming socket and obtain the socket descriptor;

2) Bind the socket to the server address through the function bind();

3) Set the number of listening ports through listen()

4) Finally, the connection request is received through accept()

5) After receiving the connection request, only read/write or recu/send functions are used to communicate with the client through the client socket returned from accept.

6) Use the close function to close the socket and disconnect the TCP connection

2. establish a TCP connection in the client program:

1) Call the function socket () to create a streaming socket and obtain the socket descriptor;

2) Through the function connect (), the socket sends a connection request to the server address.

3) The connection is established successfully. Use read() / Write() to receive and send data (also use send and recv), and communicate with the server through the previous socket.

4) Use the close() function to close the socket and disconnect the TCP connection.

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

3.在服务端程序中建立udp连接:

1) 调用函数 socket( )创建数据报套接字,并获得套接字描述符;

2) 通过函数 bind( )将套接字与服务器地址绑定;

3) 使用 recvfrom/sendto 等函数,直接接收消息或向某个地址发送消息,也可以像 TCP

连接一样使用 accept 函数等待 UDP 的“连接”请求。

4) 使用 close 函数关闭套接字,退出 UDP 通信。

4.在客户端程序中建立udp连接:

1) 调用函数 socket( )创建数据报套接字,并获得套接字描述符;

2) 使用 sendto 等函数,直接向某个地址发送消息。如果要通过 recvfrom 接受消息, 需要将套接字绑定在本地主机的网络地址上,同样也可以使用 connect 函数指UDP 通信的服务器。

3) 使用 close 函数关闭套接字,退出 UDP 通信

3. establish a UDP connection in the server program:

1) Call the function socket() to create a datagram socket and obtain the socket descriptor;

2) Bind the socket to the server address through the function bind();

3) Use recvfrom/sendto and other functions to directly receive messages or send messages to an address, or TCP

Use the accept function to wait for the "connect" request of UDP just like the connection.

4) Use the close function to close the socket and exit UDP communication.

4. establish a UDP connection in the client program:

1) Call the function socket() to create a datagram socket and obtain the socket descriptor;

2) Send a message directly to an address using functions such as SendTo. If you want to receive messages through recvfrom, you need to bind the socket to the network address of the local host. You can also use the connect function to refer to the server for UDP communication.

3) Use the close function to close the socket and exit UDP communication

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

实验结果

每周分享(三)| 进程与线程实验

每周分享(三)| 进程与线程实验

今天的分享就到这里了,如果您对今天的文章由独特的想法,欢迎给我们留言,让我们相约明天,祝您今天过得开心快乐!

That's all for today's sharing. If you have unique ideas about today's article, please leave us a message. Let's meet tomorrow. I wish you a happy day today!

参考资料:百度

翻译:百度翻译

本文由Learning Yard新学苑整理并发出,如有侵权请在后台留言。

相关文章