通过tcp socket实现Linux与windows之间的文件传输
window的客户端与服务器的代码来源于
http://www.jb51.net/article/53732.htm
目前还只能实现服务器传送文件到客户端,且在客户端中要写入的文件名我已经写死了,如果要换可以自己换
windows下
server端:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <WinSock2.h> 6 7 #define PORT 8087 8 #define SERVER_IP "127.0.0.1" 9 #define BUFFER_SIZE 1024 10 #define FILE_NAME_MAX_SIZE 512 11 #pragma comment(lib, "WS2_32") 12 /*文件从服务器发往客户端,客户端中输入的文件地址应该在相应的系统内存在,在客户端中的文件名是存贮的文件名*/ 13 int main() 14 { 15 // 声明并初始化一个服务端(本地)的地址结构 16 struct sockaddr_in server_addr; 17 server_addr.sin_family = AF_INET; 18 server_addr.sin_addr.S_un.S_addr = INADDR_ANY; 19 server_addr.sin_port = htons(PORT); 20 21 22 // 初始化socket dll 23 WSADATA wsaData; 24 WORD socketVersion = MAKEWORD(2, 0); 25 if (WSAStartup(socketVersion, &wsaData) != 0) 26 { 27 printf("Init socket dll error!"); 28 exit(1); 29 } 30 31 // 创建socket 32 SOCKET m_Socket = socket(AF_INET, SOCK_STREAM, 0); 33 if (SOCKET_ERROR == m_Socket) 34 { 35 printf("Create Socket Error!"); 36 exit(1); 37 } 38 int opt = 1; 39 setsockopt(m_Socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 40 //绑定socket和服务端(本地)地址 41 if (SOCKET_ERROR == bind(m_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr))) 42 { 43 printf("Server Bind Failed: %d", WSAGetLastError()); 44 exit(1); 45 } 46 47 //监听 48 if (SOCKET_ERROR == listen(m_Socket, 10)) 49 { 50 printf("Server Listen Failed: %d", WSAGetLastError()); 51 exit(1); 52 } 53 54 55 while (1) 56 { 57 printf("Listening To Client...\n"); 58 59 struct sockaddr_in client_addr; 60 int client_addr_len = sizeof(client_addr); 61 62 SOCKET m_New_Socket = accept(m_Socket, (struct sockaddr *)&client_addr, &client_addr_len); 63 if (SOCKET_ERROR == m_New_Socket) 64 { 65 printf("Server Accept Failed: %d", WSAGetLastError()); 66 break; 67 } 68 69 char buffer[BUFFER_SIZE];//="G:\Code\GTK+程序设计中文版.pdf" 70 memset(buffer, 0, BUFFER_SIZE); 71 if (recv(m_New_Socket, buffer, BUFFER_SIZE, 0) < 0) 72 { 73 printf("Server Receive Data Failed!"); 74 break; 75 } 76 77 char file_name[FILE_NAME_MAX_SIZE + 1]; 78 memset(file_name, 0, FILE_NAME_MAX_SIZE + 1); 79 strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE : strlen(buffer)); 80 file_name[strlen(buffer)] = '\0'; 81 printf("%s\n", file_name); 82 83 FILE * fp = fopen(file_name, "rb"); //windows下是"rb",表示打开一个只读的二进制文件 84 if (NULL == fp) 85 { 86 printf("File: %s Not Found\n", file_name); 87 } 88 else 89 { 90 memset(buffer, 0, BUFFER_SIZE); 91 int length = 0; 92 93 while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) 94 { 95 if (send(m_New_Socket, buffer, length, 0) < 0) 96 { 97 printf("Send File: %s Failed\n", file_name); 98 break; 99 } 100 memset(buffer, 0, BUFFER_SIZE); 101 } 102 103 fclose(fp); 104 printf("File: %s Transfer Successful!\n", file_name); 105 } 106 closesocket(m_New_Socket); 107 } 108 109 closesocket(m_Socket); 110 //释放winsock库 111 WSACleanup(); 112 return 0; 113 }
client端:ip地址填写服务器所在系统的ip地址
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <WinSock2.h> 6 7 #define PORT 8087 8 #define SERVER_IP "192.168.1.130" 9 #define BUFFER_SIZE 1024 10 #define FILE_NAME_MAX_SIZE 512 11 #pragma comment(lib, "WS2_32") 12 13 int main() 14 { 15 // 初始化socket dll 16 WSADATA wsaData; 17 WORD socketVersion = MAKEWORD(2, 0); 18 if (WSAStartup(socketVersion, &wsaData) != 0) 19 { 20 printf("Init socket dll error!"); 21 exit(1); 22 } 23 24 //创建socket 25 SOCKET c_Socket = socket(AF_INET, SOCK_STREAM, 0); 26 if (SOCKET_ERROR == c_Socket) 27 { 28 printf("Create Socket Error!"); 29 system("pause"); 30 exit(1); 31 } 32 33 //指定服务端的地址 34 struct sockaddr_in server_addr; 35 server_addr.sin_family = AF_INET; 36 server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP); 37 server_addr.sin_port = htons(PORT); 38 int opt = 1; 39 setsockopt(c_Socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 40 if (SOCKET_ERROR == connect(c_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr))) 41 { 42 printf("Can Not Connect To Server IP!\n"); 43 system("pause"); 44 exit(1); 45 } 46 47 //输入文件名 48 char file_name[FILE_NAME_MAX_SIZE + 1]; 49 memset(file_name, 0, FILE_NAME_MAX_SIZE + 1); 50 printf("Please Input File Name it will transfer to Server: "); 51 scanf("%s", &file_name); 52 53 char buffer[BUFFER_SIZE]; 54 memset(buffer, 0, BUFFER_SIZE); 55 strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE ? BUFFER_SIZE : strlen(file_name)); 56 57 //向服务器发送文件名 58 if (send(c_Socket, buffer, BUFFER_SIZE, 0) < 0) 59 { 60 printf("Send File Name Failed\n"); 61 system("pause"); 62 exit(1); 63 } 64 65 //打开文件,准备写入 66 FILE * fp = fopen("client.txt", "wb"); 67 //FILE * fp = fopen(file_name, "wb"); //windows下是"wb",表示打开一个只写的二进制文件 68 if (NULL == fp) 69 { 70 printf("File: %s Can Not Open To Write\n", file_name); 71 system("pause"); 72 exit(1); 73 } 74 else 75 { 76 memset(buffer, 0, BUFFER_SIZE); 77 int length = 0; 78 while ((length = recv(c_Socket, buffer, BUFFER_SIZE, 0)) > 0) 79 { 80 if (fwrite(buffer, sizeof(char), length, fp) < length) 81 { 82 printf("File: %s Write Failed\n", file_name); 83 break; 84 } 85 memset(buffer, 0, BUFFER_SIZE); 86 } 87 88 printf("Receive File: %s From Server Successful!\n", file_name); 89 } 90 91 fclose(fp); 92 closesocket(c_Socket); 93 94 //释放winsock库 95 WSACleanup(); 96 97 system("pause"); 98 return 0; 99 }
Linux下
server端
1 #include "head.h" 2 #define BUFF_SIZE 1024 3 #define PORT 8087 4 #define FILE_NAME_SIZE 512 5 int main(int argc,char *argv[]) 6 { 7 8 struct sockaddr_in s_addr,c_addr; 9 int s_sockfd,c_sockfd; 10 char buf[BUFF_SIZE]; 11 int c_addr_len=sizeof(struct sockaddr_in); 12 int s_addr_len=sizeof(struct sockaddr_in); 13 s_sockfd=socket(AF_INET,SOCK_STREAM,0); 14 if(s_sockfd==-1) 15 strerr("socket error"); 16 memset(&s_addr,0,s_addr_len); 17 s_addr.sin_family=AF_INET; 18 s_addr.sin_port=htons(PORT); 19 s_addr.sin_addr.s_addr=htonl(INADDR_ANY); 20 int opt=1; 21 setsockopt(s_sockfd,SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 22 if(bind(s_sockfd,(struct sockaddr*)&s_addr,s_addr_len)==-1) 23 strerr("bind error"); 24 if(listen(s_sockfd,5)==-1) 25 strerr("listen error"); 26 27 while(1) 28 { 29 printf("listening to connect........\n"); 30 c_sockfd=accept(s_sockfd,(struct sockaddr*)&c_addr,&c_addr_len); 31 if(c_sockfd==-1) 32 strerr("accept error"); 33 34 memset(buf,0,BUFF_SIZE); 35 if(recv(c_sockfd,buf,BUFF_SIZE,0)==-1) 36 strerr("recv error"); 37 char filename[FILE_NAME_SIZE+1]; 38 memset(filename,0,FILE_NAME_SIZE+1); 39 strncpy(filename,buf,strlen(buf)>FILE_NAME_SIZE? FILE_NAME_SIZE:strlen(buf)); 40 filename[strlen(buf)]='\0'; 41 printf("%s\n",filename); 42 43 FILE *fp=fopen(filename,"rb"); 44 if(NULL==fp) 45 strerr("fopen error"); 46 else 47 { 48 memset(buf,0,BUFF_SIZE); 49 int length=0; 50 while((length=fread(buf,sizeof(char),BUFF_SIZE,fp))>0) 51 { 52 if(send(c_sockfd,buf,length,0)==-1) 53 strerr("send error"); 54 memset(buf,0,BUFF_SIZE); 55 } 56 fclose(fp); 57 printf("FILE %s transfer successful\n",filename); 58 }//end else 59 close(c_sockfd); 60 }//end while(1) 61 62 close(s_sockfd); 63 return 0; 64 }
client端
1 #include "head.h" 2 3 #define PORT 8087 4 //#define SERVER_IP "127.0.0.1" 5 #define BUFF_SIZE 1024 6 #define FILE_NAME_MAX_SIZE 512 7 8 int main(int argc,char *argv[]) 9 { 10 if(argc!=3) 11 { 12 printf("usage : %s ip filename and format\n",argv[0]); 13 exit(1); 14 } 15 int sockfd; 16 struct sockaddr_in c_addr; 17 int c_addr_len=sizeof(struct sockaddr_in); 18 char buffer[BUFF_SIZE]; 19 sockfd=socket(AF_INET,SOCK_STREAM,0); 20 if(-1==sockfd) 21 strerr("socket error"); 22 memset(&c_addr,0,c_addr_len); 23 c_addr.sin_family = AF_INET; 24 c_addr.sin_addr.s_addr = inet_addr(argv[1]); 25 c_addr.sin_port = htons(PORT); 26 int opt = 1; 27 setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); 28 if(connect(sockfd,(struct sockaddr*)&c_addr,c_addr_len)==-1) 29 strerr("connect error"); 30 char file_name[FILE_NAME_MAX_SIZE + 1]; 31 memset(file_name, 0, FILE_NAME_MAX_SIZE + 1); 32 printf("Please Input File Name it will transfer to Server: "); 33 scanf("%s", &file_name); 34 memset(buffer, 0, BUFF_SIZE); 35 strncpy(buffer, file_name, strlen(file_name)>BUFF_SIZE ? BUFF_SIZE : strlen(file_name)); 36 if(send(sockfd,buffer,BUFF_SIZE,0)==-1) 37 strerr("send error"); 38 FILE *fp=fopen(argv[2],"wb"); 39 if(fp==NULL) 40 strerr("fopen error"); 41 else 42 { 43 memset(buffer,0,BUFF_SIZE); 44 int length=0; 45 while((length=recv(sockfd,buffer,BUFF_SIZE,0))>0) 46 { 47 if(fwrite(buffer,sizeof(char),length,fp)<length) 48 strerr("fwrite error"); 49 memset(buffer,0,BUFF_SIZE); 50 } 51 printf("receive file : %s from server successful\n",file_name); 52 } 53 fclose(fp); 54 close(sockfd); 55 return 0; 56 }
head.h 其中有很多的头文件用不上,要是自己知道socket编程需要使用那几个头文件,可以自己写,不知道就写head.h吧。
1 #include <sys/uio.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <unistd.h> 5 #include <stdlib.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <fcntl.h> 9 #include <errno.h> 10 #include <setjmp.h> 11 #include <pwd.h> 12 #include <grp.h> 13 #include <limits.h> 14 #include <shadow.h> 15 #include <sys/fsuid.h> 16 #include<netdb.h> 17 #include<time.h> 18 #include<sys/time.h> 19 #include <locale.h> 20 #include <sys/utsname.h> 21 #include <dirent.h> 22 #include<limits.h> 23 #include<ctype.h> 24 #include <strings.h> 25 #include<sys/socket.h> 26 #include<arpa/inet.h> 27 #include<netinet/in.h> 28 #include<sys/un.h> 29 void err(const char *msg) 30 { 31 perror("msg:"); 32 exit(1); 33 } 34 void strerr(const char *msg) 35 { 36 fprintf(stderr,"%s %s\n",msg,strerror(errno)); 37 exit(1); 38 }