반응형
[info.]
2004 Summer / 네트워크 프로그래밍(경북대) / 고석주 교수님
[rspgame_serv.c]
/**
RSP game SERVER
zzun
hello82@unitel.co.kr
http://zzun.net
**/
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUFSIZE 5
void err_handler( char* msg )
{
fputs( msg, stderr );
exit(1);
}
void z_handler( int sig ) // zombie child process handler
{
int rtn;
waitpid( -1, &rtn, WNOHANG );
}
int who_win( int a, int b ) // determine who win
{
if ( a>2 || b>2 || a<0 || b<0 )
return -2;
else if ( a == b)
return 0;
else if ( a == (b+1)%3 )
return 1;
else
return -1;
}
int main( int argc, char** argv )
{
char buffer[BUFSIZE];
int serv_sock, clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
struct sigaction act;
int str_len, clnt_addr_size, serv_card, result;
pid_t pid;
char* RSP[3];
char* result_print[3];
result_print[0] = "Win!\n";
result_print[1] = "Draw! Regame!\n";
result_print[2] = "Lose!\n";
RSP[0] = "Rocks";
RSP[1] = "Scissors";
RSP[2] = "Papers";
if ( argc != 2 )
{
printf( "Usage : %s <port>\n", argv[0] );
exit(1);
}
act.sa_handler = z_handler;
sigemptyset( &act.sa_mask );
act.sa_flags = 0; // for sigaction()
if ( sigaction(SIGCHLD, &act, 0) ) // when child dies
err_handler( "sigaction() error\n" );
serv_sock = socket( PF_INET, SOCK_STREAM, 0 );
if ( serv_sock == -1 )
err_handler( "socket() error\n" );
memset( &serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl( INADDR_ANY );
serv_addr.sin_port = htons( atoi(argv[1]) );
if ( bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) )
err_handler( "bind() error\n" );
if ( listen(serv_sock, 5) )
err_handler( "listen() error\n" );
while (1)
{
clnt_addr_size = sizeof( clnt_addr );
clnt_sock = accept( serv_sock, (struct sockaddr*) &clnt_addr, &clnt_addr_size );
if ( clnt_sock == -1 )
continue;
pid = fork();
switch ( pid )
{
case -1 :
printf("fork() error : client %s\n", inet_ntoa(clnt_addr.sin_addr) );
close( clnt_sock );
continue;
case 0 : // child
close( serv_sock );
do
{
srand((unsigned) time(NULL));
serv_card = rand() % 3;
printf( "Randomly generated number : %d (%s)\n", serv_card, RSP[serv_card] );
read( clnt_sock, buffer, BUFSIZE ); // get client's card
result = who_win( serv_card, atoi(buffer) );
if ( result == -2 )
err_handler( "client input error" );
buffer[0] = '1' + result;
buffer[1] = 0;
printf( "Game with %s : ", inet_ntoa(clnt_addr.sin_addr) );
printf( result_print[result+1] ); // print out the result
write( clnt_sock, buffer, 1 );
} while ( result == 0 ); // tied! try again
printf("connection to %s closed.\n", inet_ntoa(clnt_addr.sin_addr) );
close( clnt_sock );
exit(0);
default : // parent
printf("connected to %s\n", inet_ntoa(clnt_addr.sin_addr) );
close( clnt_sock );
continue;
}
}
return 0;
}
[rspgame_clnt.c]
/**
RSP game CLIENT
zzun
hello82@unitel.co.kr
http://zzun.net
**/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUFSIZE 10
void err_handler( char* msg )
{
fputs( msg, stderr );
exit(1);
}
int main( int argc, char** argv )
{
int sock, str_len, clnt_card, result;
struct sockaddr_in serv_addr;
char buffer[BUFSIZE];
char* result_print[3];
char input_msg[] = "input your card (0:Rocks, 1:Scissors, 2:Papers) : ";
result_print[0] = "You Lose!\n";
result_print[1] = "Draw! Regame!\n";
result_print[2] = "You Win!\n";
if ( argc != 3 )
{
printf( "Usage : %s <IP> <port>\n", argv[0] );
exit(1);
}
sock = socket( PF_INET, SOCK_STREAM, 0 );
memset( &serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr( argv[1] );
serv_addr.sin_port = htons( atoi(argv[2]) );
if ( connect(sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1 )
err_handler( "connect() error\n" );
printf( "connected.\n" );
do
{
do
{
write( 1, input_msg, strlen(input_msg) );
str_len = read( 0, buffer, BUFSIZE );
clnt_card = atoi( buffer );
} while ( clnt_card<0 || clnt_card>2 ); // input correctness check
write( sock, buffer, str_len );
read( sock, buffer, BUFSIZE-1 );
result = atoi( buffer );
if ( result<0 || result>2)
err_handler( "result error\n" );
else
printf( result_print[result] );
} while ( result == 1);
close( sock );
printf( "connection closed.\n" );
return 0;
}
2004 Summer / 네트워크 프로그래밍(경북대) / 고석주 교수님
[rspgame_serv.c]
/**
RSP game SERVER
zzun
hello82@unitel.co.kr
http://zzun.net
**/
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUFSIZE 5
void err_handler( char* msg )
{
fputs( msg, stderr );
exit(1);
}
void z_handler( int sig ) // zombie child process handler
{
int rtn;
waitpid( -1, &rtn, WNOHANG );
}
int who_win( int a, int b ) // determine who win
{
if ( a>2 || b>2 || a<0 || b<0 )
return -2;
else if ( a == b)
return 0;
else if ( a == (b+1)%3 )
return 1;
else
return -1;
}
int main( int argc, char** argv )
{
char buffer[BUFSIZE];
int serv_sock, clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
struct sigaction act;
int str_len, clnt_addr_size, serv_card, result;
pid_t pid;
char* RSP[3];
char* result_print[3];
result_print[0] = "Win!\n";
result_print[1] = "Draw! Regame!\n";
result_print[2] = "Lose!\n";
RSP[0] = "Rocks";
RSP[1] = "Scissors";
RSP[2] = "Papers";
if ( argc != 2 )
{
printf( "Usage : %s <port>\n", argv[0] );
exit(1);
}
act.sa_handler = z_handler;
sigemptyset( &act.sa_mask );
act.sa_flags = 0; // for sigaction()
if ( sigaction(SIGCHLD, &act, 0) ) // when child dies
err_handler( "sigaction() error\n" );
serv_sock = socket( PF_INET, SOCK_STREAM, 0 );
if ( serv_sock == -1 )
err_handler( "socket() error\n" );
memset( &serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl( INADDR_ANY );
serv_addr.sin_port = htons( atoi(argv[1]) );
if ( bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) )
err_handler( "bind() error\n" );
if ( listen(serv_sock, 5) )
err_handler( "listen() error\n" );
while (1)
{
clnt_addr_size = sizeof( clnt_addr );
clnt_sock = accept( serv_sock, (struct sockaddr*) &clnt_addr, &clnt_addr_size );
if ( clnt_sock == -1 )
continue;
pid = fork();
switch ( pid )
{
case -1 :
printf("fork() error : client %s\n", inet_ntoa(clnt_addr.sin_addr) );
close( clnt_sock );
continue;
case 0 : // child
close( serv_sock );
do
{
srand((unsigned) time(NULL));
serv_card = rand() % 3;
printf( "Randomly generated number : %d (%s)\n", serv_card, RSP[serv_card] );
read( clnt_sock, buffer, BUFSIZE ); // get client's card
result = who_win( serv_card, atoi(buffer) );
if ( result == -2 )
err_handler( "client input error" );
buffer[0] = '1' + result;
buffer[1] = 0;
printf( "Game with %s : ", inet_ntoa(clnt_addr.sin_addr) );
printf( result_print[result+1] ); // print out the result
write( clnt_sock, buffer, 1 );
} while ( result == 0 ); // tied! try again
printf("connection to %s closed.\n", inet_ntoa(clnt_addr.sin_addr) );
close( clnt_sock );
exit(0);
default : // parent
printf("connected to %s\n", inet_ntoa(clnt_addr.sin_addr) );
close( clnt_sock );
continue;
}
}
return 0;
}
[rspgame_clnt.c]
/**
RSP game CLIENT
zzun
hello82@unitel.co.kr
http://zzun.net
**/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUFSIZE 10
void err_handler( char* msg )
{
fputs( msg, stderr );
exit(1);
}
int main( int argc, char** argv )
{
int sock, str_len, clnt_card, result;
struct sockaddr_in serv_addr;
char buffer[BUFSIZE];
char* result_print[3];
char input_msg[] = "input your card (0:Rocks, 1:Scissors, 2:Papers) : ";
result_print[0] = "You Lose!\n";
result_print[1] = "Draw! Regame!\n";
result_print[2] = "You Win!\n";
if ( argc != 3 )
{
printf( "Usage : %s <IP> <port>\n", argv[0] );
exit(1);
}
sock = socket( PF_INET, SOCK_STREAM, 0 );
memset( &serv_addr, 0, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr( argv[1] );
serv_addr.sin_port = htons( atoi(argv[2]) );
if ( connect(sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1 )
err_handler( "connect() error\n" );
printf( "connected.\n" );
do
{
do
{
write( 1, input_msg, strlen(input_msg) );
str_len = read( 0, buffer, BUFSIZE );
clnt_card = atoi( buffer );
} while ( clnt_card<0 || clnt_card>2 ); // input correctness check
write( sock, buffer, str_len );
read( sock, buffer, BUFSIZE-1 );
result = atoi( buffer );
if ( result<0 || result>2)
err_handler( "result error\n" );
else
printf( result_print[result] );
} while ( result == 1);
close( sock );
printf( "connection closed.\n" );
return 0;
}
반응형