Windows收集編程之winsock完成文件傳輸示例。本站提示廣大學習愛好者:(Windows收集編程之winsock完成文件傳輸示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Windows收集編程之winsock完成文件傳輸示例正文
本文實例展現了Windows收集編程中winsock完成文件傳輸的辦法,關於停止Windows下的winsock收集編程有必定的自創參考價值。
該法式代碼重要是基於TCP流協定的winsock收集文件傳輸示例,采取Windows下的C說話編寫。可以完成傳遞任何格局文件的文件傳輸功效。
詳細完成代碼以下:
Server端代碼以下:
/************************************************************************* > File Name: Win_Server.c > Author: SongLee ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <WinSock2.h> #define PORT 8087 #define SERVER_IP "127.0.0.1" #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 512 #pragma comment(lib, "WS2_32") int main() { // 聲明並初始化一個辦事端(當地)的地址構造 sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.S_un.S_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); // 初始化socket dll WSADATA wsaData; WORD socketVersion = MAKEWORD(2, 0); if(WSAStartup(socketVersion, &wsaData) != 0) { printf("Init socket dll error!"); exit(1); } // 創立socket SOCKET m_Socket = socket(AF_INET, SOCK_STREAM, 0); if (SOCKET_ERROR == m_Socket) { printf("Create Socket Error!"); exit(1); } //綁定socket和辦事端(當地)地址 if (SOCKET_ERROR == bind(m_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr))) { printf("Server Bind Failed: %d", WSAGetLastError()); exit(1); } //監聽 if (SOCKET_ERROR == listen(m_Socket, 10)) { printf("Server Listen Failed: %d", WSAGetLastError()); exit(1); } while(1) { printf("Listening To Client...\n"); sockaddr_in client_addr; int client_addr_len = sizeof(client_addr); SOCKET m_New_Socket = accept(m_Socket, (sockaddr *)&client_addr, &client_addr_len); if (SOCKET_ERROR == m_New_Socket) { printf("Server Accept Failed: %d", WSAGetLastError()); break; } char buffer[BUFFER_SIZE]; memset(buffer, 0, BUFFER_SIZE); if (recv(m_New_Socket, buffer, BUFFER_SIZE, 0) < 0) { printf("Server Receive Data Failed!"); break; } char file_name[FILE_NAME_MAX_SIZE+1]; memset(file_name, 0, FILE_NAME_MAX_SIZE+1); strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE:strlen(buffer)); printf("%s\n", file_name); FILE * fp = fopen(file_name, "rb"); //windows下是"rb",表現翻開一個只讀的二進制文件 if (NULL == fp) { printf("File: %s Not Found\n", file_name); } else { memset(buffer, 0, BUFFER_SIZE); int length = 0; while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) { if (send(m_New_Socket, buffer, length, 0) < 0) { printf("Send File: %s Failed\n", file_name); break; } memset(buffer, 0, BUFFER_SIZE); } fclose(fp); printf("File: %s Transfer Successful!\n", file_name); } closesocket(m_New_Socket); } closesocket(m_Socket); //釋放winsock庫 WSACleanup(); return 0; }
Client端代碼以下:
/************************************************************************* > File Name: Win_Client.c > Author: SongLee ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <WinSock2.h> #define PORT 8087 #define SERVER_IP "127.0.0.1" #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 512 #pragma comment(lib, "WS2_32") int main() { // 初始化socket dll WSADATA wsaData; WORD socketVersion = MAKEWORD(2, 0); if(WSAStartup(socketVersion, &wsaData) != 0) { printf("Init socket dll error!"); exit(1); } //創立socket SOCKET c_Socket = socket(AF_INET, SOCK_STREAM, 0); if (SOCKET_ERROR == c_Socket) { printf("Create Socket Error!"); system("pause"); exit(1); } //指定辦事真個地址 sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP); server_addr.sin_port = htons(PORT); if (SOCKET_ERROR == connect(c_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr))) { printf("Can Not Connect To Server IP!\n"); system("pause"); exit(1); } //輸出文件名 char file_name[FILE_NAME_MAX_SIZE+1]; memset(file_name, 0, FILE_NAME_MAX_SIZE+1); printf("Please Input File Name On Server: "); scanf("%s", &file_name); char buffer[BUFFER_SIZE]; memset(buffer, 0, BUFFER_SIZE); strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE ? BUFFER_SIZE:strlen(file_name)); //向辦事器發送文件名 if(send(c_Socket, buffer, BUFFER_SIZE, 0) < 0) { printf("Send File Name Failed\n"); system("pause"); exit(1); } //翻開文件,預備寫入 FILE * fp = fopen(file_name, "wb"); //windows下是"wb",表現翻開一個只寫的二進制文件 if(NULL == fp) { printf("File: %s Can Not Open To Write\n", file_name); system("pause"); exit(1); } else { memset(buffer, 0, BUFFER_SIZE); int length = 0; while ((length = recv(c_Socket, buffer, BUFFER_SIZE, 0)) > 0) { if (fwrite(buffer, sizeof(char), length, fp) < length) { printf("File: %s Write Failed\n", file_name); break; } memset(buffer, 0, BUFFER_SIZE); } printf("Receive File: %s From Server Successful!\n", file_name); } fclose(fp); closesocket(c_Socket); //釋放winsock庫 WSACleanup(); system("pause"); return 0; }
法式中備有較為詳實的正文,信任年夜家不難懂得。感興致的同伙可以依據本身須要對法式加以靈巧改良,使其功效加倍壯大。