Problem with Socket.h in C

This is a discussion on Problem with Socket.h in C within the Networking/Device Communication forums, part of the General Programming Boards category; Hello all, I am trying to make a small server which is connectable trough a telnet session. I am following ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    7

    Thumbs down Problem with Socket.h in C

    Hello all,
    I am trying to make a small server which is connectable trough a telnet session. I am following Beej's guide but I am stuck with a error I do not seem to get fixed.

    when I run this source I am able to connect, I see the msges in server console and on the client. But when I start typing it only displays the first letter. What I would like is that it receives data when I hit enter.

    At this moment when I type data for example it shows a d immediatly but not the rest

    Code:
    	#include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
    
        #define MYPORT 3490
    	#define MAXDATASIZE 100
    
    
        int main()
        {
    	char buf[MAXDATASIZE];
        int sockfd, new_fd, aant_bytes;  /* luisteren op sock_fd, nieuwe verbinding op new_fd */
        struct sockaddr_in mijn_addr;   /* mijn adresinformatie */
        struct sockaddr_in hun_addr; /* connector's adresinformatie */
        int sin_size;
    
    	// vergeet de foutafhandeling voor socket() niet | errno is fout | geeft -1:
        sockfd = socket(AF_INET, SOCK_STREAM, 0); // doe wat foutafhandeling!
    
    	mijn_addr.sin_family = AF_INET;         // host byte volgorde
        mijn_addr.sin_port = htons(MYPORT);     // short, netwerk byte volgorde
        mijn_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        memset(&(mijn_addr.sin_zero), '\0', 8); // de rest van de struct op nul
    
        // vergeet de foutafhandeling voor bind() niet | errno is fout | geeft -1:
        bind(sockfd, (struct sockaddr *)&mijn_addr, sizeof(struct sockaddr));
    
    
        //server laten luisteren | TODO Errorhandling | errno is fout | value -1
        printf("server: I am now listening on %d \n", MYPORT);
    	listen(sockfd, 10);
    
        while(1){
    		sin_size = sizeof(struct sockaddr_in);
            if((new_fd = accept(sockfd,(struct sockaddr*)&hun_addr,&sin_size))== -1){
    			perror("accept");
                continue;
            }
    
    		printf("server: Received connection from: %s \n",inet_ntoa(hun_addr.sin_addr));
    	
    		if (!fork()) { /* dit is het kind proces */
    
    			if (send(new_fd, "Welcome to my postFix Calculator. \n \n", 36, 0) == -1){
    				perror("send");
    			    close(new_fd);
    				exit(0);
    			}
    		
    			if ((aant_bytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) <= 0) {
    				// got error or connection closed by client
    				if (aant_bytes == 0) {
    					// connection closed
    					printf("selectserver: socket hung up\n");
    				} 
    				else {
    					perror("recv");
    				}
    			}
    			else{
    			// we got some data from a client
    			printf("server: %s \n ",buf);
    			} 
    		}
    	}
    return 0;
    }
    I've been messing with this all night and I just don't seem to get it right.

    Kind regards,
    Yuushi Celeritas

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What happens if you call recv() in a loop?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    changed it to:

    Code:
     
    			while ((aant_bytes = recv(new_fd, buf, MAXDATASIZE, 0)) > 0) {
    
    			// we got some data from a client
    			printf("%s",buf);
    			}
    which seems to work now I type data hit enter and it shows something like:
    http://www.digital-it.nl/temp/sample.jpg

    after that every key I hit gets displayed below that text:
    http://www.digital-it.nl/temp/sample1.jpg

    :S

    kind regards,
    Yuushi

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think a normal telnet connection sends a char at a time, so that would be why you are receving the packets as one char at a time.

    So just pack together a bunch of chars until you get a newline, and then process that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    that makes sense but I don't really have a clue how to do that, I assume you retrieve the chars in a for loop and put them in a variable untill buf == "\n"?

    wouldn't slamming them together be a problem if the chars look weird like in the images?

    Kind regards,
    Yuushi

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe your "weird" characters come from the fact that buf[] is not nul-terminated, so printf just prints a bunch of rubbish after your actual data. [It looks to me like you happen to have a 32-bit value where the uppermost byte is zero, thus getting your char, then two "random" characters printed, and the zero acts as a termination - it is probably some return-address or a pointer to something that happens to be on the stack - don't rely on this behaviour to be consistant if you modify your application!]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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