Winsock TCP client and server-side transfer binaries

Source: Internet
Author: User
Tags htons

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 (&AMP;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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.