1. Network programming
1.1 Basic Concepts
Objective:
Implementing inter-process communication
Network:
Connect multiple hosts to form a network.
Internet:
Connecting the network with the network constitutes the Internet.
ip:
Unique Indication of a Host in a Network
Port:
A process is uniquely marked on a host.
1.2 Network Model
2.tcp programming process
3. tcp implementation
3.1tcp
ser.c
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<assert.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.0.1"); int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); assert(res!=-1); listen(sockfd,5);//creat listen list; //finished sharked,unfinished~,list while(1) { int len =sizeof(caddr); int c = accept(sockfd,(struct sockaddr*)&caddr,&len);//link taojiezi if(c<0) { continue; } printf("accept c=%d,ip=%s,port=%d\n",c,inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port)); while(1) { char buff[128]={0}; //int n=recv(c,buff,127,0);//n==0,close; int n=recv(c,buff,1,0);//n==0,close; if(n<=0) { break; } printf("buff==%s\n",buff); send(c,"ok",2,0); } printf("one client over!!\n"); close(c); } }
cli.c
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<assert.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.0.1"); int res=connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); assert(res!=-1); while(1) { char buff[128]={0}; printf("input:\n"); fgets(buff,128,stdin); if(strncmp(buff,"end",3)==0) { break; } send(sockfd,buff,strlen(buff),0); memset(buff,0,128); recv(sockfd,buff,127,0); printf("buff==%s\n",buff); } close(sockfd); }
tcp of 3.2 fork
fork_ser.c
#include<stdio.h> #include<string.h> #include<assert.h> #include<unistd.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<pthread.h> #include<signal.h> void sig_fun(int sig) { int val=0; wait(&val); //waitpid(-1,&val,0); } int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.1"); int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); assert(res!=-1); listen(sockfd,5); //signal() while(1) { int len=sizeof(caddr); int c=accept(sockfd,(struct sockaddr*)&caddr,&len); if(c<0) { continue; } pid_t pid =fork(); if(pid==-1) { close(c); continue; } if(pid==0) { while(1) { char buff[128]={0}; int n=recv(c,buff,127,0); if(n<=0) { break; } printf("buff==%s",buff); send(c,"ok",2,0); } close(c); printf("one client over\n"); //exit(0); } } }
tcp of 3.3 threads
///1.thread_ser.c #include<stdio.h> #include<string.h> #include<assert.h> #include<unistd.h> #include<sys/socket.h>// #include<netinet/in.h> #include<arpa/inet.h> #include<pthread.h> void *thread_fun(void *arg) { int c=(int)arg; while(1) { char buff[128]={0}; int n=recv(c,buff,127,0); if(n<=0) { break; } printf("recv(%d)=%s\n",c,buff); send(c,"ok",2,0); } close(c); printf("one cilent over\n"); } int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.1"); int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); assert(res!=-1); listen(sockfd,5); while(1) { int len=sizeof(caddr); int c=accept(sockfd,(struct sockaddr*)&caddr,&len); if(c<0) { continue; } pthread_t id; pthread_create(&id,NULL,thread_fun,(void *)c); } // exit(0); }
2.thread_cli
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<assert.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.1"); int res=connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); assert(res!=-1); while(1) { char buff[128]={0}; printf("input:\n"); fgets(buff,128,stdin); if(strncmp(buff,"end",3)==0) { break; } send(sockfd,buff,strlen(buff),0); memset(buff,0,128); recv(sockfd,buff,127,0); printf("buff==%s\n",buff); } close(sockfd); }
4. Implementation of UDP
4.1udp_ser
#include<stdio.h> #include<unistd.h> #include<assert.h> #include<sys/socket.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> int main() { int sockfd=socket(AF_INET,SOCK_DGRAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.1"); int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); assert(res!=-1); while(1) { int len=sizeof(caddr); char buff[128]={0}; int n=recvfrom(sockfd,buff,127,0,(struct sockaddr*)&caddr,&len); if(n==-1) { continue; } printf("port:%d,buff==%s\n",ntohs(caddr.sin_port),buff); sendto(sockfd,"0k",2,0,(struct sockaddr*)&caddr,sizeof(caddr)); } }
4.2udp_cli
#include<stdio.h> #include<unistd.h> #include<assert.h> #include<sys/socket.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> int main() { int sockfd=socket(AF_INET,SOCK_DGRAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.1"); while(1) { char buff[128]={0}; printf("input:\n"); fgets(buff,128,stdin); if(strncmp(buff,"end",3)==0) { break; } sendto(sockfd,buff,strlen(buff),0,(struct sockaddr*)&saddr,sizeof(saddr)); int len =sizeof(saddr); memset(buff,0,128); recvfrom(sockfd,buff,127,0,(struct sockaddr*)&saddr,&len); printf("buff==%s\n",buff); } //close(s); }
5.myhttp.c
1 #include<stdio.h> 2 #include<assert.h> 3 #include<unistd.h> 4 #include<string.h> 5 #include<sys/socket.h> 6 #include<arpa/inet.h> 7 #include<netinet/in.h> 8 #include<fcntl.h> 9 //#define IPSTR "192.168.228.128" 10 #define IPSTR "127.0.0.1" 11 #define PORT 80 12 #define BUFSIZE 1024 13 int creat_socket(); 14 int main() 15 { 16 int sockfd=creat_socket(); 17 assert(sockfd!=-1); 18 while(1) 19 { 20 struct sockaddr_in caddr; 21 int len=sizeof(caddr); 22 23 int c=accept(sockfd,(struct sockaddr*)&caddr,&len); 24 char buff[BUFSIZE]={0}; 25 26 int n=recv(c,buff,BUFSIZE-1,0); 27 printf("recv(%d):%s\n",n,buff); 28 29 char *s=strtok(buff," "); 30 if(s==NULL) 31 { 32 close(c); 33 continue; 34 } 35 printf("method %s\n",s);//GET; 36 s=strtok(NULL," "); 37 38 char path[128]={"/home/yhx/study/19linux/k0411"};//path 39 if(strcmp(s,"/")==0) 40 { 41 strcat(path,"/index.html"); 42 } 43 else 44 { 45 strcat(path,s); 46 } 47 //send(c,"hello gy1802/1803",17,0); 48 int fd=open(path,O_RDONLY); 49 if(fd==-1) 50 { 51 char res_buff[128]={"HTTP/1.1 404 not found\r\n"}; 52 strcat(res_buff,"Service:myhttp\r\n"); 53 strcat(res_buff,"Content-length:3\r\n"); 54 strcat(res_buff,"\r\n"); 55 strcat(res_buff,"404"); 56 send(c,res_buff,strlen(res_buff),0); 57 close(c); 58 continue; 59 60 } 61 int size=lseek(fd,0,SEEK_END); 62 lseek(fd,0,SEEK_SET); 63 char head_buff[256]={"http/1.1 200 ok\r\n"}; 64 strcat(head_buff,"Service: myhttp\r\n"); 65 sprintf(head_buff+strlen(head_buff),"content Length:%d\r\n",size); 66 strcat(head_buff,"\r\n"); 67 68 send(c,head_buff,strlen(head_buff),0); 69 printf("\nsend:\n%s\n",head_buff); 70 71 char data[1024]={0}; 72 int sum=0; 73 74 while(sum=read(fd,data,1025)>0) 75 { 76 send(c,data,sum,0); 77 } 78 close(fd);//daun link 79 close(c); 80 } 81 } 82 int creat_socket() 83 { 84 int sockfd=socket(AF_INET,SOCK_STREAM,0); 85 struct sockaddr_in saddr; 86 memset(&saddr,0,sizeof(saddr)); 87 if(sockfd==-1) 88 { 89 return -1; 90 } 91 //:struct sockaddr_in saddr; 92 saddr.sin_family=AF_INET; 93 saddr.sin_port=htons(PORT); 94 saddr.sin_addr.s_addr=inet_addr(IPSTR); 95 96 int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); 97 if(res ==-1) 98 { 99 return -1; 100 } 101 listen(sockfd,20); 102 return sockfd; 103 } 104
Then write HTML file, you can access the page you write.