TCP 프로 토 콜 기반 병렬 서버 프로그램

4584 단어 LinuxC
서버:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//      ,    
void *thr_fn ( void *arg )
{
    int size, j;
    char recv_buf[1024];
    int *parg = ( int * ) arg;
    int new_fd = *parg;

    while ( ( size = read ( new_fd, recv_buf, sizeof ( recv_buf ) ) ) > 0 ) {
        if ( recv_buf[0] == '@' ) {
            break;
        }

        printf ( "Message from client(%d): %s
", size, recv_buf ); for ( j = 0; j < size; ++j ) recv_buf[j] = toupper(recv_buf[j]); write(new_fd,recv_buf,size); } close(new_fd); return 0; } int main(int argc, char *argv[]){ socklen_t clt_addr_len; int listen_fd; int com_fd; int ret; int i; static char recv_buf[1024]; int len; int port; pthread_t tid; struct sockaddr_in clt_addr; struct sockaddr_in srv_addr; // , if(argc != 2){ printf("Usage:%s port
",argv[0]); return -1; } // port = atoi(argv[1]); // listen_fd = socket(PF_INET,SOCK_STREAM,0); if(listen_fd < 0){ perror("cannot create listening socket"); return -1; } // memset(&srv_addr,0,sizeof(srv_addr)); srv_addr.sin_family = AF_INET; srv_addr.sin_addr.s_addr = htonl(INADDR_ANY); srv_addr.sin_port = htons(port); // ret = bind(listen_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr)); if(ret == -1){ perror("cannot bind server socket"); close(listen_fd); return -1; } // ret = listen(listen_fd,5); if(ret == -1){ perror("cannot listen the client connect request"); close(listen_fd); return -1; } /* , , read "@" , */ while(1){ len = sizeof(clt_addr); com_fd = accept(listen_fd,(struct sockaddr *)&clt_addr,&len); if(com_fd < 0){ if(errno == EINTR) continue; else{ perror("cannot accept client connect request"); close(listen_fd); return -1; } } printf("com_fd = %d
",com_fd); // if((pthread_create(&tid,NULL,thr_fn,&com_fd)) == -1){ perror("pthread_create error"); close(listen_fd); close(com_fd); return -1; } } return 0; }

클 라 이언 트:
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char**argv){
    int connect_fd;
    int ret;
    char snd_buf[1024];
    int i;
    int port;
    int len;
    static struct sockaddr_in srv_addr;
    //                   
    if(argc != 3){
        printf("Usage: %s server_ip_address port
",argv[0]); return -1; } // port = atoi(argv[2]); // connect_fd = socket(PF_INET,SOCK_STREAM,0); if(connect_fd < 0) { perror("cannot create communication socket"); return 1; } // memset(&srv_addr,0,sizeof(srv_addr)); srv_addr.sin_family = AF_INET; srv_addr.sin_addr.s_addr = inet_addr(argv[1]); srv_addr.sin_port = htons(port); // ret = connect(connect_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr)); if(ret == -1){ perror("cannot connect to the server"); close(connect_fd); return -1; } memset(snd_buf,0,1024); /* , , read "@" , */ while(1){ write(STDOUT_FILENO,"input message:", 14); len = read(STDIN_FILENO,snd_buf,1024); if(len > 0) write(connect_fd,snd_buf,len); len = read(connect_fd,snd_buf,len); if(len > 0) printf("Message form server: %s
",snd_buf); if(snd_buf[0] == '@') break; } close(connect_fd); return 0; }

좋은 웹페이지 즐겨찾기