I'm trying to send a file with winsock.The file is Test.txt and contains the text Heya
However the end result is a file called:
Test.txt4heyaEndConnection
EndConnection is the string that gets sent to the server when the transfer was finished.And 4,I think it's the number of total bytes.
The file is empty however,and I can't find why.
In my code you will see a variable w,that is a variable of type WComm,here's the class + implementation:
class WComm
{
public:
int sendData(char*);
int recvData(char*,int);
void fileSend(char*);
void fileReceive(char*);
};
WSADATA wsaData;
SOCKET m_socket;
SOCKET m_backup;
sockaddr_in con;
SOCKET AcceptSocket;
int WComm::sendData(char *sendbuf)
{
return send( m_socket, sendbuf, strlen(sendbuf), 0 );
}
int WComm::recvData(char *recvbuf,int size)
{
int sz = recv( m_socket, recvbuf, size, 0 );
recvbuf[sz] = '\0';
return sz;
}
void WComm::fileReceive(char *filename)
{
char rec[50] = "";
recv( m_socket, filename, 32, 0 );
send( m_socket, "OK", strlen("OK"), 0 );
FILE *fw = fopen(filename, "wb");
int recs = recv( m_socket, rec, 32, 0 );
send( m_socket, "OK", strlen("OK"), 0 );
rec[recs]='\0';
int size = atoi(rec);
while(size > 0)
{
char buffer[1030];
if(size>=1024)
{
recv( m_socket, buffer, 1024, 0 );
send( m_socket, "OK", strlen("OK"), 0 );
fwrite(buffer, 1024, 1, fw);
}
else
{
recv( m_socket, buffer, size, 0 );
send( m_socket, "OK", strlen("OK"), 0 );
buffer[size]='\0';
fwrite(buffer, size, 1, fw);
}
size -= 1024;
}
fclose(fw);
}
void WComm::fileSend(char *fpath)
{
// Extract only filename from given path.
char filename[50];
int i=strlen(fpath);
for(;i>0;i--)if(fpath[i-1]=='\\')break;
for(int j=0;i<=(int)strlen(fpath);i++)filename[j++]=fpath[i];
////////////////////////////////////////
ifstream myFile (fpath, ios::in|ios::binary|ios::ate);
int size = (int)myFile.tellg();
myFile.close();
char filesize[10];itoa(size,filesize,10);
send( m_socket, filename, strlen(filename), 0 );
char rec[32] = "";recv( m_socket, rec, 32, 0 );
send( m_socket, filesize, strlen(filesize), 0 );
recv( m_socket, rec, 32, 0 );
FILE *fr = fopen(fpath, "rb");
while(size > 0)
{
char buffer[1030];
if(size>=1024)
{
fread(buffer, 1024, 1, fr);
send( m_socket, buffer, 1024, 0 );
recv( m_socket, rec, 32, 0 );
}
else
{
fread(buffer, size, 1, fr);
buffer[size]='\0';
send( m_socket, buffer, size, 0 );
recv( m_socket, rec, 32, 0 );
}
size -= 1024;
}
fclose(fr);
}
The server code:
//Consider that the server is already connected with the //client at this stage
while(1){
char rec[50] = "";
w.recvData(rec,32);
cout << "data received" << endl;
if(strcmp(rec,"FileSend")==0)
{
cout << "FileSend received" << endl;
w.sendData("OK");
char fname[32] ="";
w.fileReceive(fname);
printf("File Received.........\n");
}
The client(which sends the file to the server):
//w is a global variable already connected to the server
void runclient(char *fpath)
{
char rec[32] = "";
// 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");
}
int main(){
char* path = "C:\\Test.txt";
runclient(path);
}
Some help?