#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    C Socket Programming (Windows)


    I know all the basics of socket programming; for example a simple server and client that send messages back and forth but i've been trying to find more information and I am having trouble.

    I'm looking for any tutorials or information in general for the following:

    - Sending binary files
    - Mulithreading servers
    - Simple Instand Messengers ( client + server )

    and any other more "advanced" topics. ANY help would be appreciated. Mentioning good books would help to :)

    thanks,

    chris
  2. #2
  3. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    For binary file sending, on the server, you can open up a file for binary writing, and keep putting everything you get into it. On the client, open a file for binary reading, and put send char by char till you hit feof (not EOF, binary files don't have EOF). Here is a example, this is very slow, it sends 1byte at a time, but it works:

    on client, assuming you connected already:
    Code:
    	int c, charcount; 
    	fp = fopen("C:\\test.exe", "r+b"); 
    	if(!fp) puts("Error opening file"); 
    
    	charcount=0; 
    	while(!feof(fp))
    	{
    		c=fgetc(fp); 
    		if(!feof(fp))
    		{
    			charcount++; 
    			send(sckClient, &c, 1, 0); 
    		}
    	}
    
    	printf("Done, sent %d bytes", charcount); 
    	fclose(fp);
    On the server, assuming your listening and accepted a connection already:
    Code:
    	puts("someone connected!"); 
    
    	charcount=0; 
    
    	fp = fopen("C:\\File.exe", "w+b"); 
    	while(recv(clientsock, &charbuff, 1, 0)>0)
    	{
    		fputc(charbuff, fp); 
    		charcount++; 
    	}
    	fclose(fp); 
    
    	//User done sending file
    	printf("* File done, %d bytes\n", charcount);
    I suck *** with sockets, actually this is the first time I ever sent a binary file across a network, I just got inspired to learn it from reading your post, if I improve it, ill post what I have, I'll work with sending moer than 1byte at a time now.
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Do an fread into a large buffer (I use 16384 bytes), then send that buffer. Just be sure that when you do your socket send you pass it the return value from fread and not the buffer size or your last send will have whatever crap is at the end of your buffer.

    For example:

    Code:
        while ((intBytesRead = fread(buf, 1, BUFSIZE, fin)) > 0){
            if (send(sock, buf, intBytesRead, 0) == -1) {
                printf("send error\n");
                break;
            }
            tot += intBytesRead;
            printf("\tWritten %d bytes, tot: %d\n", intBytesRead, tot);
        }

    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
  6. #4
  7. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    Thanks mitakeet, btw I am using 20480(20*1024) for buffer size, is that ok? Any reason your using 16384?

    Here is the server/client, link ws2_32.lib
    http://www.team-od.com/CSocketBinaryFiles.zip
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Just a word of caution. If you are creating the buffer on the stack (as I am doing) as opposed to malloc'ing it, you could cause a fault. I am quite certain that your buffer size is not an issue, but if you are loading a bunch of other things on the stack, it may be an issue. There is no perfect size for the buffer, I undoubtably got that figure from some code I copied somewhere and haven't had any problems with it (you will notice, however, that 16384 is a power of two? If you are going to put in magic numbers (anything that is #defined) be sure to use powers of two, that way whoever maintains the code behind you will think you are some sort of brilliant hacker that knows some deep voodoo).

    Any special reason for 20 * 1024?

    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. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    Ok, For small app's I usually use the stack, I'll eventually use the heap when the prog get's bigger.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Thanks


    Thanks for the help guys :)

IMN logo majestic logo threadwatch logo seochat tools logo