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 to realize TCP Communication in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

It is believed that many inexperienced people have no idea about how to realize TCP communication in C language. therefore, this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

TCP protocol

TCP protocol: a connection-oriented, reliable, byte-stream-based transport layer communication protocol defined by RFC 793of IETF. In the simplified computer network OSI model, it performs the functions specified by the fourth transport layer.

Key words: three-way handshake, reliable, based on byte flow.

Some friends may ask, is TCP as simple as that? Of course not. As a very important transport protocol, TCP has a lot of detailed knowledge. I'm afraid it's not enough to talk about it in detail. However, in this article, we only need to know a few of his keyword features, we can well understand the following.

TCP server-side and client-side running process

As shown in the figure, this is a complete flow chart of the TCP server-client. In fact, I personally think that the program, no matter which language is the same, the core lies in the design of the algorithm and the call of the function. So what do the functions in the diagram mean?

1. Create socket

Socket is a structure that is created in the kernel

Sockfd=socket (AF_INET,SOCK_STREAM,0); / / AF_INT:ipv4, SOCK_STREAM:tcp protocol

two。 Call the bind function

Bind socket to addresses (including ip, port).

A structure address needs to be defined to convert the host byte order of port into a network byte order.

Struct sockaddr_in myaddr; / / address structure

Bind function

Bind (sockfd, (struct sockaddr*) & myaddr,sizeof (serveraddr))

3.listen snooping to queue received client connections

Listen (sockfd,8) / / the second parameter is the queue length

4. Call the accept function, get the request from the queue, and return the socket descriptor

If there is no request, it will block until the connection is obtained

Int fd=accept (sockfd, NULL,NULL); / / default parameters are used here.

5. Call read/write for two-way communication

6. Close the socket returned by accept

Close (scokfd)

The complete code is released below.

/ * Server * / # include # include int main () {int sockfd = socket (AF_INET, SOCK_STREAM, 0); / / create socket if (sockfd

< 0) { perror("socket"); return -1; } //创建失败的错误处理 printf("socket..............\n"); //成功则打印"socket。。。。" struct sockaddr_in myaddr; //创建"我的地址"结构体 memset(&myaddr, 0, sizeof(myaddr)); //对内存清零(保险起见) myaddr.sin_family = AF_INET; //选择IPV4地址类型 myaddr.sin_port = htons(8888); //选择端口号 myaddr.sin_addr.s_addr = inet_addr("192.168.3.169"); //选择IP地址 if (0 >

Bind (sockfd, (struct sockaddr*) & myaddr, sizeof (myaddr)) / / bind socket {perror ("bind"); return-1;} printf ("bind.\ n"); if (0 > listen (sockfd, 8)) / / call listen to listen on the specified port {perror ("listen"); return-1 } printf ("listen.\ n"); int connfd = accept (sockfd, NULL, NULL); / / get the request if (connfd) from the message queue using accept

< 0) { perror("accept"); return -1; } printf("accept..............\n"); char buf[100];//定义一个数组用来存储接收到的数据 int ret; while (1) { memset(buf, 0, sizeof(buf)); ret = read(connfd, buf, sizeof(buf)); if (0 >

Ret) {perror ("read"); break;} / / execute while loop to read data when else if (0 = = ret) {printf ("write close!\ n"); break;} printf ("recv:"); fputs (buf, stdout) / / print the received data} close (sockfd); / / close the socket close (connfd); / / disconnect return 0;} / * client * / (the specific function is the same as the server, so no more comments) # include # include int main () {int sockfd If (0 > (sockfd = socket (AF_INET, SOCK_STREAM, 0)) {perror ("socket"); return-1;} printf ("socket.\ n"); struct sockaddr_in srv_addr; memset (& srv_addr, 0, sizeof (srv_addr)); srv_addr.sin_family = AF_INET Srv_addr.sin_port = htons (8888); srv_addr.sin_addr.s_addr = inet_addr ("192.168.3.169"); if (0 > connect (sockfd, (struct sockaddr*) & srv_addr, sizeof (srv_addr)) {perror ("connect"); return-1 / / exit / / pthread_exit} printf ("connect.\ n"); char buf [100]; int ret; while (1) {printf ("send:"); fgets (buf, sizeof (buf), stdin); ret = write (sockfd, buf, sizeof (buf)) If (ret < 0) {perror ("write"); break;} if (strncmp (buf, "quit", 4) = = 0) break;} close (sockfd); return 0;} after reading the above, have you mastered how to implement TCP communication in C language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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