/* create the connection by socket * means that connect "sockfd" to "server_addr" * 同步阻塞模式 */ if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); }
/* create the connection by socket * means that connect "sockfd" to "server_addr" * 同步阻塞模式 */ if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); }
int main(int argc, char *argv[]) { int sockfd, recvbytes; char rcv_buf[MAXDATASIZE]; /*./client 127.0.0.1 hello */ char snd_buf[MAXDATASIZE]; struct hostent *host; /* struct hostent * { * char *h_name; // general hostname * char **h_aliases; // hostname's alias * int h_addrtype; // AF_INET * int h_length; * char **h_addr_list; * }; */ struct sockaddr_in server_addr;
/* */ fd_set readset, writeset; int check_timeval = 1; struct timeval timeout={check_timeval,0}; //阻塞式select, 等待1秒,1秒轮询 int maxfd; int fp; int cir_count = 0; int ret;
/* create the connection by socket * means that connect "sockfd" to "server_addr" */ if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); }
perl实现: #! /usr/bin/perl ############################################################################### # \File # tcp_client.pl # \Descript # send message to server ############################################################################### use IO::Socket; use IO::Select;
#hash to install IP Port %srv_info =( #"srv_ip" => "61.184.93.197", "srv_ip" => "192.168.1.73", "srv_port"=> "8080", );
my $srv_addr = $srv_info{"srv_ip"}; my $srv_port = $srv_info{"srv_port"};
my $sock = IO::Socket::INET->new( PeerAddr => "$srv_addr", PeerPort => "$srv_port", Type => SOCK_STREAM, Blocking => 1, # Timeout => 5, Proto => "tcp") or die "Can not create socket connect. $@";
$sock->send("Hello server!\n", 0) or warn "send failed: $!, $@"; $sock->autoflush(1);
my $sel = IO::Select->new($sock); while(my @ready = $sel->can_read) { foreach my $fh(@ready) { if($fh == $sock) { while() { print $_; } $sel->remove($fh); close $fh; } } } $sock->close();
这就是异步非阻塞模式 以read系统调用为例 steps: a. 调用read; b. read请求会立即返回,说明请求已经成功发起了。 c. 在后台完成读操作这段时间内,应用程序可以执行其他处理操作。 d. 当 read 的响应到达时,就会产生一个信号或执行一个基于线程的回调函数来完成这次 I/O 处理过程。
int main(int argc, char *argv[]) { int sockfd, recvbytes; char rcv_buf[MAXDATASIZE]; /*./client 127.0.0.1 hello */ char snd_buf[MAXDATASIZE]; struct hostent *host; /* struct hostent * { * char *h_name; // general hostname * char **h_aliases; // hostname's alias * int h_addrtype; // AF_INET * int h_length; * char **h_addr_list; * }; */ struct sockaddr_in server_addr;
/* */ fd_set readset, writeset; int check_timeval = 1; struct timeval timeout={check_timeval,0}; //阻塞式select, 等待1秒,1秒轮询 int maxfd; int fp; int cir_count = 0; int ret;
/* create the connection by socket * means that connect "sockfd" to "server_addr" */ if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); }
server端程序: /* * \brief * tcp server */ #include #include #include #include #include #include #include #define SERVPORT 8080 #define BACKLOG 10 // max numbef of client connection #define MAXDATASIZE 100
int main(char argc, char *argv[]) { int sockfd, client_fd, addr_size, recvbytes; char rcv_buf[MAXDATASIZE], snd_buf[MAXDATASIZE]; char* val; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int bReuseaddr = 1;
char IPdotdec[20];
/* create a new socket and regiter it to os . * SOCK_STREAM means that supply tcp service, * and must connect() before data transfort. */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket:"); exit(1); }
先回答最简单的这个:blocking vs non-blocking。 前面的介绍中其实已经很明确的说明了这两者的区别。 . 调用blocking IO会一直block住对应的进程直到操作完成, . 而non-blocking IO在kernel还在准备数据的情况下会立刻返回。
在说明synchronous IO和asynchronous IO的区别之前,需要先给出两者的定义。 Stevens给出的定义(其实是POSIX的定义)是这样子的: . A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes; . An asynchronous I/O operation does not cause the requesting process to be blocked;