CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Aug 2014
    Posts
    9

    Winsock - sending a picture failed.

    Hello. I'm trying to send a picture using Winsock. The problem is, that it sends approximately 30% of a picture, the rest is blank.

    Second problem is, the Client.exe should return to the main loop, send a data typed from keyboard, and wait to receive a data from Server.exe.
    Instead, after returning to the main loop, and sending something I type, it does not wait to receive a data, but prints some garbage (the garbage size is exactly the same size as the size of char array to receive input).
    Here is my code:

    -------------------------------------------------------Server-------------------------------------------
    Code:
    UploadFile(SOCKET MojSocket)
    {
    	FILE *PlikOpen; char *bufor; string Size; long SizeofFile; int BytesSend;
    	
    	PlikOpen = fopen("D:\\C++\\Visual C\\WinSock\\Client and Server\\Debug\\Jodie.ico", "rb");
    	fseek(PlikOpen, 0, SEEK_END);
    	SizeofFile = ftell(PlikOpen);
    	rewind(PlikOpen);
    
    	bufor = (char*)malloc(sizeof(char)*SizeofFile);
    	fread(bufor, 1, SizeofFile, PlikOpen);
    	fclose(PlikOpen);
    	
    	Size = to_string(SizeofFile);
    	BytesSend = send(MojSocket, Size.c_str(),strlen(Size.c_str())+1, NULL); 	//send the size of a file.
    	BytesSend = send(MojSocket, bufor, SizeofFile, NULL);					//send whole file.
    	free (bufor);
    }
    Message Loop:
    Code:
    string Send; int BytesSend, BytesRecv; char RecvBuf[20]=" ";
    while(1) 
    	{
    		cout << "Your input: "; 
    		getline(cin, Send);
    
    		if(Send == "Upload")
    		{
    			send(MojSocket, Send.c_str(), strlen(Send.c_str()), 0);
    			UploadFile(MojSocket);
    		}
    		else
    		BytesSend = send(MojSocket, Send.c_str(), strlen(Send.c_str()), 0);									
    
    		BytesRecv = recv(MojSocket, RecvBuf,sizeof(RecvBuf), 0); 
    		memset(RecvBuf, 0, sizeof(RecvBuf));	
    	}
    ---------------------------------------------------Client---------------------------------------------

    Code:
    DownloadImage(SOCKET MySocket)
    {
    	FILE *PlikSave; char *bufor; char FileSize[10]; int BytesReceived; int SizeofFile; 
    	
    	BytesReceived = recv(MojSocket, FileSize, sizeof(FileSize), NULL);	//receive the size of incoming file.
    	/* cout<<FileSize gives me 270398 and ~20 junk values. Why? */
    	SizeofFile = atoi(FileSize);
    	/* cout<<SizeofFile gives me 270398, and that is the correct size of a file. */
    	
    	bufor = (char*)malloc(sizeof(char)* SizeofFile);
    	BytesReceived = recv(MojSocket, bufor, SizeofFile, NULL);			//receive a file.
    	
    	
    	PlikSave = fopen("D:\\C++\\Visual C\\WinSock\\Klient\\Debug\\Download.ico", "wb");
    	fwrite(bufor, 1, SizeofFile, PlikSave);
    	fclose(PlikSave);
    	delete[] FileSize;
            free bufor();
    }
    Main Loop:
    Code:
    char RecvBuf[20]=" "; string  Send;	int BytesSend; int BytesReceived;
    
    while (1)
    	{
    		BytesReceived = recv(MojSocket, RecvBuf, sizeof(RecvBuf), 0);  
    		cout << "Received data: " << RecvBuf << endl;
    		
    		if(strcmp(RecvBuf,"Upload")==0)
    			DownloadImage(MySocket);
    		memset(&RecvBuf[0], 0, sizeof(RecvBuf));
    		if (BytesReceived < 0)
    			return 1;
    		cout << "Your input: "; 
    		getline(cin, Send);
    		BytesSend = send(MojSocket, Send.c_str(), strlen(Send.c_str())+1, 0);	  //+1 for '\0', is that correct?			
    	}
    Any help would be appreciated.
    Last edited by VictorN; August 10th, 2014 at 01:39 PM. Reason: adding Code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock - sending a picture failed.

    First, please, use Code tags while posting code snippets. I added them for you in your OP but next time you will have to do it yourself.
    Second, you never check the return values of the Winsock functions. Why are you sure they always succed?
    At least you had to look at the MSDN examples:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock - sending a picture failed.

    Code:
    BytesSend = send(MojSocket, Size.c_str(),strlen(Size.c_str())+1, NULL);
    Why + 1 for the number of bytes to send?

    Code:
    BytesReceived = recv(MojSocket, FileSize, sizeof(FileSize), NULL);
    The contents of FileSize hasn't been initialised prior to using recv()

    Code:
    char FileSize[10]  = {0};
    Code:
    delete[] FileSize;
    No. As FileSize is not a dynamic array and hasn't been allocated using new.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Aug 2014
    Posts
    9

    Re: Winsock - sending a picture failed.

    @VictorN - Thank you, will remember next time to add Code tags. I deleted checking for a return values, because I wanted the code to be clearer (yes, now I see this is not the best option )

    @2kaud - As for strlen(Size.c_str())+1, I wanted to send '\0' char, but now I see this is wrong.
    FileSize[10]={0} Thank you, now there's no garbage after printing FileSize.

    Unfortunetly, it still sends 30% of a picture, and receives some junk.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock - sending a picture failed.

    and the return values of these functions are? - as mentioned by Victor in post #2. You need to check for errors after every api function call as documented for that api function.

    For send() see http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx under return value.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Aug 2014
    Posts
    9

    Re: Winsock - sending a picture failed.

    I've checked every recv and send for SOCKET_ERROR, and there was no errors.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by Patry20 View Post
    I've checked every recv and send for SOCKET_ERROR, and there was no errors.
    Unfortunately your code doesn't show us wherther or how you checked "every recv and send".
    Victor Nijegorodov

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by Patry20 View Post
    I've checked every recv and send for SOCKET_ERROR, and there was no errors.
    and checked that BytesSend is the same as the number specified to send? and that BytesReceived is the expected value?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Aug 2014
    Posts
    9

    Re: Winsock - sending a picture failed.

    No, there is one that doesn't match a value.
    Server sends 6 bytes (size number), Client receives 6 bytes. -Correct
    Server sends 270398 bytes (whole file), Client receives 24464 - Incorrect!
    So, 245934 bytes are lost.

    Maybe there is a limit on how much bytes at once can socket receive, or something similar?

    Edit: If I delete sending and receiving file size, leaving only sending\receiving whole file and set variable SizeofFile to 270398, it works. It receives 270398 bytes, as it should, and the picture is copied in 100%. Any idea why?
    Last edited by Patry20; August 12th, 2014 at 02:56 PM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by Patry20 View Post
    Maybe there is a limit on how much bytes at once can socket receive, or something similar?
    No, there is not any limit. You could receive all 270398 bytes at once (in one packet) however, with a very low probability, or you could receive 270398 packets with only one byte (with a lesser probability), or you could receive any other number of packets (between one and 270398) but in the right order and the total received data would be what you was sent!
    So the problem seems to be in your receiving algorithm!
    Victor Nijegorodov

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock - sending a picture failed.

    If you leave in sending/receiving file size, but still set SizeofFile to 270398 what happens?

    What protocol are you using - reliable or unreliable?

    PS What does Wireshark (or similar) show is happening on the network?
    Last edited by 2kaud; August 12th, 2014 at 04:40 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Aug 2014
    Posts
    9

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by VictorN View Post
    So the problem seems to be in your receiving algorithm!
    Well, I think it is fairly simple algorithm. Get file size -> Send file size - > Receive file size. Send file -> Receive file -> Save file.
    I just don't know why 'recv' receives only 24464 bytes, instead of 270398 .

    Quote Originally Posted by 2kaud View Post
    If you leave in sending/receiving file size, but still set SizeofFile to 270398 what happens?
    Server sends 270398 bytes (whole file), Client receives 24464 bytes (245934 bytes lost).
    Quote Originally Posted by 2kaud View Post
    What protocol are you using - reliable or unreliable?
    Using reliable - TCP.

    I don't have Wireshark installed, if you think that might help, I'll install it tomorrow.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by Patry20 View Post
    Server sends 270398 bytes (whole file), Client receives 24464 bytes (245934 bytes lost).
    It means that your code fails to read the rest or file.
    Again: it is only very rarely possible that so big buffer would be received in only one recv call.
    So you have to check the number of bytes received and if it is less than the length of file then call recv again.
    Victor Nijegorodov

  14. #14
    Join Date
    Aug 2014
    Posts
    9

    Re: Winsock - sending a picture failed.

    Okay, so with your advise @VictorN, I added new variable "BytesReceivedTemp" and set that to 0, and added while loop.
    Code:
    while(BytesReceivedTemp!=270398)
    {
            BytesReceived=recv(MojSocket, bufor, SizeofFile, NULL);
            BytesReceivedTemp=BytesReceivedTemp + BytesReceived;
    }
    cout<<BytesReceivedTemp; //it prints 270398, correct! :)
    Now, when I try to open the downloaded image, Windows says that it cannot be opened, because it doesn't support this type of format file.
    So, I think that maybe is something wrong with the "bufor" variable? Maybe it is being overwritten each time the loop restarts?

    The good news is, now the program returns to the main loop and works fine, so we are getting somewhere!

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by Patry20 View Post
    Now, when I try to open the downloaded image, Windows says that it cannot be opened, because it doesn't support this type of format file.
    So, I think that maybe is something wrong with the "bufor" variable? Maybe it is being overwritten each time the loop restarts?
    it is hard to guess without seeing your actual code.
    Victor Nijegorodov

Page 1 of 2 12 LastLast

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