Click here to Skip to main content
13,291,690 members (61,636 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 3 Apr 2006

C++ Winsock Client To Server File Transfer - Made Easy

, 23 Aug 2006
Rate this:
Please Sign up or sign in to vote.
Easy way implement winsock c++ code for file transfering.

Sample Image - WSFileTransfer.jpg


I was searching for a file transfer program using winsock with tcp and udp. I found some code which are complex and most of them are MFC based. So problem was to convet it to a Non-MFC based program.

Another point is, the code should also be compatible with Linux sockets sys/socket.h
All my code functions is compatible to Gnu's gcc compiler except some error handling part.

I had found most project here are quite complex to understand for beginner like me. So, I collected easy examples specially from MSDN and created a simple project.

It is so simple - That people will say - its just a damn child code. Nothing more than that.

I have avoided writing many comments, so its easy to see the code steps. This is wrong I know, but this is the way I write programs. I am also very lazy to do such things.

Gradually I will comment all my codes.

This is a program which has implemented Winsock 2.0 - has a utility class WComm which has very simple methods to create client/server program as well as File Transfer Utility.


Given below are very simple steps in very simple words to start with a client/server winsock application with winsock.

Creating VC++ Project

  • Open a new windows console project.
  • Add the wcomm.cpp and wcomm.h files to it.
  • Add ws2_32.lib to Project Settings -> Link -> Object/library modules.

The Server

  • Just call startServer(port_no) to start the server at port_no
  • waitForClient() method waits for a new client and exists the function when a new client connects.
  • recvData(rec_buffer,buffer_size) receives character array sent by client. It return value is the number of bytes sent. If its -1, it means connection is closed.
  • sendData(send_buffer) sends character array to client. It retuns the number of bytes sent to client.

The Client

  • connectServer(ip_addr,port) connects the server at ip_addr at the given port
  • sending and receiving are similar to that of server.

File Transfer

  • On client side: There are 2 steps to be followed.
    1. SEND/RECEIVE Command Pair To Inform Server To Be Ready. Example:
    2. Call the File Send Command fileSend(absolute_file_fpath)
  • On server side: Call fileReceive(fname). The file is saved where the server is running and the filename is known from passed fname char[] argument.

Important Note

  • After each sendData, there has to be a recvData and after each recvData, there has to be a sendData for smooth transfer of data.
  • Please implement all the error checks as far as possible. I have avoided it - to make the codes more readable. Also Refer to Winsock MSDN Reference. There is Windows Sockets Error Codes link. Use WSAGetLastError() To find last winsock related errors. Socket errors are vital and should be handled nicely.
  • I have use direct numeric values instead of #define constants in most places. According to your ways, use Find-Replace to replace with some nice constants.


This sample project has a main program which implements both client and server according to the argument passed.

In the server code, there is a loop which listens for a client and when it gets the client connection, it moves to another loop - where it gets the client responses.

Client code is very simple - just connects to data, sends,receives, and ends.

Actually everything is very simple. One might see that the code is not good in error handling. This is because - its just made to make it more readable. This is the starting point. Now go ahead and implement whatever you want with it.

Sample Main.CPP From Src

#include "wcomm.h"

void runclient(char *ip, char *fpath);
void runserver();

WComm w;

void main(int argc, char *argv[])
	else runclient(argv[1],argv[2]);

void runserver()
	// Start Server Daemon
	printf("Server Started........\n");
    while (TRUE) {
		// Wait until a client connects
		printf("Client Connected......\n");

		// Work with client
			char rec[50] = "";

				char fname[32] ="";
				printf("File Received.........\n");
			printf("Connection Ended......\n");
		// Disconnect client

void runclient(char *ip, char *fpath)
	char rec[32] = "";
	// Connect To Server
	printf("Connected to server...\n");
	// Sending File
	w.sendData("FileSend");	w.recvData(rec,32);
	printf("File Sent.............\n");
	// Send Close Connection Signal
	printf("Connection ended......\n");

Hope you will do much better than what I did.... :-)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
Hi, I am a simple programmer and try to make things as simple as possible.

I code in c++, vb, perl etc. and try making simple objects out of things which look very hard

You may also be interested in...

Comments and Discussions

Questionerror libcid.lib !! Pin
Member 122377474-Jan-16 0:50
memberMember 122377474-Jan-16 0:50 
QuestionDev C++ Compile Problem Pin
Member 1200394522-Sep-15 9:27
memberMember 1200394522-Sep-15 9:27 
QuestionHow to send big files? Pin
Member 1176541623-Jun-15 4:47
memberMember 1176541623-Jun-15 4:47 
AnswerRe: How to send big files? Pin
Member 1176541623-Jun-15 5:45
memberMember 1176541623-Jun-15 5:45 
GeneralMy vote of 5 Pin
Member 432084412-Jun-13 16:10
memberMember 432084412-Jun-13 16:10 
QuestionMy point Pin
Member 963288513-Jan-13 9:18
memberMember 963288513-Jan-13 9:18 
-10/10 Thumbs Up | :thumbsup:

Your code answered all questions I had about c++ server programming in only 10 minutes.
Keep it up!
QuestionStrange Error Pin
Join Us21-Feb-12 7:36
memberJoin Us21-Feb-12 7:36 
AnswerRe: Strange Error Pin
CliveChu4-Jun-12 19:51
memberCliveChu4-Jun-12 19:51 
QuestionGreat example and exactly what I need Pin
aindo15-Jul-11 0:50
memberaindo15-Jul-11 0:50 
QuestionTesting of the code Pin
Sameen Maruf17-Jun-11 19:34
memberSameen Maruf17-Jun-11 19:34 
Generalneed urgent help Pin
jas_citrus25-May-11 2:25
memberjas_citrus25-May-11 2:25 
GeneralDo loop Pin
felixding24-May-11 17:58
memberfelixding24-May-11 17:58 
QuestionHow to link WS2_32.lib in VS2008 for Newbies Pin
inkonapin12-Mar-11 18:39
memberinkonapin12-Mar-11 18:39 
GeneralMy vote of 4 Pin
Gokulnath00726-Dec-10 23:52
memberGokulnath00726-Dec-10 23:52 
GeneralMy vote of 2 Pin
dio_raja30-Oct-10 20:00
memberdio_raja30-Oct-10 20:00 
Questionwhat is the exxcute file and URL Pin
cugxlj3-Sep-10 0:03
membercugxlj3-Sep-10 0:03 
GeneralUpdate Pin
Falcon4ever17-Jun-10 14:05
memberFalcon4ever17-Jun-10 14:05 
Jokeyou WIN !!! Pin
100man31-May-10 18:29
member100man31-May-10 18:29 
GeneralMissing iostream.h and fstream.h Pin
dougmasson13-Jan-10 8:22
memberdougmasson13-Jan-10 8:22 
GeneralRe: Missing iostream.h and fstream.h Pin
Member 851323228-Mar-12 4:50
memberMember 851323228-Mar-12 4:50 
GeneralChecking The file Transferred Pin
Elmer Aquino14-Oct-09 19:26
memberElmer Aquino14-Oct-09 19:26 
Generalip addressing Pin
noder29-Aug-09 16:43
membernoder29-Aug-09 16:43 
Generalit's excellent, thank u a world. Pin
cFreak27-Jul-09 9:33
membercFreak27-Jul-09 9:33 
Generalthanks Pin
cFreak12-Jul-09 23:43
membercFreak12-Jul-09 23:43 
Generallong filenames gives error to client and freezes server Pin
OwynTyler2-Jun-09 4:24
memberOwynTyler2-Jun-09 4:24 
Generalcan't compile, can't compile useing your lib in mode other than /MTd Pin
OwynTyler31-May-09 9:07
memberOwynTyler31-May-09 9:07 
GeneralPlease help Pin
johannes_dj18-Feb-09 9:23
memberjohannes_dj18-Feb-09 9:23 
GeneralRe: Please help Pin
harriccane11-Jun-09 5:15
memberharriccane11-Jun-09 5:15 
Generallinker error Pin
vidit.iitr11-Mar-08 11:08
membervidit.iitr11-Mar-08 11:08 
GeneralI no longer spent time here Pin
kbfromindia31-Aug-07 0:31
memberkbfromindia31-Aug-07 0:31 
GeneralRe: I no longer spent time here Pin
Member 38637686-Dec-07 4:04
memberMember 38637686-Dec-07 4:04 
Questiontransfered files Pin
zen.Ben27-Jun-07 15:23
memberzen.Ben27-Jun-07 15:23 
Generalerror C3861: 'atoi': identifier not found [modified] Pin
esia20-Apr-07 9:36
memberesia20-Apr-07 9:36 
Generalhelp me wid my project Pin
muzamil_mlore30-Mar-07 0:36
membermuzamil_mlore30-Mar-07 0:36 
Generaluse it in MFC Pin
lplover2k29-Mar-07 21:13
memberlplover2k29-Mar-07 21:13 
GeneralJust what I was looking for.. Pin
trojandj27-Mar-07 20:18
membertrojandj27-Mar-07 20:18 
Generalgood post hard to work out Pin
karlx6-Jan-07 1:41
memberkarlx6-Jan-07 1:41 
QuestionConverting the program to a library Pin
Pranav Lal22-Aug-06 22:56
memberPranav Lal22-Aug-06 22:56 
AnswerRe: Converting the program to a library Pin
kbfromindia23-Aug-06 2:45
memberkbfromindia23-Aug-06 2:45 
GeneralRe: Converting the program to a library Pin
Pranav Lal23-Aug-06 19:37
memberPranav Lal23-Aug-06 19:37 
Generalsuch a nice code Pin
tiki24-Jul-06 3:44
membertiki24-Jul-06 3:44 
GeneralMoron Pin
Darka3-Apr-06 23:37
memberDarka3-Apr-06 23:37 
GeneralRe: Moron Pin
Darka3-Apr-06 23:48
memberDarka3-Apr-06 23:48 
GeneralRe: Moron Pin
kbfromindia3-Apr-06 23:59
memberkbfromindia3-Apr-06 23:59 
GeneralRe: Moron Pin
Darka4-Apr-06 0:08
memberDarka4-Apr-06 0:08 
GeneralRe: Moron Pin
yafan4-Apr-06 19:18
memberyafan4-Apr-06 19:18 
GeneralRe: Moron Pin
Darka4-Apr-06 21:52
memberDarka4-Apr-06 21:52 
GeneralRe: Moron Pin
yafan5-Apr-06 13:23
memberyafan5-Apr-06 13:23 
GeneralRe: Moron Pin
sk8er_boy28713-May-07 3:04
membersk8er_boy28713-May-07 3:04 
GeneralRe: Moron Pin
delacroix21cn1-May-10 2:44
memberdelacroix21cn1-May-10 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 23 Aug 2006
Article Copyright 2006 by kbfromindia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid