Thread: Beginners - Problem

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

    Beginners - Problem

    Hello all,
    I do not have a lot of expierence with C and at this moment I am kinda stuck. What I am trying to do is make a server in c which is able to communicate with a telnet session. At this moment I can connect with telnet and send a msg from the server to the client and the client can send a msg to the server.

    Code:
      #include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
    
        #define MYPORT 3490
    	#define MAXDATASIZE 100
    
    
        main()
        {
    
        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", 34, 0) == -1)
    				        perror("send");
    					    close(new_fd);
    						exit(0);
    					}
    
    
    	char buf[MAXDATASIZE];
    
    	while((aant_bytes = recv(new_fd, buf, MAXDATASIZE - 1, 0)) > 0) {
        buf[aant_bytes] = '\0';
    
    	printf("%s",buf);
    	}
    
    }
    		
    }
    My problem
    My problem is located in the last bit of code in the while statement. the server receives chars from the client and it gets printed on the server window. This is fun and all but what want to do is convert all the char value's into one intiger. alltought this doesn't sound to hard I can't seem to figure it out.

    at this moment if I type 100 in the client I will get the following result in buf '1' '0' '0'. is there a way to convert this into a int saying 100?

    Kind regards,
    Yuushi Celeritas

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strtol()?

    Also, it should be "int main" and you should "return 0;" at the end of main.

    --
    Mats
    Last edited by matsp; 10-09-2007 at 07:29 AM.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strtol(), strtoul(), strtod(), sscanf()
    Are possible functions to use.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    I though this was my problem but I think it is the way I use the recv statement Its probably totally wrong since because of the while it never comes to an end. And if I try it with a simple if it doesn't show any output.

    Code:
    while((aant_bytes = recv(new_fd, buf, MAXDATASIZE - 1, 0)) > 0) {
    buf[aant_bytes] = '\0';
    printf("%s",buf);
    }
    Is there anyone who can show me a simple C example to recv a input from a telnet session?

    I am following Beej guide on socket programming but I can't really find an example of what I am trying to do.

    Kind regards,
    Yuushi Celeritas

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what you are saying is that you never get zero bytes back from recv(), right?

    If the socket is not set for non-blocking mode, then that's what I'd expect.

    You may set the flags to MSG_DONTWAIT, and it will grab what you have at the moment.

    Don't forget to put a small sleep in so that you don't get someone telling you off for using up 100% of the CPU at all times by polling the port, tho'.

    --
    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.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    ok that makes sense I assume that the flag needs to be added to the recv flag part of the code. where I have a zero now. How would I add the flag? since it requires a int.

    I assume this won't work
    recv(new_fd, buf, MAXDATASIZE - 1, "MSG_DONTWAIT")

    Kind regards,
    Yuushi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Beginner's Problem With First Funtion
    By Ohrange in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2007, 06:59 PM
  5. A beginner's problem
    By NewToC in forum C Programming
    Replies: 5
    Last Post: 11-21-2002, 05:20 PM