Home  >  Article  >  Backend Development  >  How do C++ functions establish and manage server-side sockets in network programming?

How do C++ functions establish and manage server-side sockets in network programming?

PHPz
PHPzOriginal
2024-04-26 13:03:02974browse

C provides establishment and management functions for server-side sockets, including creating sockets through socket(), binding addresses and ports through bind(), and listening for connections through listen(). Once established, you can use accept() to accept the connection, send()/recv() to send/receive data, and finally close the socket with close().

C++ 函数在网络编程中如何建立和管理服务器端套接字?

How C function creates and manages server-side sockets in network programming

In network programming, server-side sockets Words are the key to communication between processes and clients. C provides a rich set of functions for developing network applications that can be used to establish and manage sockets on the server side.

Establishing a server-side socket

To establish a server-side socket, you need to call the following steps:

  1. Using The socket() function creates a socket.
  2. Use the bind() function to bind the socket to an address and port number.
  3. Use the listen() function to put the socket in the listening state.
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
  // 创建套接字
  int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_fd == -1) {
    std::cerr << "socket() failed" << std::endl;
    return -1;
  }

  // 绑定套接字
  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8080);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int bind_result = bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
  if (bind_result == -1) {
    std::cerr << "bind() failed" << std::endl;
    return -1;
  }

  // 监听套接字
  int listen_result = listen(server_fd, 5);
  if (listen_result == -1) {
    std::cerr << "listen() failed" << std::endl;
    return -1;
  }

  // ... // 处理客户端连接的代码
}

Manage server-side sockets

Once the socket is established, it can be used to communicate with the client. Here are the main ways on how to manage server-side sockets:

  • Use the accept() function to accept connections from clients.
  • Use the send() and recv() functions to send and receive data.
  • Use the close() function to close the socket.

Practical case: TCP server

The following is a simple C TCP server example that demonstrates how to establish and manage server-side sockets :

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>  // for close()

int main() {
  // 创建套接字
  int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_fd == -1) {
    std::cerr << "socket() failed" << std::endl;
    return -1;
  }

  // 绑定套接字
  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8080);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int bind_result = bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
  if (bind_result == -1) {
    std::cerr << "bind() failed" << std::endl;
    return -1;
  }

  // 监听套接字
  int listen_result = listen(server_fd, 5);
  if (listen_result == -1) {
    std::cerr << "listen() failed" << std::endl;
    return -1;
  }

  // 接受客户端连接
  int client_fd;
  while ((client_fd = accept(server_fd, (struct sockaddr *) NULL, NULL)) != -1) {
    // 为客户端创建子进程进行处理
    if (fork() == 0) {
      // ... // 处理客户端连接的代码
      close(client_fd);
      exit(0);
    }
  }

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

  return 0;
}

The above is the detailed content of How do C++ functions establish and manage server-side sockets in network programming?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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