Like Tree1Likes

c socket send int instead of a char array

This is a discussion on c socket send int instead of a char array within the C Programming forums, part of the General Programming Boards category; I'm running client and server on the same machine with Ubuntu 12.04-amd64 (errors checking have been deleted). Server send: Code: ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52

    c socket send int instead of a char array

    I'm running client and server on the same machine with Ubuntu 12.04-amd64 (errors checking have been deleted).

    Server send:
    Code:
    uint32_t fsize;
    struct stat fileStat;
    fd = open(filename, O_RDONLY);
    stat(fd, &fileStat);
    fsize = fileStat.st_size
      if(send(f_sockd, &fsize, sizeof(fsize), 0) < 0){
        perror("error on sending file size\n");
        onexit(f_sockd, m_sockd, 0, 2);
      }
    Client receive:
    Code:
    uint32_t fsize;
      if(recv(f_sockd, &fsize, sizeof(fsize), 0) < 0){
        perror("error on receiving file size");
        onexit(f_sockd, 0, 0, 1);
      }
    The problem is that the client receive a biiiig number.
    For example the server sent 20 and the client receives 1801745249 O.o
    How can i solve this strange behaviour=

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your code looks ok. just a thought. is some other process sending to that address - port?
    Last edited by dmh2000; 08-29-2012 at 08:35 AM. Reason: typos

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    no! i'm doing some test and only me are connected to my network

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what port are you binding the receive socket to?

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    ./ftpserver 2000
    ./ftpclient localhost 2000

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You should ideally use these routines when encoding wide data for transmission on the network. You will come across endian issues when you get out into the wider world.
    htonl

    But your large number in hex is 0x6B647361
    Strangely, all printable characters - spelling "asdk"
    Is this in your text file by any chance?

    Are your client and server built at the same time?
    dmh2000 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    I know but it is only a personal project to understand and study C (and network programming)
    However I have built them with "make"!
    And no, "asdk" isn't in any file i've used xD

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    just gessing here. when you configured your sockaddr_in structures, did you use htons on the port number 2000? if not, you might have bound to the FTP port.

    edit: never mind, it would not be the FTP port. if you assigned 2000 to sin_port without htons, it would give you a big number that would be ok as long as both sides did the same thing
    Last edited by dmh2000; 08-29-2012 at 08:57 AM. Reason: brain error

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    yes, i've used it!

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    does the program work the first time then start giving you the error?

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    The program works perfectly the only problem is when i have to send the file size because it doesn't works

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    the problem is into my program because i've tried to code a small socket program that exchange an int (send(sock, &val, sizeof(val), 0)) and it works perfectly -.-''

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    then Salem was on the right track. either you are not sending all the file data or you are not receiving all the file data, so that when you read the file size you get part of a file rather than the file size. instrument your program with printfs to see how much is actually sent and received.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    > (errors checking have been deleted)

    This worries me for several reasons:

    • You are new to programming, you probably don't have the best intuition as to the cause of such problems.
    • If you don't actually have error checking in your program, then you're trying to find problems/debug code without error checking, so you wont know where or what the error is.
    • If you do have error checking in your program, then the code you posted is not the same as the code causing problems, so we can't really help you. Please post the smallest, compilable program that demonstrates the problem (unless it's the code from your other thread -- in which case, one thread per problem please).


    You need to check the return result from every IO related call. That means every socket operation, send/recv/read/write, all your open() calls, stat(), etc. Read the man page for each one thoroughly, check all error conditions and print a sensible error (using perror or the like). You only check some things in your other program.

    You claim the server sent 20 but the client didn't receive the right number. I take it you printed out that info, or examined it in a debugger. Which reminds me, can you step through your program with a debugger and identify the actual line that is a problem? Perhaps you receive the right number, but something else, like a buffer overflow corrupts it.

    Also, your other program uses
    Code:
    fflush(stdin);
    You can't flush an input stream, only output. Flushing stdin results in undefined behavior. Technically that could be the cause of your problem, but it's unlikely. Regardless, remove it. I didn't read it thoroughly for other such problems.
    Last edited by anduril462; 08-29-2012 at 09:29 AM.

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    Hello
    1) i've deleted only the checks of the open and stat calls (if ... < 0 then error)
    2) the other 3d is for another problem
    3) thanks, i was desperate so i've tried all without know the consequence :O i've deleted all the fflush(stdin) but the problem is still here!
    Last edited by polslinux; 08-29-2012 at 10:05 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket send
    By Drogin in forum Networking/Device Communication
    Replies: 8
    Last Post: 11-19-2009, 06:44 AM
  2. Socket Send
    By anne_kleyheeg in forum C Programming
    Replies: 3
    Last Post: 08-13-2009, 02:20 PM
  3. How Do I Send a Two-Dimensional Char Array to a Function?
    By DonFord81 in forum C Programming
    Replies: 7
    Last Post: 02-26-2009, 02:01 AM
  4. Send and Int Through Socket?
    By BENCHMARKMAN in forum C Programming
    Replies: 2
    Last Post: 03-16-2008, 06:58 AM
  5. Socket Send Help
    By cloudy in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-13-2007, 03:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21