易截截图软件、单文件、免安装、纯绿色、仅160KB

Unix(Linux) C++ 下支持timeout的connect()的实现例子

java 的connect有timeout这个功能,C++的connect反而没有? 网查得到了两个实现的版本如下:
支持timeout的connect() / Connect with timeout
Code:
 void connect_w_to(void) {
int res;
struct sockaddr_in addr;
long arg;
fd_set myset;
struct timeval tv;
int valopt;
socklen_t lon;
// Create socket
soc = socket(AF_INET, SOCK_STREAM, 0);
if (soc < 0) {
fprintf(stderr, "Error creating socket (%d %s)\n", errno, strerror(errno));
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(2000);
addr.sin_addr.s_addr = inet_addr("192.168.0.1");
// Set non-blocking
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
exit(0);
}
arg |= O_NONBLOCK;
if( fcntl(soc, F_SETFL, arg) < 0) {
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
exit(0);
}
// Trying to connect with timeout
res = connect(soc, (struct sockaddr *)&addr, sizeof(addr));
if (res < 0) {
if (errno == EINPROGRESS) {
fprintf(stderr, "EINPROGRESS in connect() - selecting\n");
do {
tv.tv_sec = 5 //overtime;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(soc, &myset);
res = select(soc+1, NULL, &myset, NULL, &tv);
if (res < 0 && errno != EINTR) {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
exit(0);
}
else if (res > 0) {
// Socket selected for write
lon = sizeof(int);
if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) {
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
exit(0);
}
// Check the value returned...


相关文档:

linux 0.11 内核学习 file_dev.c


/*
 *  linux/fs/file_dev.c
 *
 *  (C) 1991  Linus Torvalds
 */
#include <errno.h>
#include <fcntl.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <asm/segment.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#defi ......

linux 常用网络工具

1,网络整体流量查看
  ifconfig -s
  cat /proc/net/dev
  sar -n DEV/EDEV interval(时间隔) count(次数)
  nload -m [-d interface]
2,详细查看网络流量,比如查看某一个ip/port,某一个协议
  iptraf,文本窗口界面
  ntop,web网页界面 ......

Linux设置DNS和主机名

Linux下配置静态IP地址,设置DNS和主机名
配置文件位于:
/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.3
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
使IP地址生效:
/sbin/ifdown eth0
/sbin/ifup eth0
配置dns解析
echo "nameserver 211.98.1.28" ......

Linux上构筑iPhone OS3.1.2开发环境之build

昨天郁闷了一天,因为不管我如何编译,总是有错误产生。
没想到今天我清理掉所有文件,重新操作了一编,居然神奇般编译成功了。挖哈哈。。。太高兴了,终于离编译可运行在IPHONE
机器上的HELLO world 不远了,太激动了。。
总结一下:我昨天安装了管方列出的所有包,还有就是不放弃,多试几次。
Here we provide a list ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号