当前位置: 技术问答>linux和unix
大家帮忙看下这个非阻塞程序的代码哈
来源: 互联网 发布时间:2016-04-04
本文导语: #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct sockaddr servaddr; struct sockaddr_in *st_in; int ...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
struct sockaddr servaddr;
struct sockaddr_in *st_in;
int sockfd, flags;
char buffer[1000], str[30];
int recvlen;
char *ptr;
int dstport, n;
fd_set rset, wset, *tmpset = NULL;
struct timeval tval;
int error = 0;
socklen_t len;
if (argc != 2) { //这个地方的2是不是就是说有两个参数?一个IP,一个端口?
printf("usage:need ip:portn");
return 0;
}
strncpy(str, argv[1], 30);
str[29] = 0;
ptr = strchr(str, ':');
*ptr = '';
dstport = atoi(ptr + 1);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd sin_family = AF_INET;
st_in->sin_port = htons(dstport);
inet_pton(AF_INET, str, &st_in->sin_addr);
printf("connect to %s:%d....n", str, dstport);
if ((n = connect(sockfd, &servaddr, sizeof(servaddr)))