Home > Backend Development > C++ > How do C++ functions implement data transmission in network programming?

How do C++ functions implement data transmission in network programming?

王林
Release: 2024-04-27 17:06:02
Original
1010 people have browsed it

C's network data transmission functions include recv() and send(), which are used to receive and send data on the server side. The following steps demonstrate the process of using recv() and send() to create an echo server: 1. Create a socket; 2. Set the server address information; 3. Bind the socket to the server address; 4. Listen for connections; 5 . Accept the connection, receive data and send it back to the client in a loop; 6. Close the connection and socket.

C++ 函数在网络编程中如何实现数据传输?

Using C functions for network data transmission

In network programming, transmitting data is a crucial operation. C provides powerful functions for this purpose, allowing developers to handle data transfers easily and efficiently.

Server side: receiving and sending data

On the server side, you can use the recv() function to receive data from the client. The prototype of this function is:

int recv(int sockfd, void *buf, size_t len, int flags);
Copy after login

Among them:

  • sockfd: The socket descriptor to read data.
  • buf: Buffer to store received data.
  • len: Maximum number of bytes to receive.
  • flags: Flags that control receiving behavior.

To send data to the client, you can use the send() function:

int send(int sockfd, const void *buf, size_t len, int flags);
Copy after login

Its parameters are similar to the recv() function .

Practical case: Create a simple echo server

The following is a sample code for implementing an echo server using C:

#include <iostream>
#include <stdlib.h>
#include <sys/socket.h> // 套接字支持
#include <netinet/in.h> // 互联网地址结构支持

using namespace std;

int main() {
  // 创建套接字
  int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_sockfd < 0) {
    cerr << "错误:无法创建套接字!" << endl;
    return -1;
  }

  // 设置服务器地址信息
  sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8080); // 将端口号转换为网络字节序
  server_addr.sin_addr.s_addr = INADDR_ANY; // 接受任何 IP 地址的连接

  // 绑定套接字到服务器地址
  if (bind(server_sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
    cerr << "错误:绑定套接字失败!" << endl;
    return -1;
  }

  // 监听连接
  if (listen(server_sockfd, 5) < 0) {
    cerr << "错误:监听连接失败!" << endl;
    return -1;
  }

  while (true) {
    // 接受连接
    sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    int client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_addr, &client_addr_len);
    if (client_sockfd < 0) {
      cerr << "错误:接受连接失败!" << endl;
      continue;
    }

    // 接收和发送数据
    char buffer[1024]; // 用于存储数据的缓冲区
    int n;
    while ((n = recv(client_sockfd, buffer, sizeof(buffer), 0)) > 0) {
      // 发送回客户端
      int m = send(client_sockfd, buffer, n, 0);
      if (m < 0) {
        cerr << "错误:发送数据失败!" << endl;
        break;
      }
    }

    // 关闭连接
    close(client_sockfd);
  }

  // 关闭套接字
  close(server_sockfd);

  return 0;
}
Copy after login

In this case , the recv() and send() functions are used to receive data from the client and send it back to the client as is.

The above is the detailed content of How do C++ functions implement data transmission in network programming?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template