Integrated UNP The first five chapters, wrote a server-client program.
Main function: The client reads the string from the standard input to the server and the server is displayed. After the server accepts the connection, the fork () child process implements the operation.
Server main process:
Socket ()->bind ()->listen ()->accept ()->read ()
Client:
Socket ()->connect ()->write ()
Test results:
(1) New server, client
[Email protected]Virtual-machine:~$ Lsof-ICOMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEserver16423Zy3uIPv41739090t0 TCP *:9999(LISTEN) client16425Zy3uIPv41725440t0 TCP localhost:44896->localhost:9999(established) server16426Zy4uIPv41739100t0 TCP localhost:9999->localhost:44896(established)
The server's parent process is in the listening state, and the child process and client are in the established connection state.
(2) data transmission, the client sends "off" means to close the connection
[Email protected] virtual-machine:~/practice/tcp$./client Sockethellooff[email protected]-Virtual- machine:~/practice/tcp$./is0.0. 0.0 9999 from127.0. 0.1 44896 Sockethello
(3) Port status after connection is closed
[Email protected] Virtual-machine:~$ lsof-iCOMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEserver 16423 Zy 3u 173909 0t0 TCP *:9999 (LISTEN)
Server code:
1#include <stdio.h>2#include <sys/socket.h>3#include <sys/types.h>4#include <arpa/inet.h>5#include <netinet/inch.h>6#include <unistd.h>7#include <string.h>8#include <stdlib.h>9 Ten One voidFoostructSockaddr_in addr,intFD) { A Charbuff[1024x768]; - CharC; - inti; the intLen; -Inet_ntop (af_inet, &addr.sin_addr, Buff,sizeof(Buff)); -printf"connction from%s, Port%d\n", Buff, Ntohs (addr.sin_port)); - while(1) { +i =0; -c ='0'; + while(I <1024x768&& C! =' /') { ALen = Read (FD, (void*) &c,1); at if(Len >0) { -buff[i++] =C; - } - } -Buff[i] =' /'; - if(I >1) { in if(strcmp (Buff,"OFF") ==0){ - Break; to } +printf"%s\n", buff); - } theSleep1); * } $Exit0);Panax Notoginseng } - intMain () { the pid_t pid; + intserverfd, clientfd; A structsockaddr_in serveraddr, clientaddr; the socklen_t Clientaddr_len; +U_short port =9999; - Charbuff[1024x768]; $SERVERFD = socket (af_inet, Sock_stream,0); $ if(SERVERFD = =-1) { -printf"Socket error\n"); -Exit0); the } -Bzero (&SERVERADDR,sizeof(SERVERADDR));Wuyiserveraddr.sin_family =af_inet; theServeraddr.sin_port =htons (port); -SERVERADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); Wu if(Bind (SERVERFD, (structSOCKADDR *) &serveraddr,sizeof(SERVERADDR)) == -1) { -printf"bind error\n"); AboutExit0); $ } -Inet_ntop (af_inet, &serveraddr.sin_addr, Buff,sizeof(Buff)); -printf"server IP is%s running on port%d\n", Buff, Ntohs (serveraddr.sin_port)); - A if(Listen (SERVERFD,1) == -1 ) { +printf"Listen error\n"); theExit0); - } $ while(1) { theClientaddr_len =sizeof(CLIENTADDR); theCLIENTFD = Accept (SERVERFD, (structSOCKADDR *) &clientaddr, &Clientaddr_len); the if(PID = fork ()) = =0) { the Close (SERVERFD); - foo (clientaddr, clientfd); in Close (CLIENTFD); theExit0); the } About Close (CLIENTFD); the } the Close (SERVERFD); theExit0); +}
Client code:
1#include <stdio.h>2#include <sys/socket.h>3#include <sys/types.h>4#include <arpa/inet.h>5#include <netinet/inch.h>6#include <unistd.h>7#include <string.h>8#include <stdlib.h>9 Ten intMain () { One intclientfd; A structsockaddr_in clientaddr; - Charbuff[1024x768]; - inti; theCLIENTFD = socket (af_inet, Sock_stream,0); - if(Clientfd = =-1) { -printf"Socket error\n"); -Exit0); + } -clientaddr.sin_family =af_inet; +CLIENTADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1"); AClientaddr.sin_port = htons (9999); at if(Connect (CLIENTFD,structSOCKADDR *) &clientaddr,sizeof(CLIENTADDR)) == -1) { -printf"connet error\n"); -Exit0); - } - while(1) { - gets (buff); ini =0; - Do{ toWrite (CLIENTFD, (void*) &buff[i],1); +} while(buff[i++]! =' /'); - if(strcmp (Buff,"OFF") ==0){ the Break; * } $ }Panax Notoginseng Close (CLIENTFD); -Exit0); the}
Socket programming under Linux