#include
#include
#include
#include
#include
#include
#include
char *cmd_1 = "test_create_inode";
char *cmd_2 = "test_destroy_inode";
int sockfd,numbytes;
char buf[100];
struct sockaddr_in their_addr;
//int i = 0;
int socket_set_nonblock (int fd)
{
int flags = 0;
int ret = -1;
flags = fcntl (fd, F_GETFL);
if (flags != -1)
ret = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
else
printf("failed to get flags\n");
return ret;
}
int main(int argc,char *argv[])
{
time_connect(2);
return 1;
}
//man connect
//read : EINPROCESS
int time_connect(int seconds)
{
//he = gethostbyname(argv[1]);
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
printf("create socket error\n");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(2323);
// their_addr.sin_addr = *((struct in_addr *)he->h_addr);
inet_aton( "127.0.0.1", &their_addr.sin_addr );
socket_set_nonblock(sockfd);
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect");
}
fd_set wrevents;
FD_ZERO(&wrevents);
FD_SET(sockfd, &wrevents);
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
int retcode = select(sockfd + 1, NULL, &wrevents, NULL, &tv);
if (retcode < 0)
{
printf("failed\n");
}
else if (retcode == 0)
{
printf("time out\n");
}
else //(retcode > 0)
{
printf("have event\n");
if (FD_ISSET(sockfd, &wrevents))
{
printf("writable\n");
int err = 0;
int len = sizeof(err);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
printf("failed to getsockopt\n");
}
else
printf("succ to getsockopt\n");
if (err == 0)
{
printf("connect succ\n");
return 1;
}
else
printf("connect failed\n");
}
}
return 0;
}
void print_usage()
{
printf("input an integer command. 1: create inodes; 2: destroy inode\n");
}
阅读(1477) | 评论(0) | 转发(0) |