A Simply Server Code with C.

xiaoxiao2021-03-06  75

#include #include #include #include

#include / * # include * // * # include * // * # include * /

#define myport 3490 # Define Backlog 10 / * How Many Pending Connections Queue Will Hold * /

Main (void) {int SockFD, new_fd; struct sockaddr_in my_addr; / * my address information * / struct sockaddr_in their_addr; / * connector's address information information * / int SIN_SIZE

IF ((Sockfd = Socket (AF_INET, SOCK_STREAM, 0) == - 1) {Printf ("socket"); exit (1);

MY_ADDR.SIN_FAMILY = AF_INET; my_addr.sin_port = htons (myport); my_addr.sin_addr.s_addr = INADDR_ADR.S_ADDR = INADDR_Addr_addr = INADDR_Addr; / * Auto-Fill with My IP * / MEMSET (& (MY_ADDR.SIN_ZERO), '/ 0', 8);

/ * Don't forget your error checking for these calls: * / if (bind (STRUCKD, STRUCKADDR *) & my_addr, sizeof (struct sockaddr) == - 1) {Printf ("bind"); exit (1 }

IF (Listen (Sockfd, Backlog) == - 1) {Printf ("Listen"); exit (1);}

while (1) {sin_size = sizeof (struct sockaddr_in); if ((new_fd = accept (sockfd, (struct sockaddr *) & their_addr, & sin_size)) == - 1) {printf ( "accept"); continue;} printf ( "Server: Got Connection from% S / N", INET_NTOA (THEIR_ADDR.SIN_ADDR);

Close (SOCKFD); IF (Send (New_fd, "Hello, World! / N", 14, 0) == - 1) Printf ("send"); close (new_fd); exit (0);} return 0; }

转载请注明原文地址:https://www.9cbs.com/read-93489.html

New Post(0)