首页 > 代码库 > linux网络编程echo服务器

linux网络编程echo服务器

echo_server

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <www.qixoo.qixoo.com/sys/types.h>
#include <signal.h>
#include <memory.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>



int sockfd;
void sign_handler(int signo)
{
    if(signo==SIGINT)
    {
        printf("server is stop\n");
        close(sockfd);
    }
    if(signo==SIGCHLD)
    {
        printf("server child is close\n");
        wait(0);
    }
}

void server_do(int fd)
{
    char buffer[BUFSIZ];
    ssize_t size;
    while(1){
        printf("ready to read\n");
        memset(buffer,0,sizeof(buffer));
        if((size=read(fd,buffer,sizeof(buffer)))<0){
            perror("protocal error\n");
            break;
        }else if(size==0){
            break;
        }else{
            printf("number of received bytes=%ld\n",size);
            buffer[size]=‘\0‘;
            printf("%s\n",buffer);
            if(write(fd,buffer,sizeof(buffer))<0){
                if(errno==EPIPE){
                    break;
                }
                perror("patrol error\n");
            }
        }
    }
}

int main(int argc,char *argv[])
{
    if(argc<2)
    {
        printf("usage: %s #port\n",argv[0]);
        exit(1);
    }
    //信号注册
    signal(SIGINT,sign_handler);
    signal(SIGCHLD,sign_handler);
    /*create socket*/
    sockfd=socket(AF_INET,SOCK_STREAM,0);
    /*绑定socket地址*/
    struct sockaddr_in serveraddr;
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(atoi(argv[1]));
    serveraddr.sin_addr.s_addr=INADDR_ANY;
    if(bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))<0){
        perror("socket bind fail\n");
        exit(1);
    }
    /*listen*/
    if(listen(sockfd,10)<0){
        perror("socket listen fail\n");
        exit(1);
    }
    /*accept*/
    struct sockaddr_in clientaddr;
    socklen_t clientaddr_len=sizeof(clientaddr);
    while(1)
    {
        int fd=accept(sockfd,(struct sockaddr*)&clientaddr,&clientaddr_len);
        if(fd<0){
            perror("socket accpet fail\n");
            exit(1);
        }
        /*fork child progress*/
        pid_t pid=fork();
        if(pid<0){
            perror("fork child pid error\n");
            continue;
        }else if(pid==0){
            close(sockfd);//子进程中关闭sockfd
            printf("client from %s\n",inet_ntoa(clientaddr.sin_addr));
            server_do(fd);
            close(fd);
            break;
        }else{
            close(fd);
        }
    }
    close(sockfd);
    return 0;
}

 

  echo_client

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>



int main(int argc,char *argv[])
{
    if(argc<3)
    {
        printf("usage #ip #port\n");
        exit(1);
    }
    
    /*创建socket*/
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        perror("socket error");
        exit(1);
    }
    struct sockaddr_in serveraddr;
    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(atoi(argv[2]));
    inet_pton(AF_INET,argv[1],&serveraddr.sin_addr.s_addr);
    /**connect**/
    if(connect(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))<0)
    {
        perror("socket connect fail\n");
        exit(1);
    }
    /*双向通信*/
    char buff[BUFSIZ];
    size_t size;
    char *prompt=">";
    while(1)
    {
        memset(buff,0,sizeof(buff));
        write(STDOUT_FILENO,prompt,1);
        size=read(STDIN_FILENO,buff,sizeof(buff));
        if(size<0) continue;
        buff[size-1]=‘\0‘;
        
        if(write(sockfd,buff,sizeof(buff))<0){
            perror("write msg error\n");
            continue;
        }else{
            if(read(sockfd,buff,sizeof(buff))<0){
                perror("read msg error\n");
                continue;
            }else{
                printf("%s\n",buff);
            }
        }
    }
    
}

linux网络编程echo服务器