However, the function is relatively simple, and it will continue to be improved later!
Server.c
/ * Server program * /
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main (int argc, char * argv []) {int sockfd, new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size, portnumber; char hello [] = "Hello, Welcome to visited btlfe's Server ... / n" ; If (argc! = 2) {fprintf (stderr, "usage:% s portnumber / a / n", argv [0]); exit (1);} if ((portnumber = atoi (argv [1])) <0) {FPRINTF (stderr, "usage:% s portnumber / a / n", argv [0]); exit (1);} / * server side begins to establish a socket descriptor * / if ((Sockfd = Socket) AF_INET, SOCK_STREAM, 0) == - 1) {FPRINTF (stderr, "socket error:% s / n / a", strrror (errno)); exit (1);} PUTS ("this is a simple program to Test Socket Connect! "); SLEEP (1); PUTS (" Written Btlfe Of 6.21, 2004 "); Sleep (2); PUTS (" Now Server Starting ... please wait ... "); / * server Fill SockAddr Structure * /
bzero (& server_addr, sizeof (struct sockaddr_in)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl (INADDR_ANY); server_addr.sin_port = htons (portnumber); / * bundled sockfd descriptor * /
IF (Bind (STRUCKD, STRUCKADDR), SIZEOF (STRUCKADDR) == - 1) {FPRINTF (stderr, "bind error:% s / n / a", strrror (errno)); EXIT (1);} / * listener SOCKFD descriptor * / if (Listen (SockFD, 5) == - 1) {FPRINTF (stderr, "listen error:% s / n / a", strrror (errno)); exit (1);} / * Server block until the client is established * /
While (1) {sin_size = sizeof (struct sockaddr_in); if ((new_fd = accept (sockfd, (struct socdr *) (& client_addr), & sin_size)) == - 1) {fprintf (stderr, "accept error:% s / N / A ", strerror (errno); exit (1);} fprintf (stderr," server get connection from% s / n ", inet_ntoa (client_addr.sin_addr)); if (Write (new_fd, hello, strlen (Hello) == - 1) {FPRINTF (stderr, "write error:% s / n", strrror (errno)); exit (1);} close (new_fd);} close (sockfd); exit (0 } client.c
/ * Room program * /
#include
#include
#include
#include
#include
#include
#include
#include
#include
Int main (int Argc, char * argv []) {int Sockfd; char buffer [1024]; struct sockaddr_in server_addr; struct host * host; int portnumber, nbytes; buffer [2] = '/ 0'; if (Argc! = 3) {FPRINTF (stderr, "usage:% s hostname portnumber / a / n", argv [0]); exit (1);} if ((Host = gethostByname) == null) {FPRINTF (stderr, "gethostname error / n"); exit (1);} if ((portnumber = ATOI (Argv [2])) <0) {fprintf (stderr, "usage:% s hostname portnumber / A / n ", argv [0]); exit (1);} / * Room interface starts establishing a Socket descriptor * /
PUTS ("This Is A Simple Program to Test Socket Connect!"); Sleep (1); PUTS ("Written Btlfe, 6.21, 2004"); Sleep (2); PUTS ("Now Connect To The Server ... "); If ((SOCKFD = Socket (AF_INET, SOCK_STREAM, 0) == - 1) {fprintf (stderr," socket error:% s / a / n ", strrror (errno)); exit (1); }
/ * Fill client server data * / bzero (& server_addr, sizeof (server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons (portnumber); server_addr.sin_addr = * ((struct in_addr *) host-> h_addr); / * Client program initiates a connection request! * /