server.c
#include<stdio.h> #define BUFSIZE 100 void *clnt_connection( void* args ); int clnt_number = 0; int main( int argc , char** argv ) if( argc != 2 ) if( pthread_mutex_init( &mutx , NULL ) ) serv_sock = socket( PF_INET , SOCK_STREAM , 0 ); memset( &serv_addr , 0 , sizeof( serv_addr ) ); if( bind(serv_sock , ( struct sockaddr* ) &serv_addr , sizeof( serv_addr ) ) == -1 ) if( listen( serv_sock , 5 ) == -1 ) while( 1 ) pthread_mutex_lock( &mutx ); pthread_create( &thread , NULL , clnt_connection , ( void* )clnt_sock ); printf( "new connection , client ip : %s \n" , inet_ntoa( clnt_addr.sin_addr ) ); void *clnt_connection( void *argv ) while( (str_len = read( clnt_sock , message , sizeof( message ) ) ) != 0 ) pthread_mutex_lock( &mutx ); close( clnt_sock ); void send_message( char *message , int len ) void error_handling( char *message ) |
client.c
#include <stdio.h> #define BUFSIZE 100 void *send_message( void *arg ); char name[NAMESIZE] = "[Default]"; int main( int argc , char** argv ) if( argc != 4 ) sprintf( name , "[%s]" , argv[3] ); sock = socket( PF_INET , SOCK_STREAM , 0 ); memset( &serv_addr , 0 , sizeof( serv_addr ) ); if( connect( sock , (struct sockaddr*) &serv_addr , sizeof( serv_addr ) ) == -1 ) pthread_create( &snd_thread , NULL , send_message , ( void* ) sock ); pthread_join( snd_thread , &thread_result ); close( sock ); void *send_message( void *arg ) void *recv_message( void *arg ) name_message[str_len] = 0; void error_handling( char *message ) |
컴파일 실행
[cent@centhost pthread]$ gcc -D_REENTRANT server.c -o server -lpthread [cent@centhost pthread]$ ./server 9999 |
[cent@centhost pthread]$ gcc -D_REENTRANT client.c -o client -lpthread [cent@centhost pthread]$ ./client 127.0.0.1 9999 kim |
[cent@centhost pthread]$ ./client 127.0.0.1 9999 lee |
'프로그램언어 > C/C++' 카테고리의 다른 글
Posix Thread Example (0) | 2012.03.24 |
---|---|
컴파일 과정 & gcc 옵션 요약 (0) | 2012.03.24 |
Pthread API Reference (1) | 2012.03.24 |
유용하게 사용하는 매크로 함수 (0) | 2012.03.24 |
[AIX]dynamic library & static library (0) | 2012.03.24 |