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

Simple example of unix socket Network programming TCP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Server.c#include # include int main (int argc, char** argv) {int ret;// creates the socket file int sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd = =-1) {perror ("socket\ n"); exit (1);} printf ("socket_fd =% d\ n", sockfd) / / use this address repeatedly to bind to the socket file. If the system is not set up, the connection will not be released until the last reference, that is, it will take several minutes for the system to re-bind int I = 1 position ret = setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, & I, sizeof (I)); if (ret) {perror ("setsockopt\ n"); exit (1);} printf ("setsockopt complete!\ n") / / create server socket address sock_addrstruct sockaddr_in server_addr;server_addr.sin_family = AF_INET;server_addr.sin_port = htons (4444); / / address specified INADDR_ANY indicates that you can receive packets from all network cards installed in the system server_addr.sin_addr.s_addr = INADDR_ANY;memset (server_addr.sin_zero, 8,8) / / bind socket to address so that all packets destined for port 4444 and protocol IPV4 point to the socket file ret = bind (sockfd, (struct sockaddr*) & server_addr, sizeof (struct sockaddr)); if (ret) {perror ("bind\ n"); exit (1);} printf ("bind complete!\ n") / / listen for requests connected to the socket without blocking. Set the queue size to 1, that is, when the queue is full, other connections will be rejected: ret = listen (sockfd, 10); if (ret) {perror ("listen\ n"); exit (1);} printf ("listen complete!\ n") / / receive a connection request and establish a connection. If there is no connection request, accept will block until a request comes struct sockaddr_in client_addr;int client_fd;socklen_t len = sizeof (struct sockaddr); client_fd = accept (sockfd, (struct sockaddr*) & client_addr, & len); if (client_fd =-1) {perror ("bind\ n"); exit (1);} printf ("accept complete!\ n") / / Please fetch the message char buf from client; ssize_t readnum;memset (buf,100,0); / / here should be the client socket file descriptor readnum = recv (client_fd, (void*) buf,100, MSG_WAITALL); / / clear the standard output fflush (stdout); printf ("received:%s\ nnumber byte:%d\ n", buf,strlen (buf)); / / send the message to the client ssize_t sendnum Char content = "\" server:received the data!\ "; sendsendnum = send (client_fd, content, 100,0); printf (" reply:%s\ nnumber byte:%d\ n ", content,sendnum); / / close the socket file close (sockfd); close (client_fd); return 0;} client.c#include # include int main (int argc, char** argv) {int ret / / create socket file int sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd = =-1) {perror ("socket\ n"); exit (1);} printf ("socket_fd =% d\ n", sockfd); / / create server socket address sock_addrstruct sockaddr_in server_addr;server_addr.sin_family = AF_INET;server_addr.sin_port = htons (4444) / / there are two ways to convert a string address to a network address: inet_pton (AF_INET, "127.0.0.1", (void*) & server_addr.sin_addr.s_addr); / / IPV4 IPV6 available / / server_addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); / / only IPV4memset (server_addr.sin_zero, 8,0) / / Connect server ret = connect (sockfd, (struct sockaddr*) & server_addr,sizeof (struct sockaddr)); if (ret = =-1) {perror ("connect\ n"); exit (1);} printf ("connect complete!\ n"); / / send message char content [100] = "\" hello,i am client!\ "; ssize_t sendsendnum = send (sockfd, content, strlen (content) + 1, 0) Printf ("send complete\ nnumber byte:%d\ ncontent:%s\ n", sendnum,content); / / receive message char buf [100]; ssize_t readnum;memset (buf,100,0); readnum = recv (sockfd, (void*) buf,100, MSG_WAITALL); / / clear standard output fflush (stdout); printf ("received:%s\ nnumber byte:%d\ n", buf,strlen (buf)); / / close socketclose (sockfd); return 0;}

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

Servers

Wechat

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

12
Report