Thread: What's wrong with my Multi-Thread Echo Server ?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Unhappy What's wrong with my Multi-Thread Echo Server ?

    I've just coded a simple Multi-thread Echo Server (with POSIX pthread)

    First client is served well, but from second client cannot be served at all.
    all clients connect from 127.0.0.1 (localhost).


    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <pthread.h>

    void echo(int sock);

    int
    main(int argc,char** argv){
    int sock; // socket
    int ac_sock; // socket which is accepted.
    struct sockaddr_in soaddr_server; // for server
    struct sockaddr_in soaddr_client; // for client
    struct hostent *hp; // for client
    int len_soaddr_client;
    int z;
    int k;
    pthread_t t_1;

    sock = socket(PF_INET,SOCK_STREAM,0);
    if( sock == -1 )
    printf("error socket()\n");

    memset(&soaddr_server,0,sizeof soaddr_server);

    soaddr_server.sin_family = AF_INET;
    soaddr_server.sin_port = htons(9005);
    inet_aton("127.0.0.1",(struct in_addr *)&soaddr_server.sin_addr);
    if ( soaddr_server.sin_addr.s_addr == INADDR_NONE )
    printf("error sin_addr\n");

    z = bind(sock,(struct sockaddr *)&soaddr_server,sizeof soaddr_server);
    if( z == -1 )
    printf("error bind() : %s\n",strerror(errno));

    for(;{


    z = listen(sock,5);

    len_soaddr_client = sizeof soaddr_client;
    ac_sock = accept(sock,(struct sockaddr *)&soaddr_client,&len_soaddr_client);
    hp = gethostbyaddr((char*)&soaddr_client.sin_addr,
    sizeof soaddr_client.sin_addr,
    soaddr_client.sin_family);

    printf("Connected by %s\n",(char *)hp->h_name);


    pthread_create(&t_1,NULL,(void *)echo,(void *)ac_sock);
    pthread_join(t_1,NULL);

    }

    return 0;
    }

    void echo(int ac_sock){
    int k;
    char buf[255];

    while( (k = read(ac_sock,buf,sizeof buf)) > 0 ){
    printf("%s",buf);
    fflush(stdout);
    write(ac_sock,buf,k);
    }

    close(ac_sock);

    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Taking a guess here: Are you using non-blocking sockets? It doesn't look like you've set them to non-blocking, so it's possible that that is your problem. I haven't worked with threads, so I'm not sure if your thread code is correct. It's possible that the first thread just sits there and blocks while the other[s] can't read/write.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  2. Visual Studio and FTP Server...
    By Grayson_Peddie in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-03-2003, 12:31 PM
  3. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  4. Server receive and echo
    By freddyg in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 06:17 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM