Help needed in echo server program

Discussion in 'C' started by lionaneesh, May 28, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    hey i fixed up my previous echo server program but now i am facing problems in recv() function..

    This program successfully sends "Hiii!!!!" to the client .

    But is not receiving properly ..
    I telneted the program to test
    and when i send a message from telnet to this program .

    It is showing some:-
    "libc.so.6"

    on the screen

    why this is happening ..
    Help me with this.

    Here is the new source code:-

    Code:
    /* A simple server in the internet domain using TCP
       The port number is passed as an argument */
    #include <stdio.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include<arpa/inet.h>
    #include<netdb.h>
    #include<string.h>
    
    #define portno "4040"
    
    int main()
    {
            struct addrinfo hints,*serv,*p;
        int sockfd,newfd,status;
        struct sockaddr_storage their_addr;
        socklen_t addr_size;
        char *msg;
        int len;
        char buff[100];
    
        memset(&hints,0,sizeof(hints));
        hints.ai_socktype=SOCK_STREAM;
        hints.ai_family=AF_UNSPEC;
        hints.ai_flags=AI_PASSIVE;
    
        if((status=getaddrinfo(NULL,portno,&hints,&serv)) == -1)
        {
            fprintf(stderr,"Error in getaddrinfo :-\n %s\n",gai_strerror(status));
        }
    
        for(p=serv;p!=NULL;p = p->ai_next)
        {
            if( (sockfd = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1 )
            {
                printf("Error in Socket \n");
                continue;
            }
            
            if( bind(sockfd,p->ai_addr,p->ai_addrlen) == -1)
            {
                printf("Binding error!!!\n");
                close(sockfd);            
                continue;
            }
            
            break;
        }
    
        /* if( (connect(sockfd,serv->ai_addr,serv->ai_addrlen) ) == -1 )
        {
            printf("Connect err!!!\n\n");
            return(0);
        }  if on client we use this*/
    
        listen(sockfd,10);
    
        addr_size= sizeof(their_addr);
        newfd = accept(sockfd, (struct sockaddr *)&their_addr,&addr_size);
        
        
            printf("%s",msg);
            
            if( ( send(newfd,"Hiii!!!!",9,0) ) == -1)
            {
                printf("err in send\n\n");
                return(1);
            }
        
        recv(newfd,buff,sizeof(buff),0);
        printf("\n");
        freeaddrinfo(serv);
        close(sockfd);
        return(0);
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice