Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report