Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to transfer file between client, server using winsock?
I am a beginner in c++,I write this code for the client server app but i don't know how to transfer file.
server code:
C++
<pre lang="xml">#pragma comment(lib,"ws2_32.lib")

//for basic functions
#include <sdkddkver.h>
#include <conio.h>
#include <stdio.h>

// for socket function
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
//Windows socket version
#define SCK_VERSION2 0x0202

using namespace std;
void main()
{
    char read[45];
          long data;
          WSAData wsdata;
          WORD dllversion;
          //dll version
          dllversion = MAKEWORD(1,2);
          //start winsock
          data = WSAStartup(dllversion,&wsdata);
          //create socket structure
          SOCKADDR_IN address;
          int addresssize = sizeof(address);
          //create sockets
          SOCKET sconnection;
          SOCKET slisten;

          sconnection = socket(AF_INET,SOCK_STREAM,NULL);
          address.sin_addr.s_addr = inet_addr("127.0.0.1");
          address.sin_family = AF_INET;
          address.sin_port = htons(444);

          slisten = socket(AF_INET,SOCK_STREAM,NULL);
          bind(slisten, (SOCKADDR*)&address,sizeof(address));
          listen(slisten, SOMAXCONN);

          for(;;)
          {
                    cout << "Waiting for incommining connection...."<<endl;

                if(sconnection = accept(slisten, (SOCKADDR*)&address, &addresssize))
                {
                              cout << "A connection was found..." << endl;
                              data = send(sconnection,"Welcome your connected with my server",45,NULL);
                }
          }
}


Client code:
C++
#pragma once
#pragma comment(lib,"ws2_32.lib")

//for basic functions
#include <sdkddkver.h>


// for socket function
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
//Windows socket version
#define SCK_VERSION2 0x0202

using namespace std;
void main()
{

		  long data;
		  WSAData wsdata;
		  WORD dllversion;
		  //dll version
		  dllversion = MAKEWORD(1,2);
		  //start winsock
		  data = WSAStartup(dllversion,&wsdata);
		  //create socket structure
		  SOCKADDR_IN address;
		  //create sockets
		  SOCKET sock;

		  string response;
		  string converter;
		  char message[200];

		  sock = socket(AF_INET,SOCK_STREAM,NULL);
		  address.sin_addr.s_addr = inet_addr("127.0.0.1");
		  address.sin_family = AF_INET;
		  address.sin_port = htons(444);

		  cout << "Do you want to connect with server(y/n): ";
		  cin >> response;

		  response[0] = tolower(response[0]);

		  if(response == "y" || response == "Y")
		  {
			  connect(sock,(SOCKADDR*)&address,sizeof(address));
			  data = recv(sock,message,sizeof(message),NULL);
					converter = message;
					cout << "Message from server: " << converter << endl;
					system("pause");
		  }
		  else if(response == "n" || response == "N")
		  {
					cout << "Ok you are quiting..."<<endl;
					system("pause");
		  }
		  else
		  {
					cout << "Worng input used..."<<endl;
		  }
		  cout << "\n\n\t"<<endl;
		  system("pause");
}

I tried several time to transfer file using buffer and fstream but its not make sence to me.
Posted
Updated 17-Jul-13 23:52pm
v3
Comments
Richard MacCutchan 18-Jul-13 9:22am    
It's quite a simple sequence:
1. Read some data from the file (say 512 bytes)
2. Send that to the server
3, If more data in file goto 1; else send a "complete" message.

At the server:
1. Receive data from the client.
2. If first time create a new file
3. If "complete" message then close file and quit this loop.
4. Write the data to the file.
5. Goto 1.
Alamgirable 18-Jul-13 9:41am    
how to receive complete file with declaring "char buffer[512]".
Because with this method buffer receive only 512 bytes.
How to receive full file....
Albert Holguin 18-Jul-13 11:12am    
You have to do the process over and over until you're done... otherwise you'd have to write different code for different file sizes and that doesn't make much sense now does it?
Alamgirable 18-Jul-13 12:57pm    
Can you please give me little code example....
Albert Holguin 18-Jul-13 14:37pm    
There's examples of this all over the internet...

i miss some file reading and file writing calls in your code

fopen, fread, fwrite.

Start here:

http://www.cplusplus.com/reference/cstdio/fopen/[^]
 
Share this answer
 
On server side you should:
  1. Read the file content from disk into memory.
  2. Send the memory content to the client.

On the client side you have to:
  1. Receive data from server.
  2. Write the received content (memory) to disk.

Please note server/client file content exchange needs a minimal protocol (for instance the server may send first the file lenght and then its content and eventually a checksum; the client could ACK back to the server and so on).
 
Share this answer
 
v2
Comments
Alamgirable 18-Jul-13 6:53am    
please give me a little code example,I tried these steps several times but not working.
I shell be very thankful to you.
Alamgirable 18-Jul-13 6:56am    
On server side i tried like this but the data is croupted.
data = send(sconnection,contents.c_str(),sizeof(contents),NULL);
CPallini 19-Jul-13 3:22am    
sizeof(contents) is wrong. You should use instead contents.lenght();.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900