Server-side:
1#include <Windows.h>2#include <stdio.h>3#include <stdlib.h>4#include <iostream>5#include <sys/stat.h>6 7 using namespacestd;8 9 #definePORT 5800Ten #defineFILESIZE 4096*100 One A #pragmaComment (lib, "Ws2_32.lib") - - intMain () the { - DWORD wrequiredversion; - wsadata wsadata; - SOCKET LSD, CLIENTSD; + -FILE *FP =NULL; +CHAR imagebuffer[filesize]= {0}; A at intIresult; - -Wrequiredversion = Makeword (2,2); -Iresult = WSAStartup (wrequiredversion,&wsadata); - if(Iresult! =0) { -cout <<"Load Socket library failed"<<Endl; in return-1; - } to +LSD = socket (Af_inet,sock_stream,0); - if(LSD = =invalid_socket) { thecout <<"Socket failed"<<Endl; * return-1; $ }Panax Notoginseng -Sockaddr_in seraddr = {0}; theseraddr.sin_family =af_inet; +Seraddr.sin_port =htons (PORT); ASeraddr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); theIresult = Bind (LSD, (sockaddr*) &seraddr,sizeof(SERADDR)); + - if(Iresult = =socket_error) { $cout <<"bind error"<<Endl; $ return-1; - } - the //Iresult = Listen (lsd,5); - // Wuyi //struct _stat filestruct = {0}; the // - //_stat ("Jj.jpg", &filestruct); Wu // - //int size = filestruct.st_size; About $CLIENTSD =Accept (lsd,null,null); - -Fopen_s (&FP,"jj.jpg","RB"); - if(fp = =NULL) { Acout <<"Open File Error"<<Endl; + return-1; the } - $ theFread (Imagebuffer,1, FILESIZE,FP); the theIresult =Send (Clientsd,imagebuffer,filesize,msg_oob); the - incout <<"Send image Complete! "<< iresult<<Endl; the the CharC; AboutCIN >>C; the the closesocket (LSD); the closesocket (CLIENTSD); + - fclose (FP); the Bayi return 0; the}
Client:
1#include <Windows.h>2#include <stdio.h>3#include <stdlib.h>4#include <iostream>5 6 using namespacestd;7 8 #definePORT 58009 #defineFILESIZE 4096*100Ten One #pragmaComment (lib, "Ws2_32.lib") A - intMain () - { the DWORD wrequiredversion; - wsadata wsadata; - SOCKET LSD, CLIENTSD; - +FILE *FP =NULL; -CHAR imagebuffer[filesize]= {0}; + A intIresult; at -Wrequiredversion = Makeword (2,2); -Iresult = WSAStartup (wrequiredversion,&wsadata); - if(Iresult! =0) { -cout <<"Load Socket library failed"<<Endl; - return-1; in } - toLSD = socket (Af_inet,sock_stream,0); + if(LSD = =invalid_socket) { -cout <<"Socket failed"<<Endl; the return-1; * } $ Panax NotoginsengSockaddr_in seraddr = {0}; -seraddr.sin_family =af_inet; theSeraddr.sin_port =htons (PORT); +Seraddr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); A theIresult = Connect (LSD, (SOCKADDR *) &seraddr,sizeof(SERADDR)); + if(Iresult = =socket_error) { -cout <<"Connect Error"<<Endl; $ return-1; $ } - -Fopen_s (&FP,"jj.jpg","wb+"); the if(fp = =NULL) { -cout <<"Crate File Error"<<Endl;Wuyi return-1; the } - WuIresult = recv (Lsd,imagebuffer,filesize,0); - AboutIresult = fwrite (Imagebuffer,1, FILESIZE,FP); $ -Iresult =GetLastError (); - fclose (FP); - Acout <<"receive file Complete!"<<Endl; + the - return 0; $}
Winsock TCP Client and server-side transfer binaries