Problems with winsock file transfer

Started by
4 comments, last by ApochPiQ 10 years, 7 months ago

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?

Advertisement

That's not really a helpful code sample since you don't show any actual API calls. It's also impossible to know if your problem is failing API calls, bad encapsulation into your socket objects or some other problem. Some things to look for: Are you checking the return values for the API calls for failure? Does your sendData() function send a terminating null for the string? Are you sure that you're accepting the connection properly?

Other than that I'd recommend posting a complete, but minimal code sample that demonstrates your problem.

edited the first post

It would also be helpful if you, y'know, described what's wrong.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I didn't really have a lot of time to debug,it just connects to the socket and then nothing

Here's some questions that might help illuminate your problem for you:

1. How many bytes are in the C string "test" (not including quotes)?
2. How many bytes will strlen("test") give you?
3. What happens if you send strlen("test") bytes, receive strlen("test") bytes, and then try to use the result as a C string?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement