WINSOCK Transfer File

Source: Internet
Author: User
Tags htons

SERVER:

Send_server.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <winsock2.h># Include <stdio.h> #pragma comment (lib, "Ws2_32.lib") int main (int argc,char** argv) {wsadata wsadata;if (WSAStartup (Makeword (2,2), &wsadata) = =-1) {printf ("WSAStartup error\r\n");} Socket server;server = socket (af_inet,sock_stream,ipproto_tcp), if (server = = Socket_error) {printf ("Server socket start Error\r\n "); return 0;} sockaddr_in server_service;server_service.sin_family = Af_inet;server_service.sin_port = htons (8000); Server_ Service.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); if (Bind (server, (sockaddr*) &server_service,sizeof (server_service)) = = Socket_ ERROR) {printf ("Bind Port error\r\n"); return 0;} if (listen (server,5) = = Socket_error) {printf ("Listen client error\r\n"); return 0;} SOCKET acceptsocket;printf ("Wait Accept recv data\r\n"); while (1) {acceptsocket = Socket_error;while (AcceptSocket = = SOCKET_ERROR) {AcceptSocket = accept (server,null,null);} printf ("Client connetcd\r\n "); server = Acceptsocket;break;} Char sendbuf[1024] = {0};D word dwread; BOOL Bret;char filename[] = "Test.exe"; HANDLE hfile = Createfilea (filename,generic_read,0,0,open_existing,file_attribute_normal,0); while (true) {bret = ReadFile (Hfile,sendbuf,1024,&dwread,null); if (bret = = False) {printf ("read file error\r\n"); else if (Dwread = = 0) {printf ("Send file OK"); Else{send (server,sendbuf,dwread,0);}} CloseHandle (hfile); WSACleanup (); return 0;}

  

CLIENT:

Send_client.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <stdio.h> #inc Lude <WinSock2.h> #pragma comment (lib, "Ws2_32.lib") int main () {wsadata Wsadata;int Iresult = WSAStartup (Makeword ( 2,2), &wsadata); if (iresult! = no_error) {printf ("Errpr at wsastartup\r\n");} Socket client;client = socket (af_inet,sock_stream,ipproto_tcp), if (client = = Invalid_socket) {printf ("Error at socket () :%ld\r\n ", WSAGetLastError ()); WSACleanup (); return 0;} sockaddr_in clientservice;clientservice.sin_family = Af_inet;clientservice.sin_port = htons (8000); ClientService.sin _addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); if (Connect (client, (sockaddr*) &clientservice,sizeof (clientservice)) = = Socket_ ERROR) {printf ("faied to error\r\n"); WSACleanup (); return 0;} Char recvbuf[1024] = {0};int Read;dword dwwrite; BOOL Bret;char filename[] = "Test.exe"; HANDLE hfile = Createfilea (filename,generic_write,0,0,create_always,file_attribute_normal,0); while (true) {read = Recv (client,recvbuf,1024,0); if (read ==-1) {break;} BRet = WriteFile (Hfile,recvbuf,read,&dwwrite,null), if (BRet = = FALSE) {MessageBoxW (null,_t ("Write buffer error\r\n "), NULL,MB_OK);}} MessageBoxW (null,_t ("Write buffer susccess\r\n"), NULL,MB_OK); return 0;}

Before sending a file, we can use GetFileSize to get the size of the file open.

WINSOCK Transfer File

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.