CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2004
    Posts
    118

    Question winsock client/server, download working, upload not

    hey.. here i have a client/server example..

    the client can do something like this, for example

    client 192.168.1.103 upload c:\filetosend.dat c:\remotefilename.dat

    client 192.168.1.103 download c:\locafilename.dat c:\filetogetfromremote.dat

    the uploading has a problem though, the file never gets completely sent. it's seems to skip some of the early bytes in the file. for example if i send an .exe file the .exe headers are cut off somewhat... its as if it forgot to send the MZ at the beginning of the file and some other stuff after that. the upload never fails by that much; it seems like usually under 500 bytes are missing. obviously this is bad, but it appears their must be some minor bug in the code im not seeing.

    the download seems to work perfect, however.
    note: i have only tested the download and upload on my 192.168.1.103 at home in the network. for example, if i use the client from my machine on .102 and the server on the .103 machine.

    client code:
    Code:
    #include <windows.h>
    #include <winsock2.h>
    #include <stdio.h>
    SOCKET m_socket;
    int main(int argc,char **argv)
    {
    	if(argc<2)
    	{
    		return 0;
    	}
    	WSADATA wsaData;
    	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    	{
    		return 0;
    	}
    	if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
    	{
    		WSACleanup();
    		return 0;
    	}
    	m_socket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(m_socket==INVALID_SOCKET)
    	{
    		WSACleanup();
    		return 0;
    	}
    	sockaddr_in clientService;
    	struct hostent *hnet;
    	hnet=gethostbyname(argv[1]);
    	if(hnet==0)
    	{
    		printf("%s\n","Couldn't find address");
    		closesocket(m_socket);
    		WSACleanup();
    		return 0;
    	}
    	clientService.sin_family=AF_INET;
     	clientService.sin_addr=*(struct in_addr *)hnet->h_addr_list[0];
    	clientService.sin_port=htons(60006);
    	if(connect(m_socket,(SOCKADDR *)&clientService,sizeof(clientService))!=0)
    	{
    		closesocket(m_socket);
    		WSACleanup();
    		printf("%s\n","Connection failed");
    		return 0;
    	}
    	int opLen=lstrlen(argv[2]);
    	int bytesSent=send(m_socket,argv[2],opLen+1,0);
    	if(bytesSent<0)
    	{
    		closesocket(m_socket);
    		WSACleanup();
    		printf("%s\n","Sending core operation failed");
    		return 0;
    	}
    	if((argc==5)&&(strcmp(argv[2],"download")==0))
    	{
    		int lenremote=lstrlen(argv[4]);
    		bytesSent=send(m_socket,argv[4],lenremote+1,0);
    		if(bytesSent<0)
    		{
    			closesocket(m_socket);
    			WSACleanup();
    			printf("%s\n","Sending download parameter 2 failed");
    			return 0;
    		}
    		HANDLE h=CreateFile(argv[3],GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    		if(h==INVALID_HANDLE_VALUE)
    		{
    			closesocket(m_socket);
    			WSACleanup();
    			printf("%s\n","Invalid save path for download parameter 1");
    			return 0;
    		}
    		int i,j;
    		char buffer[4096];
    		DWORD dw;
    		for(i=0;;)
    		{
    			j=recv(m_socket,buffer,sizeof(buffer),0);
    			if(j<=0)
    			{
    				CloseHandle(h);
    				closesocket(m_socket);
    				WSACleanup();
    				return 0;
    			}
    			i+=j;
    			WriteFile(h,buffer,j,&dw,0);
    		}
    		return 0;
    	}
    
    	if((argc==5)&&(strcmp(argv[2],"upload")==0))
    	{
    		int lensaveto=lstrlen(argv[4]);
    		bytesSent=send(m_socket,argv[4],lensaveto+1,0);
    		if(bytesSent<0)
    		{
    			closesocket(m_socket);
    			WSACleanup();
    			printf("%s\n","Sending upload parameter 2 failed");
    			return 0;
    		}
    		FILE *fptr=fopen(argv[3],"rb");
    		if(fptr==0)
    		{
    			closesocket(m_socket);
    			WSACleanup();
    			printf("%s\n","Invalid save path for upload parameter 1");
    			return 0;
    		}
    		int i,end,j;
    		char buffer[4096];
     		for(i=0,end=0;;)
    		{
    			j=fread(buffer,1,sizeof(buffer),fptr);
    			if(j<0)
    			{
    				fclose(fptr);
    				closesocket(m_socket);
    				WSACleanup();
    				return 0;
    			}
    			if(j==0)
    			{
    				end=1;
    				fclose(fptr);
    				closesocket(m_socket);
    				WSACleanup();
    				printf("%s\n","Success");
    				return 0;
    			}
    			int iSent=0,iRet=0;
    			while(iSent<j)
    			{
    				iRet=send(m_socket,buffer+iSent,j-iSent,0);
    				if(iRet<=0)
    				{
    					fclose(fptr);
    					closesocket(m_socket);
    					WSACleanup();
    					printf("%s\n","Send failed");
    					return 0;
    				}
    				iSent+=iRet;
    			}
    		}
    	}
    	printf("%s\n","Invalid command");
    	return 0;
    }
    server code:
    Code:
    #include <windows.h>
    #include <winsock2.h>
    #include <stdio.h>
    char operation[1024];
    char fopbuffer[MAX_PATH];
    void operationListen(int insock);
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
    	WSADATA wsaData;
    	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    	{
    		return 1;
    	}
    	if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
    	{
    
    		return 1;
    	}
    	SOCKET oursock=socket(AF_INET,SOCK_STREAM,0);
    	if(oursock==INVALID_SOCKET)
    	{
    
    		return 1;
    	}
    	sockaddr_in local;
    	sockaddr_in from;
    	local.sin_family=AF_INET;
    	local.sin_addr.s_addr=INADDR_ANY;
    	local.sin_port=htons(60006);
    	if(bind(oursock,(sockaddr *)&local,sizeof(local))!=0)
    	{
    
    		return 1;
    	}
    	if(listen(oursock,1)!=0)
    	{
    
    		return 1;
    	}
    	int sz=sizeof(struct sockaddr_in);
    	while(1)
    	{
        	SOCKET socknew=accept(oursock,(struct sockaddr *)&from,&sz);
    		operationListen(socknew);
    		Sleep(1000);
    	}
    	return 0;
    }
    void operationListen(int insock)
    {
    	FillMemory(operation,1024,0x0);
    	int bytesRecv=recv(insock,operation,1024,0);
    	if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
    	{
    		closesocket(insock);
    		return;
    	}
    	if(strcmp(operation,"download")==0)
    	{
    		FillMemory(fopbuffer,MAX_PATH,0x0);
    		bytesRecv=recv(insock,fopbuffer,MAX_PATH,0);
    		if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
    		{
    			closesocket(insock);
    			return;
    		}
    		FILE *fptr=fopen(fopbuffer,"rb");
    		if(fptr==0)
    		{
    			closesocket(insock);
    			return;
    		}
    		int i,end,j;
    		char buffer[4096];
     		for(i=0,end=0;;)
    		{
    			j=fread(buffer,1,sizeof(buffer),fptr);
    			if(j<0)
    			{
    				fclose(fptr);
    				closesocket(insock);
    				return;
    			}
    			if(j==0)
    			{
    				end=1;
    				fclose(fptr);
    				closesocket(insock);
    				return;
    			}
    			int iSent=0,iRet=0;
    			while(iSent<j)
    			{
    				iRet=send(insock,buffer+iSent,j-iSent,0);
    				if(iRet<=0)
    				{
    					fclose(fptr);
    					closesocket(insock);
    					return;
    				}
    				iSent+=iRet;
    			}
    		}
    		return;
    	}
    	if(strcmp(operation,"upload")==0)
    	{
    		FillMemory(fopbuffer,MAX_PATH,0x0);
    		bytesRecv=recv(insock,fopbuffer,MAX_PATH,0);
    		if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
    		{
    			closesocket(insock);
    			return;
    		}
    		HANDLE h=CreateFile(fopbuffer,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    		if(h==INVALID_HANDLE_VALUE)
    		{
    			closesocket(insock);
    			return;
    		}
    		if(SetFilePointer(h,0,0,FILE_END)==0xffffffff)
    		{
    			CloseHandle(h);
    			closesocket(insock);
    			return;
    		}
    		int i,j;
    		char buffer[4096];
    		DWORD dw;
    		for(i=0;;)
    		{
    			j=recv(insock,buffer,sizeof(buffer),0);
    			if(j<=0)
    			{
    				break;
    			}
    			i+=j;
    			WriteFile(h,buffer,j,&dw,0);
    		}
    		CloseHandle(h);
    		closesocket(insock);
    		return;
    	}
    	closesocket(insock);
    	return;
    }
    i have absolutely been pulling my hair out trying to get this thing working properly.. stayed up almost all nite; probably about 10 hours on this :X

    but hey,thats what its like sometimes=)

    so yeah any help/advice would be awesome.

    thanks!
    -avery

  2. #2
    Join Date
    Mar 2005
    Posts
    137

    Re: winsock client/server, download working, upload not

    Did you try sending a smaller size buffer? On Ethernet (and most other networks) maximum possible size transferrable at any given time is 1500 bytes. It is called as Link MTU (Maximum Transmission Unit). Any data that execeeds this MTU will be fragmented by IP layer. So its in your best interest to send the data fitting the MTU which is (MTU - (sizeof(IP header(20 bytes)) + sizeof(TCP header(20)) + sizeof(ethernet header (16 bytes)) = 1444 bytes).

    Following proper buffer size may avoid unnecessary fragmentation.

  3. #3
    Join Date
    Sep 2004
    Posts
    118

    Re: winsock client/server, download working, upload not

    hey, still the same problems.. i tried making the buffer sizes 1024 and same issue.. no matter what file i try upload its always missing some bytes at the beginning..

    i have turned off all optimizations in devc++ too just to be safe.. no luck though.

    i have no firewall running or anything. the other machine is running win98 i dont know why that would matter though..

    not sure whats going on here

    **edit** i tried compiling the same program in visual c++ 2003 prof. and same result as expected (just wanted to make sure not a compiler issue)
    Last edited by c0ldshadow; February 2nd, 2006 at 11:15 PM.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: winsock client/server, download working, upload not

    Your problem may be that you are looping and trying to change the pointer on the send buffer manually. This is un-necessary, since the pointer will move by its self, even with non-blocking sockets. With Blocking sockets, it will not be necessary to loop.

    Code:
    			
                            while(iSent<j)
    			{
    				iRet=send(m_socket,buffer+iSent,j-iSent,0);
    				if(iRet<=0)
    				{
    					fclose(fptr);
    					closesocket(m_socket);
    					WSACleanup();
    					printf("%s\n","Send failed");
    					return 0;
    				}
    				iSent+=iRet;
    			}
    So change your send to something like:
    Code:
    	iRet=send(m_socket,buffer,j,0);
    Additionally in my own experience, bytes missing are usually caused by simple miscounting or not sending the same number of bytes that you read from the buffer or write to the buffer because of an error in counting and logic. I believe I found one. I recently ran into an error like this where a few bytes were being cut off.

    Exhaustively check your logic to see if you are reading, sending, recieving and writing the same number of bytes.

    HTH,

    ahoodin
    If this helped, dont forget to rate.
    **Good things come to those who rate**
    Last edited by ahoodin; February 3rd, 2006 at 09:23 AM.

  5. #5
    Join Date
    Sep 2004
    Posts
    118

    Re: winsock client/server, download working, upload not

    ahoodin, i tried applying that change but the same thing is still happening=(

    when i upload the file to the remote machine the file on the remote machine is always missing bytes at the beginning of the file...

  6. #6
    Join Date
    Sep 2004
    Posts
    118

    Re: winsock client/server, download working, upload not

    ol' scorpion seems to have found the problem.. here is what he explained in my conversation with him:

    Code:
    scorp: by the way, why are you doing send() in a loop in your client
    scorp: also, gotos are a good thing :-)
    scorp: and you forgot to fclose() your file after the end in a normal operation
    scorp: also, you don't need to send in a loop.
    scorp: while(iSent<j) 			{ 				iRet=send(m_socket,buffer+iSent,j-iSent,0); 				if(iRet<=0) 				{
    scorp: that's bogus
    scorp: just do a nice iRet = send(m_socket, buffer, j, 0);
    scorp: it'll send j bytes
    scorp: you let the kernel worry if it'll break it up into chunks or not.
    scorp: that's not your concern
    scorp: also I found the error!
    scorp: it is in your operationListen()
    scorp: 	FillMemory(operation,1024,0x0); 	int bytesRecv=recv(insock,operation,1024,0);
    scorp: guess what happens here :-)
    scorp: hint:
    scorp: how many bytes are received here
    scorp: int bytesRecv=recv(insock,operation,1024,0);
    scorp: 1024
    scorp: the first few bytes are:
    scorp: ['d' 'o' 'w' 'n' 'l' 'o' 'a' 'd' '\0' "first xxx bytes of the file"]
    scorp: now this will work
    if(strcmp(operation,"download")==0)
    scorp: because of the \0 in the string
    scorp: but that doesn't mean the remaining stuff isn't in that array :-)
    scorp: hehe pl. post on the forum that ol' scorps found it :-)
    scorp: the trick is to write out the remaining data in operation first
    scorp: then write out the rest
    i will post back once i have the code changed around and working..
    here is scropions web page if any of you all want to check it out

    http://mayukhbose.com

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: winsock client/server, download working, upload not

    ol scorpion is not correct on a few points:
    Code:
    scorp: just do a nice iRet = send(m_socket, buffer, j, 0);
    scorp: it'll send j bytes
    That's not correct all the time. send() might send j bytes, but it might not. It always sends iRet bytes, though, which is why you use the returned value from send() and not the value you asked it to send.

    Likewise,
    Code:
    scorp: how many bytes are received here
    scorp: int bytesRecv=recv(insock,operation,1024,0);
    scorp: 1024
    Again, recv() might receive 1024 bytes, but it might not. It tells you how many bytes it received, which is why we use the bytesRecv value that's returned.

    On the other hand, he is completely correct when he is telling you that you are throwing away the first part of your tranferred file. Here's your code:
    Code:
    void operationListen(int insock)
    {
    	FillMemory(operation,1024,0x0);
    	int bytesRecv=recv(insock,operation,1024,0);  // first recv()
    	if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
    	{
    		closesocket(insock);
    		return;
    	}
    	if(strcmp(operation,"download")==0)
    	{
    		FillMemory(fopbuffer,MAX_PATH,0x0);
    		bytesRecv=recv(insock,fopbuffer,MAX_PATH,0);  // second recv()
    	// .. etc
    In your first recv(), you are also recieving the first few bytes of your file. You discard them completely in your second recv().

    That's why I told you in your other thread (at http://www.codeguru.com/forum/showthread.php?t=373503 ) that "you must also preserve any extra data that might have been received so that you can use it subsequently when writing your received file's data."

    Mike

  8. #8
    Join Date
    Sep 2004
    Posts
    118

    Re: winsock client/server, download working, upload not

    i restructured both the client and the server a bit..

    MikeAThon, you would advise that i put back the while loop for the sending, right?
    its working as it is, but my guess is that its better to keep the loop based on what you're saying..

    example usages:
    client 192.168.1.103 "upload&c:/filetosend.dat&c:/remotesaved.dat"
    client 192.168.1.103 "download&c:/wheretosave.dat&c:/whattoget.dat"

    here is the updated code for both of them

    client:
    Code:
    #include <windows.h>
    #include <stdio.h>
    char buffer[1024];
    int main(int argc,char **argv)
    {
    	bool clean1=false;
    	bool clean2=false;
    	bool clean3=false;
    	bool clean4=false;
    	if(argc!=3)
    	{
    		return 0;
    	}
    	WSADATA wsaData;
    	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    	{
    		return 0;
    	}
    	if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
    	{
    		clean1=true;
    		goto cleanup;
    	}
    	SOCKET m_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(m_socket==INVALID_SOCKET)
    	{
    		clean1=true;
    		goto cleanup;
    	}
    	sockaddr_in clientService;
    	struct hostent *hnet;
    	hnet=gethostbyname(argv[1]);
    	if(hnet==0)
    	{
    		printf("%s\n","Couldn't find address");
    		clean2=true;
    		goto cleanup;
    	}
    	clientService.sin_family=AF_INET;
     	clientService.sin_addr=*(struct in_addr *)hnet->h_addr_list[0];
    	clientService.sin_port=htons(60006);
    	if(connect(m_socket,(SOCKADDR *)&clientService,sizeof(clientService))!=0)
    	{
    		printf("%s\n","Connection failed");
    		clean2=true;
    		goto cleanup;
    	}
    	FILE *fptr=0;
    	HANDLE h=0;
    	int opLen=lstrlen(argv[2]);
    	bool download=false;
    	bool upload=false;
    	bool enumdrive=false;
    	if(strncmp(argv[2],"download",8)==0)
    	{
    		if(opLen<18)
    		{
    			clean2=true;
    			goto cleanup;
    		}
    		download=true;
    		goto nextstep;
    	}
    	if(strncmp(argv[2],"upload",6)==0)
    	{
    		if(opLen<16)
    		{
    			clean2=true;
    			goto cleanup;
    		}
    		upload=true;
    		goto nextstep;
    	}
    
    
    
    	nextstep:
    	char *clone=new char[opLen+1];
    	strncpy(clone,argv[2],opLen);
    	clone[opLen]='\0';
    	for(int c=0;c<opLen;++c)
    	{
    		if(clone[c]=='&')
    		{
    			clone[c]='\0';
    		}
    	}
    	int bytesSent=send(m_socket,clone,opLen+1,0);
    	if(bytesSent<=0)
    	{
    		printf("%s\n","Sending core operation failed");
    		clean2=true;
    		goto cleanup;
    	}
    	if(download==true)
    	{
    		h=CreateFile(&clone[9],GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    		if(h==INVALID_HANDLE_VALUE)
    		{
    			clean2=true;
    			goto cleanup;
    		}
    		clean3=true;
    		int i,j;
    		DWORD dw;
    		for(i=0;;)
    		{
    			j=recv(m_socket,buffer,sizeof(buffer),0);
    			if(j<=0)
    			{
    				goto cleanup;
    			}
    			i+=j;
    			if(WriteFile(h,buffer,j,&dw,0)==0)
    			{
    				goto cleanup;
    			}
    		}
    		goto cleanup;
    	}
    	if(upload==true)
    	{
    		fptr=fopen(&clone[7],"rb");
    		if(fptr==0)
    		{
    			clean2=true;
    			goto cleanup;
    		}
    		clean4=true;
    		int i,end,j;
     		for(i=0,end=0;;)
    		{
    			j=fread(buffer,1,sizeof(buffer),fptr);
    			if(j<0)
    			{
    				goto cleanup;
    			}
    			if(j==0)
    			{
    				end=1;
    				goto cleanup;
    			}
    			int iRet=send(m_socket,buffer,j,0);
    			if(iRet<=0)
    			{
    				goto cleanup;
    			}
    		}
    		goto cleanup;
    	}
    
    	cleanup:
    	if(clean1==true)
    	{
    		WSACleanup();
    		return 0;
    	}
    	if(clean2==true)
    	{
    		closesocket(m_socket);
    		WSACleanup();
    		delete[]clone;
    		return 0;
    	}
    	if(clean3==true)
    	{
    		closesocket(m_socket);
    		WSACleanup();
    		CloseHandle(h);
    		delete[]clone;
    		return 0;
    	}
    	if(clean4==true)
    	{
    		closesocket(m_socket);
    		WSACleanup();
    		fclose(fptr);
    		delete[]clone;
    		return 0;
    	}
    	return 0;
    }
    server
    Code:
    #include <windows.h>
    #include <stdio.h>
    char operation[1024];
    char sockbuf[1024];
    void operationListen(int insock);
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
    	bool c1=false;
    	bool c2=false;
    	bool c3=false;
    	WSADATA wsaData;
    	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    	{
    		c1=true;
    		goto winclean;
    	}
    	if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
    	{
    		c2=true;
    		goto winclean;
    	}
    	SOCKET oursock=socket(AF_INET,SOCK_STREAM,0);
    	if(oursock==INVALID_SOCKET)
    	{
    		c2=true;
    		goto winclean;
    	}
    	c3=true;
    	sockaddr_in local;
    	sockaddr_in from;
    	local.sin_family=AF_INET;
    	local.sin_addr.s_addr=INADDR_ANY;
    	local.sin_port=htons(60006);
    	if(bind(oursock,(sockaddr *)&local,sizeof(local))!=0)
    	{
    		goto winclean;
    	}
    	if(listen(oursock,1)!=0)
    	{
    		goto winclean;
    	}
    	int sz=sizeof(struct sockaddr_in);
    	while(1)
    	{
        	SOCKET socknew=accept(oursock,(struct sockaddr *)&from,&sz);
    		operationListen(socknew);
    		closesocket(socknew);
    		Sleep(1000);
    	}
    	winclean:
    	if(c1==true)
    	{
    		return 0;
    	}
    	if(c2==true)
    	{
    		CloseHandle(hThread);
    		WSACleanup();
    		return 0;
    	}
    	if(c3==true)
    	{
    		CloseHandle(hThread);
    		closesocket(oursock);
    		WSACleanup();
    		return 0;
    	}
    	return 0;
    }
    void operationListen(int insock)
    {
    	FILE *fptr;
    	HANDLE h;
    	bool clean1=false;
    	bool clean2=false;
    	bool clean4=false;
    	FillMemory(operation,1024,0x0);
    	int bytesRecv=recv(insock,operation,1024,0);
    	if(bytesRecv<=0)
    	{
    		clean1=true;
    		goto opclean;
    	}
    	if(strncmp(operation,"download",8)==0)
    	{
    		lenfirst=lstrlen(&operation[9])+10;
    		fptr=fopen(&operation[lenfirst],"rb");
    		if(fptr==0)
    		{
    			clean1=true;
    			goto opclean;
    		}
    		clean2=true;
    		int i,end,j;
    		FillMemory(sockbuf,1024,0x0);
     		for(i=0,end=0;;)
    		{
    			j=fread(sockbuf,1,sizeof(sockbuf),fptr);
    			if(j<0)
    			{
    				goto opclean;
    			}
    			if(j==0)
    			{
    				end=1;
    				goto opclean;
    			}
    			int iRet=send(insock,sockbuf,j,0);
    			if(iRet<=0)
    			{
    				goto opclean;
    			}
    		}
    		goto opclean;
    	}
    	if(strncmp(operation,"upload",6)==0)
    	{
    		lenfirst=lstrlen(&operation[7])+8;
    		h=CreateFile(&operation[lenfirst],GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    		if(h==INVALID_HANDLE_VALUE)
    		{
    			clean1=true;
    			goto opclean;
    		}
    		clean4=true;
    		int i,j;
    		FillMemory(sockbuf,1024,0x0);
    		DWORD dw;
    		for(i=0;;)
    		{
    			j=recv(insock,sockbuf,sizeof(sockbuf),0);
    			if(j<=0)
    			{
    				break;
    			}
    			i+=j;
    			WriteFile(h,sockbuf,j,&dw,0);
    		}
    	}
    
    
    
    	opclean:
    	if(clean1==true)
    	{
    		closesocket(insock);
    		return;
    	}
    	if(clean2==true)
    	{
    		closesocket(insock);
    		fclose(fptr);
    		return;
    	}
    	if(clean4==true)
    	{
    		closesocket(insock);
    		CloseHandle(h);
    		return;
    	}
    
    	return;
    }

  9. #9
    Join Date
    May 2009
    Posts
    140

    Re: winsock client/server, download working, upload not

    if i include #include <winsock2.h> i this code this gives me tons of erros inside winsock2.h if i don't it says hThread and lenfirst not declared =\ what can be wrong?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured