/*minitalk サーバプログラム*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
int CanIRecv( int fd) {
fd_set fdset;
struct timeval timeout;
FD_ZERO( & fdset ) ;
FD_SET( fd , & fdset ) ;
timeout.tv_sec = 0 ;
timeout.tv_usec = 0 ;
return select( fd+ 1 , & fdset , NULL , NULL , & timeout ) ;
}
int main( int argc, char * argv[ ] )
{
int srv_sock, sock, port;
struct sockaddr_in srv_addr, cli_addr;
int addrlen, sendlen, recvlen;
char buf[ 256 ] ;
/* ポート番号を引数から取得 */
port = strtol ( argv[ 1 ] , NULL , 10 ) ;
/* ソケットの作成 */
srv_sock = socket( AF_INET, SOCK_STREAM, 0 ) ;
if ( srv_sock < 0 ) {
perror ( "cannot create socket" ) ;
exit ( - 1 ) ;
}
/* サーバとしてバインド */
srv_addr.sin_family = AF_INET;
srv_addr.sin_port = htons( port) ;
srv_addr.sin_addr .s_addr = INADDR_ANY;
if ( bind( srv_sock, ( struct sockaddr* ) & srv_addr, sizeof ( srv_addr) ) < 0 ) {
perror ( "bind failed" ) ;
exit ( - 1 ) ;
}
/* 待ち受け人数を1人に設定 */
listen( srv_sock, 1 ) ;
/* クライアントからの接続を受け付ける */
addrlen = sizeof ( cli_addr) ;
sock = accept( srv_sock, ( struct sockaddr* ) & cli_addr, & addrlen) ;
if ( sock == - 1 ) {
perror ( "accept failed" ) ;
exit ( - 1 ) ;
}
printf ( "connected\n " ) ;
/* これ以上クライアントを受け付けないので、サーバのソケットを閉じる */
close( srv_sock) ;
/* 実際の通信 */
while ( feof ( stdin ) == 0 ) {
if ( CanIRecv( 0 ) ) {
/* 標準入力からキーボード入力された文字を読み込む */
printf ( "message : " ) ;
fgets ( buf, sizeof ( buf) , stdin ) ;
/* 読み込んだ文字をソケットに書き込む */
sendlen = write( sock, buf, strlen ( buf) + 1 ) ;
if ( sendlen < 0 ) {
perror ( "cannot send a message" ) ;
}
}
if ( CanIRecv( sock) ) {
/* ソケットからのデータを読み込む */
recvlen = read( sock, buf, sizeof ( buf) ) ;
if ( recvlen <= 0 ) {
/* クライアント側が EOF */
break ;
}
printf ( ">> %s" , buf) ;
}
}
close( sock) ;
}
LyptaW5pdGFsayDjgrXjg7zjg5Djg5fjg63jgrDjg6njg6AqLwojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN5cy90eXBlcy5oPgojaW5jbHVkZSA8c3lzL3NvY2tldC5oPgojaW5jbHVkZSA8bmV0ZGIuaD4KI2luY2x1ZGUgPG5ldGluZXQvaW4uaD4KCgppbnQgQ2FuSVJlY3YoaW50IGZkKXsgCglmZF9zZXQgZmRzZXQ7CglzdHJ1Y3QgdGltZXZhbCB0aW1lb3V0OwoJRkRfWkVSTyggJmZkc2V0ICk7IAoJRkRfU0VUKCBmZCAsICZmZHNldCApOwoJdGltZW91dC50dl9zZWMgPSAwOyAKCXRpbWVvdXQudHZfdXNlYyA9IDA7CglyZXR1cm4gc2VsZWN0KCBmZCsxICwgJmZkc2V0ICwgTlVMTCAsIE5VTEwgLCAmdGltZW91dCApOwp9CgoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgKmFyZ3ZbXSkKewoJaW50IHNydl9zb2NrLCBzb2NrLCBwb3J0OwoJc3RydWN0IHNvY2thZGRyX2luIHNydl9hZGRyLCBjbGlfYWRkcjsKCWludCBhZGRybGVuLCBzZW5kbGVuLCByZWN2bGVuOwoJY2hhciBidWZbMjU2XTsKCgkvKiDjg53jg7zjg4jnlarlj7fjgpLlvJXmlbDjgYvjgonlj5blvpcgKi8KCXBvcnQgPSBzdHJ0b2woYXJndlsxXSwgTlVMTCwgMTApOwoKCS8qIOOCveOCseODg+ODiOOBruS9nOaIkCAqLwoJc3J2X3NvY2sgPSBzb2NrZXQoQUZfSU5FVCwgU09DS19TVFJFQU0sIDApOwoJaWYgKHNydl9zb2NrIDwgMCkgewoJCXBlcnJvcigiY2Fubm90IGNyZWF0ZSBzb2NrZXQiKTsKCQlle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n+aWh+Wtl+OCkuiqreOBv+i+vOOCgCAqLwoJCQlwcmludGYoIm1lc3NhZ2UgOiAiKTsKCQkJZmdldHMoYnVmLCBzaXplb2YoYnVmKSwgc3RkaW4pOwoJCQkvKiDoqq3jgb/ovrzjgpPjgaDmloflrZfjgpLjgr3jgrHjg4Pjg4jjgavmm7jjgY3ovrzjgoAgKi8KCQkJc2VuZGxlbiA9IHdyaXRlKHNvY2ssIGJ1Ziwgc3RybGVuKGJ1ZikgKyAxKTsKCQkJaWYgKHNlbmRsZW4gPCAwKSB7CgkJCQlwZXJyb3IoImNhbm5vdCBzZW5kIGEgbWVzc2FnZSIpOwoJCQl9CgkJfQoKCQkKCQlpZiAoQ2FuSVJlY3Yoc29jaykpIHsKCQkJLyog44K944Kx44OD44OI44GL44KJ44Gu44OH44O844K/44KS6Kqt44G/6L6844KAICovCgkJCXJlY3ZsZW4gPSByZWFkKHNvY2ssIGJ1Ziwgc2l6ZW9mKGJ1ZikpOwoJCQlpZiAocmVjdmxlbiA8PSAwKSB7CgkJCQkvKiDjgq/jg6njgqTjgqLjg7Pjg4jlgbTjgYwgRU9GICovCgkJCQlicmVhazsKCQkJfQoJCQlwcmludGYoIj4+ICVzIiwgYnVmKTsKCQl9CgkJCgkJCgl9CgljbG9zZShzb2NrKTsKfQoK
compilation info
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:28: error: ‘strtol’ was not declared in this scope
prog.cpp:34: error: ‘exit’ was not declared in this scope
prog.cpp:43: error: ‘exit’ was not declared in this scope
prog.cpp:51: error: invalid conversion from ‘int*’ to ‘socklen_t*’
prog.cpp:51: error: initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’
prog.cpp:54: error: ‘exit’ was not declared in this scope
prog.cpp:59: error: ‘close’ was not declared in this scope
prog.cpp:69: error: ‘strlen’ was not declared in this scope
prog.cpp:69: error: ‘write’ was not declared in this scope
prog.cpp:78: error: ‘read’ was not declared in this scope
prog.cpp:67: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
stdout