![]() |
Languages »
C / C++ Language »
General
Intermediate
C++ Winsock Client To Server File Transfer - Made EasyBy kbfromindiaEasy way implement winsock c++ code for file transfering. |
C++, Windows, Visual Studio, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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.
#include "wcomm.h" void runclient(char *ip, char *fpath); void runserver(); WComm w; void main(int argc, char *argv[]) { if(argc==1)runserver(); else runclient(argv[1],argv[2]); } void runserver() { // Start Server Daemon w.startServer(27015); printf("Server Started........\n"); while (TRUE) { // Wait until a client connects w.waitForClient(); printf("Client Connected......\n"); // Work with client while(TRUE) { char rec[50] = ""; w.recvData(rec,32);w.sendData("OK"); if(strcmp(rec,"FileSend")==0) { char fname[32] =""; w.fileReceive(fname); printf("File Received.........\n"); } if(strcmp(rec,"EndConnection")==0)break; printf("Connection Ended......\n"); } // Disconnect client w.closeConnection(); } } void runclient(char *ip, char *fpath) { char rec[32] = ""; // Connect To Server w.connectServer(ip,27015); printf("Connected to server...\n"); // Sending File w.sendData("FileSend"); w.recvData(rec,32); w.fileSend(fpath); printf("File Sent.............\n"); // Send Close Connection Signal w.sendData("EndConnection");w.recvData(rec,32); printf("Connection ended......\n"); }
Hope you will do much better than what I did.... :-)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Aug 2006 Editor: |
Copyright 2006 by kbfromindia Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |