Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does C++ realize the communication between socket server and client?

Shulou Source: shulou.com Published: 2022-06-01 20:49:52 09月30日 Update

This article mainly explains "how C++ realizes socket server and client communication". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how C++ realizes socket server and client communication" together!

I. Server:

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char const *argv[]) {

// 1. Create socket connection

int sock = socket(AF_INET, SOCK_STREAM, 0);

// 2. Initialize network structure

struct sockaddr_in addr;

addr.sin_family = AF_INET;

addr.sin_port = htons(11115);

addr.sin_addr.s_addr = INADDR_ANY;

// 3. Set port reusable

int flag = 1, fd = -1;

setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));

// 4. bound port

int ret = bind(sock, (struct sockaddr*)&addr, sizeof(addr));

if(ret

< 0) { perror("bind err"); close(sock); return -1; } // 5. 监听端口 listen(sock, 10); fd_set read_fdsets; while(true) { // 6. 客户端过来建立连接 int newfd = accept(sock, NULL, NULL); // 7. 接收客户端的数据 char buf[1024 + 1] = {0}; // 使用select 多路复用接收消息 FD_ZERO(&read_fdsets); // 清空fdset与所有文件句柄的联系 FD_SET(newfd, &read_fdsets); // 建立文件句柄fd与fdset的联系 struct timeval stOut; stOut.tv_sec = 10; stOut.tv_usec = 0; ret = select(newfd + 1, &read_fdsets, NULL, NULL, &stOut); if(ret >

0)// fd has changed

{

if(FD_ISSET(newfd, &read_fdsets))

{

memset(buf, 0, sizeof(buf));

ret = recv(newfd, buf, sizeof(buf), 0);

if(ret < 0)

{

perror("select recv error");

exit(-1);

}

printf("[select] newfd = %d, recv buf = %s\n", newfd, buf);

}

}

else if(ret == 0) //No message is sent after the client connects, timeout

puts("timeout. ");

else

puts("select socket error. ");

}

return 0;

}

II. Client

#include

#include

#include

#include

#include

int main(int argc, char const *argv[]) {

// 1. Creating a socket connection

int sock = socket(AF_INET, SOCK_STREAM, 0);

// 2. Initialize network structure

struct sockaddr_in addr;

addr.sin_family = AF_INET;

addr.sin_port = htons(11115);

addr.sin_addr.s_addr = inet_addr("127.0.0.1");

// 3. connection server

connect(sock, (struct sockaddr*)&addr, sizeof(addr));

// 4. transmit data

char sendBuf[1024 + 1] = "hello server, im client. ";

sleep(12);

send(sock, sendBuf, sizeof(sendBuf), 0);

// 5. close the connection

close(sock);

return 0;

}

Thank you for reading, the above is "C++ how to achieve socket server and client communication" content, after the study of this article, I believe that everyone on C++ how to achieve socket server and client communication this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Customers clients services clients communications ports learning network structures content handles data files messages structures networks contacts that is ideas situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Linux MariaDB Microsoft