socket编程(TCP多进程客户与服务器)

能够实现一个服务器与多个客户端之间通信
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;
}
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;
        }
         printf("get a connect,ip is %s port is %d\n",
               inet_ntoa(client.sin_addr),
               ntohs(client.sin_port));

        pid_t id=fork();
        if(id==0)
        {
            while(1)
            {
            ssize_t s=read(newsock,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(newsock,buf,strlen(buf));
                    }
                }
                else if(s==0)
                {
                    printf("client quit...\n");
                    break;
                }
                else
                {
                    break;
                }
            }
        }
    }
        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;
}

你可能感兴趣的:(网络编程,socket,服务器,编程)