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:
<pre lang="xml">#pragma comment(lib,"ws2_32.lib")
#include <sdkddkver.h>
#include <conio.h>
#include <stdio.h>
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#define SCK_VERSION2 0x0202
using namespace std;
void main()
{
char read[45];
long data;
WSAData wsdata;
WORD dllversion;
dllversion = MAKEWORD(1,2);
data = WSAStartup(dllversion,&wsdata);
SOCKADDR_IN address;
int addresssize = sizeof(address);
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:
#pragma once
#pragma comment(lib,"ws2_32.lib")
#include <sdkddkver.h>
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
#define SCK_VERSION2 0x0202
using namespace std;
void main()
{
long data;
WSAData wsdata;
WORD dllversion;
dllversion = MAKEWORD(1,2);
data = WSAStartup(dllversion,&wsdata);
SOCKADDR_IN address;
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.