标签:style blog color os io ar 文件 art div
服务器端:
1 #include<Windows.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <sys/stat.h> 6 7 using namespace std; 8 9 #define PORT 5800 10 #define FILESIZE 4096*100 11 12 #pragma comment(lib,"Ws2_32.lib") 13 14 int main() 15 { 16 DWORD wRequiredVersion; 17 WSADATA wsaData; 18 SOCKET lsd, clientsd; 19 20 FILE *fp = NULL; 21 CHAR imagebuffer[FILESIZE]= {0}; 22 23 int iResult; 24 25 wRequiredVersion = MAKEWORD(2,2); 26 iResult = WSAStartup(wRequiredVersion,&wsaData); 27 if (iResult != 0) { 28 cout << "Load socket library failed"<<endl; 29 return -1; 30 } 31 32 lsd = socket(AF_INET,SOCK_STREAM,0); 33 if(lsd == INVALID_SOCKET) { 34 cout << "socket failed" <<endl; 35 return -1; 36 } 37 38 sockaddr_in seraddr = {0}; 39 seraddr.sin_family = AF_INET; 40 seraddr.sin_port = htons(PORT); 41 seraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); 42 iResult = bind(lsd,(sockaddr*)&seraddr,sizeof(seraddr)); 43 44 if (iResult == SOCKET_ERROR) { 45 cout << "bind error"<<endl; 46 return -1; 47 } 48 49 // iResult = listen(lsd,5); 50 // 51 // struct _stat filestruct = {0}; 52 // 53 // _stat("jj.jpg",&filestruct); 54 // 55 // int size = filestruct.st_size; 56 57 clientsd = accept(lsd,NULL,NULL); 58 59 fopen_s(&fp,"jj.jpg","rb"); 60 if (fp == NULL) { 61 cout << "open file error"<<endl; 62 return -1; 63 } 64 65 66 fread(imagebuffer,1,FILESIZE,fp); 67 68 iResult = send(clientsd,imagebuffer,FILESIZE,MSG_OOB); 69 70 71 cout << "send image complete ! " << iResult<<endl; 72 73 char c; 74 cin >>c; 75 76 closesocket(lsd); 77 closesocket(clientsd); 78 79 fclose(fp); 80 81 return 0; 82 }
客户端:
1 #include<Windows.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <iostream> 5 6 using namespace std; 7 8 #define PORT 5800 9 #define FILESIZE 4096*100 10 11 #pragma comment(lib,"Ws2_32.lib") 12 13 int main() 14 { 15 DWORD wRequiredVersion; 16 WSADATA wsaData; 17 SOCKET lsd, clientsd; 18 19 FILE *fp = NULL; 20 CHAR imagebuffer[FILESIZE]= {0}; 21 22 int iResult; 23 24 wRequiredVersion = MAKEWORD(2,2); 25 iResult = WSAStartup(wRequiredVersion,&wsaData); 26 if (iResult != 0) { 27 cout << "Load socket library failed"<<endl; 28 return -1; 29 } 30 31 lsd = socket(AF_INET,SOCK_STREAM,0); 32 if(lsd == INVALID_SOCKET) { 33 cout << "socket failed" <<endl; 34 return -1; 35 } 36 37 sockaddr_in seraddr = {0}; 38 seraddr.sin_family = AF_INET; 39 seraddr.sin_port = htons(PORT); 40 seraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); 41 42 iResult = connect(lsd,(sockaddr *)&seraddr,sizeof(seraddr)); 43 if (iResult == SOCKET_ERROR) { 44 cout << "connect error "<<endl; 45 return -1; 46 } 47 48 fopen_s(&fp,"jj.jpg","wb+"); 49 if(fp == NULL) { 50 cout << "crate file error"<<endl; 51 return -1; 52 } 53 54 iResult = recv(lsd,imagebuffer,FILESIZE,0); 55 56 iResult = fwrite(imagebuffer,1,FILESIZE,fp); 57 58 iResult = GetLastError(); 59 fclose(fp); 60 61 cout << "receive file complete !"<<endl; 62 63 64 return 0; 65 }
标签:style blog color os io ar 文件 art div
原文地址:http://www.cnblogs.com/disemboltura/p/3953911.html