⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_server.c.txt

📁 unix program of tcp ip client server for network sockets
💻 TXT
字号:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <netdb.h>

#include <sys/types.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/errno.h>
#include <netinet/in.h>

#include <errexit.c>

int passivesock(const char *service, const char *transport,int qlen);
int passiveTCP(const char *service, int qlen);


#define QLEN 32
#define MAXSIZE 1200000
#define LINELEN 1000

int filerequest(int ssock);

int main(int argc, char* argv[])
{
/* Variable declarations */
struct sockaddr_in fsin,clientaddr;
char* service = "3331";
int msock,ssock;
int rd;
unsigned int alen;
int acceptctr = 0;
pthread_t th;
pthread_attr_t ta;

/* Creating a passive TCP server socket */
msock = passiveTCP(service,QLEN);

/* pthread attribute initialization */
(void)pthread_attr_init(&ta);
(void)pthread_attr_setdetachstate(&ta,PTHREAD_CREATE_DETACHED);

while(1)
{
        fflush(stdout);
        alen = sizeof(struct sockaddr_in);

        /* Accepting client requests */
        ssock = accept(msock,(struct sockaddr*)&clientaddr, &alen);
        printf("accepted:%d\n",acceptctr);
        if(ssock<0)
        {
                printf("Accept failed\n");
                close(msock);
                exit(-1);
        }

        /* Creation of a new thread to handle client requests */
        if(pthread_create(&th,&ta,(void* (*)(void*))filerequest,(void*)ssock) < 0)
        {
                printf("Error! New thread could not be created\n");

        }

        acceptctr++;
}
}

/* Function used by server threads to handle client requests concurrently */
int filerequest(int ssock)
{
        /* Variable declarations */
        int rd;
        char output[MAXSIZE];
        char outputline[LINELEN];
        char* outputptr;
        char* outputptr1;
        int outputlen;
        FILE* fp;
        //printf("In function filerequest\n");
        char filename[255];
        char* fileptr;
        int filelen;
        char* countptr = NULL;
        int size = 0;

        fileptr = filename;
        filelen = sizeof(filename);

        outputptr = output;
        outputptr1 = outputline;
        outputlen = sizeof(output);

        /* Receiving filename from the client */
        rd = recv(ssock,fileptr,filelen,0);
        //printf("Value of read:%d\n",rd);
        if(rd < 0)
        {
                printf("Error! recv() failed\n");
                close(ssock);
                exit(-1);
        }
        //printf("fileptr:%s\n",fileptr);
        /* Opening file & reading the contents */
        fp = fopen(fileptr,"r");
        fgets(outputptr,LINELEN,fp);
        while(feof(fp)==0)
        {
                fgets(outputptr1,LINELEN,fp);
                strcat(outputptr,outputptr1);
        }
        countptr = outputptr;
        while(*countptr!='\0')
        {
                countptr++;
                size++;
        }
        //fputs(outputptr,stdout);
        /* Sending file to the client */
                rd = send(ssock,outputptr,size+1,0);
                if(rd < 0)
                {
                        printf("Error! send() failed\n");
                        close(ssock);
                        exit(-1);
                }
        //printf("Done\n");
        fclose(fp);
        close(ssock);
        return 0;
}


int passiveTCP(const char *service, int qlen)
{
        return passivesock(service, "tcp", qlen);
}

int passivesock(const char *service, const char *transport, int qlen)
/*
 * Arguments:
 *      service   - service associated with the desired port
 *      transport - transport protocol to use ("tcp" or "udp")
 *      qlen      - maximum server request queue length
 */
{
        struct servent  *pse;   /* pointer to service information entry */
        struct protoent *ppe;   /* pointer to protocol information entry*/
        struct sockaddr_in sin; /* an Internet endpoint address         */
        int    x, s, type;
        /* socket descriptor and socket type    */

        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = INADDR_ANY;

    /* Map service name to port number */
        if (( pse = getservbyname(service, transport) ) != NULL)
                sin.sin_port = pse->s_port;
        else if ((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
                errexit("can't get \"%s\" service entry\n", service);

    /* Map protocol name to protocol number */
        if ( (ppe = getprotobyname(transport)) == 0)
                errexit("can't get \"%s\" protocol entry\n", transport);

    /* Use protocol to choose a socket type */
        if (strcmp(transport, "udp") == 0)
                type = SOCK_DGRAM;
        else
                type = SOCK_STREAM;

    /* Allocate a socket */
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0)
                errexit("can't create socket: %s\n", strerror(errno));

    /* Bind the socket */
        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                errexit("can't bind to %s port: %s\n", service,
                        strerror(errno));
 if (type == SOCK_STREAM && listen(s, qlen) < 0)
                errexit("can't listen on %s port: %s\n", service,
                        strerror(errno));
        return s;
}

void reaper(int sig)
{
        int status;
        while (wait3(&status, WNOHANG, (struct rusage *)0) >= 0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -