记录下来,供自己学习!
server.c
1 #define _CRT_SECURE_NO_DEPRECATE 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <WinSock2.h> 6 #define BUF_SIZE 1024 7 char buf[BUF_SIZE], message[BUF_SIZE], file_name[BUF_SIZE]; 8 void error_handling(char* message); 9 int main(int argc, char* argv[]) 10 { 11 WSADATA wsadata; 12 if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) 13 error_handling("WSAStartup error"); 14 15 SOCKET hServSock, hClntSock; 16 SOCKADDR_IN servAddr, clntAddr; 17 int clntAdrSize, length; 18 19 if (argc != 2) 20 { 21 printf("usage:%s <ip> <port>\n", argv[1]); 22 exit(1); 23 } 24 25 hServSock = socket(PF_INET, SOCK_STREAM, 0); 26 if (hServSock == INVALID_SOCKET) 27 error_handling("socket error"); 28 29 memset(&servAddr, 0, sizeof(servAddr)); 30 servAddr.sin_family = AF_INET; 31 servAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); 32 servAddr.sin_port = htons(atoi(argv[1])); 33 34 if (bind(hServSock, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) 35 error_handling("bind error"); 36 if (listen(hServSock, 3) == SOCKET_ERROR) 37 error_handling("listen error"); 38 while (1) 39 { 40 clntAdrSize = sizeof(clntAddr); 41 hClntSock = accept(hServSock, (struct sockaddr*)&clntAddr, &clntAdrSize); 42 if (hClntSock == SOCKET_ERROR) 43 error_handling("accept() error"); 44 else 45 printf("connecting client......\n"); 46 47 if (recv(hClntSock, message, sizeof(message), 0) < 0) 48 error_handling("recv error"); 49 50 strncpy(file_name, message, strlen(message)); 51 52 FILE *fp; 53 fp = fopen(file_name, "r"); 54 if (fp == NULL) 55 { 56 printf("fopen %s error\n", file_name); 57 exit(1); 58 } 59 else 60 { 61 while ((length = fread(buf, sizeof(char), 1024, fp)) > 0) 62 { 63 if (send(hClntSock, buf, length, 0) < 0) 64 error_handling("send error"); 65 else 66 printf("send %s successful\n", file_name); 67 memset(message, 0,sizeof(message)); 68 memset(file_name, 0, sizeof(file_name)); 69 } 70 fclose(fp); 71 } 72 closesocket(hClntSock); 73 } 74 closesocket(hServSock); 75 WSACleanup(); 76 return 0; 77 } 78 79 void error_handling(char* message) 80 { 81 fputs(message, stderr); 82 fputc(‘\n‘, stderr); 83 exit(1); 84 }
client.c
1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <WinSock2.h> 6 void error_handling(char* message); 7 #define BUF_SIZE 1024 8 int main(int argc, char* argv[]) 9 { 10 WSADATA wsadata; 11 if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) 12 error_handling("WSADTA error\n"); 13 14 SOCKADDR_IN servAddr; 15 SOCKET hSocket; 16 char buf[BUF_SIZE]; 17 char message[BUF_SIZE]; 18 int length; 19 FILE* fp; 20 if (argc != 3) 21 { 22 printf("usage:%s <ip><port>", argv[1]); 23 exit(1); 24 } 25 26 hSocket = socket(PF_INET, SOCK_STREAM, 0); 27 if (hSocket == SOCKET_ERROR) 28 error_handling("socket error"); 29 30 memset(&servAddr, 0, sizeof(servAddr)); 31 servAddr.sin_family = AF_INET; 32 servAddr.sin_addr.S_un.S_addr = inet_addr(argv[1]); 33 servAddr.sin_port = htons(atoi(argv[2])); 34 35 if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) 36 error_handling("connect error"); 37 else 38 printf("connecting......\n"); 39 40 fp = fopen("F:\\2.txt", "w"); 41 if (NULL == fp) 42 { 43 error_handling("fopen error"); 44 } 45 46 printf("enter file‘s name: "); 47 scanf("%s", message); 48 send(hSocket, message, sizeof(message), 0); 49 while ((length = recv(hSocket, buf, BUF_SIZE, 0)) > 0) 50 { 51 printf("%d", length); 52 if (fwrite(buf, sizeof(char), length, fp) < length) 53 { 54 printf("fwrite error"); 55 exit(1); 56 } 57 else 58 { 59 printf("receve successful\n"); 60 } 61 } 62 fclose(fp); 63 closesocket(hSocket); 64 WSACleanup(); 65 return 0; 66 } 67 void error_handling(char* message) 68 { 69 fputs(message, stderr); 70 fputc(‘\n‘, stderr); 71 exit(1); 72 }
时间: 2024-11-09 00:31:41