socket编程(TCP多线程客户端服务器)

能够实现一个服务器与多个客户端之间通信
Makefile中引入了pthread库
Makefile

.PHONY:all
all:server client
server:server.c
    gcc -o $@ $^ -lpthread
client:client.c
    gcc -o $@ $^ -lpthread
.PHONY:clean
clean:
    rm -f server client

server.c

#include 
#include 
#include 
#include 
#include 
#include 

void Usage(char* proc)
{
    printf("usage:%s [local_ip] [local_port]\n",proc);
}

int startup(char* ip,int port)
{
    int sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock<0)
    {
        perror("socket");
        return -1;
    }
    int opt=1;
    if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt))<0)
    {
        perror("setsock");
        return -5;
    }
    struct sockaddr_in server;
    server.sin_family=AF_INET;
    server.sin_port=htons(port);
    server.sin_addr.s_addr=inet_addr(ip);

    if(bind(sock,(struct sockaddr*)&server,sizeof(server))<0)
    {
        perror("bind");
        return -2;
    }
    if(listen(sock,10)<0)
    {
        perror("listen");
        return -3;
    }
    return sock;
}

void* new_pthread(void* arg)
{
    int sock=(int)arg;
    char buf[1024];
    while(1)
    {
        ssize_t s=read(sock,buf,sizeof(buf)-1);
        if(s>0)
        {
            buf[s]=0;
            printf("client say#%s\n",buf);
            printf("server say#");
            fflush(stdout);
            s=read(0,buf,sizeof(buf)-1);
            if(s>0)
            {
                buf[s-1]=0;
                write(sock,buf,strlen(buf));
            }
        }
        else if(s==0)
        {
            printf("client quit....\n");
            break;
        }
        else
        {
            break;
        }
    }
    pthread_exit(NULL);
}

int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        return -4;
    }
    int sock=startup(argv[1],atoi(argv[2]));

    struct sockaddr_in client;
    socklen_t len=sizeof(client);
    char buf[1024];
    while(1)
    {
        int newsock=accept(sock,(struct sockaddr*)&client,&len);
        if(newsock<0)
        {
            perror("accept");
            return -6;
        }

        pthread_t tid;
        pthread_create(&tid,NULL,new_pthread,(void*)newsock);
        pthread_detach(tid);
    }
        return 0;
}

client.c

#include 
#include 
#include 
#include 
#include 
#include 
void Usage(char* proc)
{
    printf("usage:%s [local_ip] [local_port]\n",proc);
}

int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        return -1;
    }
    int sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock<0)
    {
        perror("socket");
        return -2;
    }
    struct sockaddr_in server;
    server.sin_family=AF_INET;
    server.sin_port=htons(atoi(argv[2]));
    server.sin_addr.s_addr=inet_addr(argv[1]);

    if(connect(sock,(struct sockaddr*)&server,sizeof(server))<0)
    {
        perror("connect");
        return -3;
    }
    printf("connect success......\n");
    char buf[1024];
    while(1)
    {
        printf("client say#");
        fflush(stdout);
        ssize_t s=read(0,buf,sizeof(buf)-1);
        if(s>0)
        {
            buf[s-1]=0;
            write(sock,buf,strlen(buf));
            printf("please wait....\n");
            s=read(sock,buf,strlen(buf));
            if(s>0)
            {
                buf[s]=0;
                printf("server echo# %s\n",buf);
            }
        }
    }
        return 0;
}

你可能感兴趣的:(网络编程)