problem with file tranfser

I created a client and server application. This all works fine but now i want to add file transfer code. so the client has a GUI form with a listbox that displays a list of servers because they might be more than more running. The user selects a server from the list and connects to it, and will send the file. But no file is appearing in the server's folder.

client code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void SendUML()
{
	FILE *fp;
	char buf[4096];
	fp = fopen("UAM.UML", "r");
	fseek(fp, 0L, SEEK_END);
	int sz = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	int n_size;
	int header=99999;
	send(ServSock,(const char*)&header,sizeof(int),0);
	send(ServSock,(const char*)&sz,sizeof(int),0);
	char temp[60];
	sprintf(temp,"Client-%d",sz);
	OutputDebugString(temp);

	while( 0 < (n_size = fread( buf, 1, 4096, fp)))
    {
       send(ServSock,buf,4096,0);
    }                            
	fclose(fp);
}


server code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
SOCKADDR_IN SenderInfo;
SOCKET NewConnection;

int ClientLen = sizeof(SOCKADDR_IN);
SOCKADDR_IN ClientAddr;

int ByteReceived, nlen;
char recvbuff[1024];

NewConnection = SOCKET_ERROR;
if(NewConnection == SOCKET_ERROR) 
{

	NewConnection = accept(ListeningSocket, NULL, NULL);	

	ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

	if (ByteReceived > 0) 
	{
		getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));

		memset(&SenderInfo, 0, sizeof(SenderInfo));
		nlen = sizeof(SenderInfo);

		getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);	
		char temp[10];
		sprintf(temp,"service: Header- %d",Header);
		OutputDebugStringA(temp);
		if(Header == 99999)
		{
			int size = 0;
			char buf[4096];
			recv(NewConnection, (char*)&size, sizeof(int), 0);
			sprintf(temp,"service: Filesize- %d",size);
			OutputDebugStringA(temp);
			int recvbyte = 0,nsum = 0;
			FILE* fp = fopen("UAM_.UML","w");
			while(1)
			{
				recvbyte = recv(NewConnection, buf,4096,0);
				if(recvbyte == 0)
					break;
				fwrite(buf,1,recvbyte,fp);
				nsum += recvbyte;
				if(nsum >= size)
					break;
			}
			fclose(fp);
		}
	}

}
Last edited on
There're several possible problems:

Is the socket NewConnection not blocking? If so on line 41 (33) you might very well receive 0. So better not break; instead just don't write.

you didn't specify a path to the file. May be the file appears somewhere you don't expect?
So better not break; instead just don't write.
so i should remove the break and fwrite line?

i did a search for the file and i can't find it.
should i change the line FILE* fp = fopen("UAM_.UML","w"); to specify a path?
Last edited on
first of all: Are you sure that you get int to the if(Header == 99999)

so i should remove the break and fwrite line?
No, only the break;:
1
2
3
4
5
6
7
8
9
10
				recvbyte = recv(NewConnection, buf,4096,0);
				if(recvbyte == 0)
					; // Note: no break!
else
{
				fwrite(buf,1,recvbyte,fp);
				nsum += recvbyte;
				if(nsum >= size)
					break;
}


should i change the line FILE* fp = fopen("UAM_.UML","w"); to specify a path?
Yes, for now you can specify a fixed path. So you know where the file appears if. Later you may want to get this path from somwhere else to make it more flexible.
first of all: Are you sure that you get int to the if(Header == 99999)
yes you're right. i don't get into the if statement. i debugged and found that header is 0

Last edited on
which means the problem must be in the client application because it is not sending the header data to the server
which means the problem must be in the client application because it is not sending the header data to the server
No, it's on the server side.

Where on the server side do you change the value of Header?
It should happen after if (ByteReceived > 0).
I can't even see where Header is declared. It can be very well a local variable.

The important thing is: It has to be extracted from recvbuff
I can't even see where Header is declared. It can be very well a local variable.
sorry forgot to copy over that part. its just above the server code i put in the first post int Header = 0;

so i should move if(Header == 99999) after if (ByteReceived > 0)?
you problem is that you don't set the value for Header.

One way to get this value is

Header = *reinterpret_cast<int *>(recvbuff);

NOTE: Be aware that ByteReceived >= sizeof(int)
Topic archived. No new replies allowed.