标签:
今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作!
在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中,如果我们使用TCP传输,会造成传输速度较慢的情况,所以我们在进行文件传输的过程中,最好要使用UDP传输。
在其中,我们需要写两个程序,一个客户端,一个服务端,在一个终端中,先运行服务端,在运行客户端,在服务端和客户端都输入IP地址和端口号,注意服务端和客户端的端口号要相同,然后选择功能,在linux网络编程中,使用UDP进行传输属于比较简单的操作,所以直接上代码吧,详细的讲解我将会在之后上传!
client(客户端):
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <sys/types.h> 7 #include <fcntl.h> 8 #include <sys/stat.h> 9 #include "protocol.h" 10 11 /*--------------------------*/ 12 int socketfd ; 13 int addrlen; 14 struct sockaddr_in server; 15 struct protocol sentbuf; 16 struct protocol recvbuf; 17 int num; 18 char ip[20]; 19 int port; 20 int choice ; 21 char filename[100]; 22 23 /*--------------------------*/ 24 void ShowMenu(); 25 void DownLoad(); 26 void UpLoad(); 27 void ShutDown(); 28 29 30 int main(){ 31 /*---------------------------*/ 32 socketfd = socket(AF_INET , SOCK_DGRAM , 0 ); 33 if(socketfd == -1){ 34 perror("socket failed!\n"); 35 exit(1); 36 } 37 /*---------------------------*/ 38 printf("please input ip of server:\n"); 39 scanf("%s",ip); 40 printf("please input port of server:\n"); 41 scanf("%d",&port); 42 /*---------------------------*/ 43 bzero(&server , sizeof(server)); 44 server.sin_family = AF_INET; 45 server.sin_port = htons(port); 46 server.sin_addr.s_addr = inet_addr(ip); 47 addrlen = sizeof(server); 48 /*---------------------------*/ 49 while(1){ 50 ShowMenu(); 51 scanf("%d",&choice); 52 if(choice == DOWNLOAD){ 53 printf("client download!\n"); 54 DownLoad(); 55 } 56 else if(choice == UPLOAD){ 57 UpLoad(); 58 } 59 else if(choice == SHUTDOWN){ 60 printf("client shutdown!\n"); 61 ShutDown(); 62 break; 63 } 64 else{ 65 printf("please input the right choice!\n"); 66 } 67 } 68 close(socketfd); 69 return 0; 70 } 71 72 void ShowMenu(){ 73 printf("please make a choice:\n"); 74 printf("0:shutdown!\n"); 75 printf("1:download!\n"); 76 printf("2:upload!\n"); 77 } 78 79 void DownLoad(){ 80 bzero(&recvbuf , sizeof(recvbuf)); 81 bzero(&sentbuf , sizeof(sentbuf)); 82 bzero(filename , sizeof(filename)); 83 printf("please input the filename:\n"); 84 scanf("%s",sentbuf.buf); 85 sentbuf.command = DOWNLOAD; 86 sendto(socketfd , &sentbuf , sizeof(sentbuf), 0 , (struct sockaddr*)&server , sizeof(server)); 87 bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf)); 88 recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&server , &addrlen); 89 printf("recvbuf:%d\n",recvbuf.command); 90 if(recvbuf.command == YES){ 91 printf("YES!\n"); 92 int choice_1; 93 printf("if you input a 5 , the file transmission start!\n"); 94 scanf("%d",&choice_1); 95 if(choice_1 == START){ 96 sentbuf.command = START; 97 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server , sizeof(server)); 98 int no = 0 ; 99 int fd = open(filename , O_CREAT | O_TRUNC | O_WRONLY , 0644 ); 100 if(fd < 0){ 101 perror("creat file is failed!\n"); 102 exit(1); 103 } 104 bzero(&recvbuf , sizeof(recvbuf)); 105 while( ( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&server , &addrlen)) > 0){ 106 if( recvbuf.command == CONTENT ){ 107 if(no == recvbuf.no){ 108 write(fd , recvbuf.buf , recvbuf.len); 109 bzero(&recvbuf , sizeof(recvbuf)); 110 } 111 else{ 112 perror("The file no is not same, Some massage is missed!error occured!\n"); 113 break; 114 } 115 } 116 if( recvbuf.command == END){ 117 close(fd); 118 printf("transmission is successful!\n"); 119 break; 120 } 121 } 122 } 123 } 124 else if(recvbuf.command == NO){ 125 perror("No such file on server!\n"); 126 } 127 else{ 128 perror("recvbuf.command error!\n"); 129 exit(1); 130 } 131 } 132 133 void ShutDown(){ 134 sentbuf.command = SHUTDOWN; 135 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server , sizeof(server)); 136 printf("client is end!\n"); 137 } 138 139 void UpLoad(){ 140 bzero(&recvbuf , sizeof(recvbuf)); 141 bzero(&sentbuf , sizeof(sentbuf)); 142 bzero(filename , sizeof(filename)); 143 printf("please input you want to upload filename:\n"); 144 scanf("%s",sentbuf.buf); 145 sentbuf.command = UPLOAD; 146 sendto(socketfd , &sentbuf , sizeof(sentbuf), 0 , (struct sockaddr*)&server , sizeof(server)); 147 bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf)); 148 int fd ; 149 fd = open(filename , O_RDONLY); 150 if(fd < 0){ 151 perror("The file is not exist!\n"); 152 exit(1); 153 } 154 recvfrom(socketfd , &recvbuf , sizeof(recvbuf), 0 , (struct sockaddr*)&server , &addrlen); 155 if( recvbuf.command == START ){ 156 int no = 0 ; 157 while( ( num = read(fd , sentbuf.buf , INFOLEN)) > 0 ){ 158 sentbuf.no = no ; 159 sentbuf.command = CONTENT; 160 sentbuf.len = strlen(sentbuf.buf); 161 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server,sizeof(server)); 162 no++; 163 bzero(&sentbuf , sizeof(sentbuf)); 164 } 165 bzero(&sentbuf , sizeof(sentbuf)); 166 sentbuf.command = END; 167 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server , sizeof(server)); 168 } 169 else if(recvbuf.command == NO){ 170 printf("not transmission!\n"); 171 } 172 else{ 173 perror("error! wrong choice!\n"); 174 } 175 }
server(服务端):
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <sys/types.h> 7 #include <fcntl.h> 8 #include <sys/stat.h> 9 #include "protocol.h" 10 11 int socketfd; 12 int addrlen; 13 struct sockaddr_in server; 14 struct sockaddr_in client; 15 struct protocol sentbuf; 16 struct protocol recvbuf; 17 int num; 18 char ip[20]; 19 int port; 20 int choice; 21 char filename[100]; 22 23 void DownLoad(); 24 void ShutDown(); 25 void UpLoad(); 26 27 int main(){ 28 socketfd = socket( AF_INET , SOCK_DGRAM , 0 ); 29 if( socketfd == -1){ 30 perror("socket failed!\n"); 31 exit(1); 32 } 33 34 printf("please input client ip:\n"); 35 scanf("%s" , ip); 36 printf("please input client port:\n"); 37 scanf("%d",&port); 38 39 bzero(&server , sizeof(server)); 40 server.sin_family = AF_INET; 41 server.sin_port = htons(port); 42 server.sin_addr.s_addr = inet_addr(ip); 43 44 int bin = bind(socketfd , (struct sockaddr*)&server , sizeof(server)); 45 if(bin == -1){ 46 perror("bind failed!\n"); 47 exit(1); 48 } 49 50 addrlen = sizeof(client); 51 //char filename[100]; 52 53 while(1){ 54 bzero(&recvbuf , sizeof(recvbuf)); 55 num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&client , &addrlen); 56 choice = recvbuf.command; 57 bcopy(recvbuf.buf ,filename, sizeof(recvbuf.buf)); 58 printf("%s\n",filename); 59 if(choice == DOWNLOAD){ 60 printf("kkkkkkkk\n"); 61 DownLoad(); 62 } 63 else if(choice == SHUTDOWN){ 64 printf("server will shutdown!\n"); 65 ShutDown(); 66 break; 67 } 68 else if(choice == UPLOAD){ 69 UpLoad(); 70 } 71 } 72 return 0; 73 } 74 75 void DownLoad(){ 76 char buf[100]; 77 int fd ; 78 printf("11111\n"); 79 printf("%s\n",filename); 80 fd = open((filename) , O_RDONLY); 81 printf("fd:%d\n",fd); 82 bzero(&sentbuf , sizeof(sentbuf)); 83 if(fd < 0){ 84 sentbuf.command = NO; 85 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&client , sizeof(client)); 86 printf("no such file!\n"); 87 exit(1); 88 } 89 else{ 90 sentbuf.command = YES; 91 printf("YES!\n"); 92 sendto(socketfd , &sentbuf, sizeof(sentbuf) , 0 , (struct sockaddr*)&client , sizeof(client)); 93 recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&client , &addrlen); 94 if(recvbuf.command == START){ 95 int no = 0 ; 96 while((num = read(fd , sentbuf.buf , INFOLEN)) > 0){ 97 sentbuf.no = no ; 98 sentbuf.command = CONTENT; 99 sentbuf.len = strlen(sentbuf.buf); 100 sendto(socketfd , &sentbuf , sizeof(sentbuf),0,(struct sockaddr*)&client , sizeof(client)); 101 no++; 102 bzero(&sentbuf , sizeof(sentbuf)); 103 } 104 bzero(&sentbuf , sizeof(sentbuf)); 105 sentbuf.command = END; 106 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&client , sizeof(client)); 107 } 108 } 109 } 110 111 void ShutDown(){ 112 printf("Now server is shutdown!\n"); 113 } 114 115 116 void UpLoad(){ 117 //bzero(&recvbuf , sizeof(recvbuf)); 118 bzero(&sentbuf , sizeof(sentbuf)); 119 bzero(&filename , sizeof(filename)); 120 //recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&client , &addrlen); 121 printf("4:NO 5:START\n"); 122 scanf("%d" , &sentbuf.command); 123 sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&client , sizeof(client)); 124 if( sentbuf.command == START){ 125 int no = 0 ; 126 printf("filename:%s\n",recvbuf.buf); 127 int fd = open(recvbuf.buf , O_CREAT | O_TRUNC | O_WRONLY , 0644 ); 128 if(fd < 0){ 129 perror("create file failed!\n"); 130 exit(1); 131 } 132 bzero(&recvbuf , sizeof(recvbuf) ); 133 while(( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&client , &addrlen)) > 0){ 134 if( recvbuf.command == CONTENT ){ 135 if( no == recvbuf.no ){ 136 write(fd , recvbuf.buf , recvbuf.len); 137 bzero(&recvbuf , sizeof(recvbuf)); 138 } 139 else{ 140 perror("The file no is not same . Some massage is missed!\n"); 141 break; 142 } 143 } 144 if( recvbuf.command == END ){ 145 close(fd); 146 printf("transmission is successful!\n"); 147 break; 148 } 149 } 150 } 151 else if( sentbuf.command == NO ){ 152 printf("The file can‘t transmission!\n"); 153 } 154 else{ 155 perror("please input right choice!\n"); 156 exit(1); 157 } 158 }
makefile:
main:udpserver.o udpclient.o gcc -o udpserver udpserver.o gcc -o udpclient udpclient.o udpserver.o:udpserver.c gcc -c udpserver.c udpclient.o:udpclient.c gcc -c udpclient.c
linux网络环境下socket套接字编程(UDP文件传输)
标签:
原文地址:http://www.cnblogs.com/turnips/p/4915810.html