Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12

    Unhappy Sending files in C using Winsock 1.1


    I have a problem.
    I wrote this:
    Code:
    //Sending:
    
    /*
    
    server is receiving files.
    At first, the program sends the filename to server to create a file for writing.
    then it sends a length of a file, which is stored in 
    
    static long llength;
    
    This part of code fills a buffer and send it*/
    
    #define BS 256
    
    static char buffer[BS];
    static short eof=0;
    static long status = 0;
    
    while(eof == 0){
      for(short temp = 0; temp < BS; temp++){
        status++;
        if(status <= llength){
          buffer[temp] = (char)getc(input);
        }
        else{
          status = 0;
          eof = 1;
          break;
        }
      }
      send(server, buffer, strlen(buffer), 0);
    }
    
    //on the other side, receiving:
    
    static char buffer[BS];
    static short eof = 0;
    static long status = 0;
    
    while(eof == 0){
      recv(client, buffer, BS, 0);
      for(short temp = 0; temp < BS; temp++){
        status++;
        if(status <= llength){
          putc((int)buffer[temp], output);
        }
        else{
          eof = 1;
          break;
        }
      }
    }
    This works fine with text files, but when I want to send *.mp3 or exe file, it stops.

    So I'm asking you to tell e what is wrong.
    And if there is any other way to send any other files, please, tell me.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Here is at least part of your problem:

    Code:
    send(server, buffer, strlen(buffer), 0);
    strlen() looks for NULLs at the end of the string and I am quite certain your mp3 file is full of them. You need to know exactly how much data is in your buffer.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Oh, jesus, I know there's such a stupid problem...

    Thanks a lot. Now it works. Check out this: http://www.loserswebdiary.com
    Last edited by Loser; August 17th, 2003 at 10:19 AM.
  6. #4
  7. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Just one more thing. Is there a simple way to find out on which IP address is the server working?
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Look up gethostbyname()

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    How do you mean - gethostbyname?

    I want to find MY computer's IP.
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Your computer is always 127.0.0.1 (called the loopback address). If you are attached to a network and running Win2K and I believe XP you can type 'ipconfig' at the command prompt (for Linux it is 'ifconfig'), I have no idea for the other Windows versions. As for getting it via code, I am sure there is a way but I haven't bothered to try to figure it out (after all, why run a server if you are going to be talking to yourself?).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    So there's no way to find out my IP on the internet?
    I need to tell a friend my IP. :)
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Yes, use ipconfig (run from a text console shell). The part that says PPP adapter gives your dial-up address (unless you are using certain types of DSL/Cable modem that connect via a network card and act as a gateway, in which case you will have to interrogate the modem).

    Alternatively ask/pay your ISP for a fixed IP address.

    Clifford
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222

    Here's an idea


    Alternatively, your computer should have its own host name, so you could do a gethostbyname on that name. I wrote some code a long time before that did that, but I haven't tried it on a DHCP client.

    [EDIT: I just tried it on my desktop whose DSL connection is a DHCP client. It works, as confirmed by running ipconfig /all . If you think that the code would help you, just ask for it. Though, it was my first exercise using gethostbyname, so the code is a bit ugly.]

    Also, is there an added wrinkle to your problem in that you are on a local network sitting behind a gateway router that uses Network Address Translation? In that case, "your" IP would be the router's IP on its Internet side, though the ports could be switched around as part of NAT.

    Here's a thought: send a datagram (udp) or form a tcp connection with your friend's computer. The packets that he receives will contain your IP address as it appears on the Internet. If you're doing the sockets programming on this, then the task will be trivial. If not, then here's how I would approach it off-hand:
    1. Write an ID server (I just made that up) that will run on your friend's machine. It will listen on a pre-assigned port for an ID message, the data packet of which could include your host name and whatever else you want -- it's all your choice. The main thing is that the ID server's call to recvfrom (if udp) will also return the source address, both IP and port. If you choose to use tcp instead, then the client address is returned by the call to accept.
    2. Write an ID client that your machine runs. If udp, then it will send a datagram to your friend's server. If tcp, then it will connect to your friend's server. It will send as data whatever you have defined that it will send.

    My general idea is that the ID client will send its host name. The ID server will take that host name and the IP address and write them to a flat-ASCII data file which other programs can then read when they need to know what a host's IP address is.

    Does that sound like it would work for you?
    Last edited by dwise1_aol; August 15th, 2003 at 07:14 PM.
  20. #11
  21. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    here you can use this script it will tell your ip on the internet if that's all you need. its just a few line perl script
    http://www.1nfamus.netfirms.com/cgi-bin/addr.pl?
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by infamous41md
    here you can use this script it will tell your ip on the internet if that's all you need. its just a few line perl script
    http://www.1nfamus.netfirms.com/cgi-bin/addr.pl?
    Not being able to read the code, I assume that it does basically as I had described: it reads one of your packets (probably a GET) for the source address, which is your IP and port.

    Actually, the ID server/client suggestion I offered doesn't need to be a separate program. Every single socket program is capable of reading the IP and port that it is connected to or reading datagrams from. It's just a matter of putting in that tiny bit of extra code to do it; e.g., from my udp timserver server (printf slightly modified):
    Code:
        struct sockaddr_in fromAddr;      /* Source address of echo */
    
    ...
    
        if ((respStringLen = recvfrom(sock, (char *)buffer, BUFFERMAX, 0,
                        (struct sockaddr *) &fromAddr, (int *)&fromSize)) < 0)
            DieWithError("recvfrom() failed");
    
        *((unsigned long*)(&sendString[0])) = htonl(GetTime(sTime));
        sendStringLen = 4;
    
        if (sendto(sock, sendString, sendStringLen, 0,
                          (struct sockaddr *)&fromAddr, fromSize) != sendStringLen)
            DieWithError("sendto() sent a different number of bytes than expected");
    
        printf("Serviced request from %s:%d\n",inet_ntoa(fromAddr.sin_addr),
                   ntohs(fromAddr.sin_port));
    Last edited by dwise1_aol; August 15th, 2003 at 09:48 PM.
  24. #13
  25. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Huh, well... Thanks for help. :)
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222

    FWIW


    For what it's worth, here's my forementioned MFC code for reading a host's IP address. In this case, you would pass it your own machine's host name and its IP address will be returned in the addr parameter:
    Code:
    void CTelnetTestDlg::ResolveHostAddress(CString name,CString &addr)
    {
    //	insert here code to read host's IP address
        CString s;
    
        hostent *he = gethostbyname(name);
    
        if (he == NULL)
        {
            s.Format("gethostbyname for %s failed (%d)",name,WSAGetLastError());
            WriteToStatus(s);    // echo error message to status window
            addr = "0.0.0.0";
        }
        else
        {
            if (he->h_addr_list == NULL)
            {
                s.Format("No h_addr_list present for %s.",name);
                WriteToStatus(s);    // echo error message to status window
                addr = "0.0.0.0";
            }
            else
            {
                addr = inet_ntoa(*((struct in_addr*)he->h_addr));
                s.Format("%s IP = %s",name,addr);
                WriteToStatus(s);    // echo action message to status window
            }
        }
    }
    BTW, it works on Win98SE and on Win2k. I've never tried it on XP.
  28. #15
  29. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Thanks a lot.
    Just one more question: I read TCP/IP protocol was made to "cancel" errors. Is my program OK (I tested it on localhost and it worked perfect) or do I still have to write programs which tests those bits...

    How can I do that?
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo