#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <semaphore.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <pthread.h>
#include <netdb.h>

int MyTcp_listen(const char *,const char *,socklen_t *) ;
int MyAccept(int,struct sockaddr *,socklen_t *) ;
char * MySock_ntop(const struct sockaddr *,socklen_t) ;

int main(int argc, char **argv)
{
    int   listenfd;
    int    connfd;
    socklen_t clilen, addrlen;
    struct sockaddr *cliaddr;
    void   *doit(void *);
    pthread_t tid;
    char ipaddr[20] ;
    char portno[8] ;

    strcpy(ipaddr,"128.110.9.71") ;
    strcpy(portno,"4444") ;
    listenfd = MyTcp_listen(ipaddr,portno,&addrlen);
    cliaddr = (struct sockaddr *)malloc(addrlen);
    for ( ; ; ) {
    clilen = addrlen;
    connfd = MyAccept(listenfd, cliaddr, &clilen);
    printf("threadid [%05d] connection from %s\n",connfd,MySock_ntop(cliaddr, clilen));
    pthread_create(&tid, NULL, &doit, (void *) connfd);
    }
    free(cliaddr);

}//main

void * doit(void *arg)
{
    void accept_child(int);

    pthread_detach(pthread_self());
    accept_child((int) arg);
    close((int) arg);
    printf("thread [%05d] dead...\n",(int) arg);
    return(NULL);
} //doit
void accept_child(int sockfd)
{
    char strdata[81] ;
    int icnt = 0,nread=0 ;
    for ( ; ; )
    {
    memset(strdata,0x00,sizeof(strdata) ) ;
    if ( (nread = recv(sockfd,strdata,50,MSG_NOSIGNAL)) <= 0)
    {
    break;
    }
    strdata[50]=0x00 ;
    //printf("recieve(%s)\n",strdata) ;
    /*
    printf("recieve(%s)\n",strdata) ;
    if (send(sockfd,strdata,strlen(strdata),MSG_NOSIGNAL ) < 0)
    {
    printf("socket write failed  !\n");
    close(sockfd);
    exit(0);
    }
    */
    } //for
} //accept_child

int MyTcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
    int listenfd, n;
    const int on = 1;
    struct addrinfo hints, *res, *ressave;

    bzero(&hints, sizeof(struct addrinfo));
    hints.ai_flags = AI_PASSIVE;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
    {
    printf("tcp_connect error for %s, %s: %s",
   host, serv, gai_strerror(n));
    exit(0) ;
    }
    ressave = res;
    do {
    listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (listenfd < 0)
    continue;    /* error, try next one */

    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    int abuf=30650000 ;
    setsockopt(listenfd, SOL_SOCKET, SO_RCVBUF, &abuf, sizeof(int))  ;
    if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
    break;   /* success */

    close(listenfd);     /* bind error, close and try next one */
    } while ( (res = res->ai_next) != NULL);
    listen(listenfd, 1024);

    if (addrlenp)
    *addrlenp = res->ai_addrlen;    /* return size of protocol address */

    freeaddrinfo(ressave);

    return(listenfd);
} //MyTcp_listen

int MyAccept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
    int n;

    if ( (n = accept(fd, sa, salenptr)) < 0)
    printf("accept error\n");
    return(n);
}
char * MySock_ntop(const struct sockaddr *sa, socklen_t salen)
{
    char portstr[8];
    static char str[128]; /* Unix domain is largest */

    switch (sa->sa_family) {
    case AF_INET: {
    struct sockaddr_in *sin = (struct sockaddr_in *) sa;
    if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL)
    return(NULL);
    if (ntohs(sin->sin_port) != 0) {
    snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin_port));
    strcat(str, portstr);
    }
    return(str);
    }
    case AF_UNIX: {
    struct sockaddr_un   *unp = (struct sockaddr_un *) sa;
    /* OK to have no pathname bound to the socket: happens on
    every connect() unless client calls bind() first. */
    if (unp->sun_path[0] == 0)
    strcpy(str, "(no pathname bound)");
    else
    snprintf(str, sizeof(str), "%s", unp->sun_path);
    return(str);
    }
    default:
    snprintf(str, sizeof(str), "sock_ntop: unknown AF_xxx: %d, len %d",
sa->sa_family, salen);
    return(str);
    }
    return (NULL);
}
gcc -lpthread puresocketserver.c  -o puresocketserver.exe

 

hedgezzz 發表在 痞客邦 留言(0) 人氣()