[Linux]非阻塞模式下socket发送数据
2014-10-29 23:13
375 查看
#include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<netinet/tcp.h> #include<netdb.h> #include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<fcntl.h> #include<errno.h> #include<string.h> #define BUF_SIZE 1024*128 int main(const int argc,char* const argv[]) { int socket_fd; struct sockaddr_in name; struct hostent *hostinfo; socket_fd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); name.sin_family = AF_INET; hostinfo = gethostbyname(argv[1]); if(hostinfo == NULL) return -1; name.sin_addr = *((struct in_addr*)hostinfo->h_addr); name.sin_port = htons(atoi(argv[2])); if(connect(socket_fd,(struct sockaddr*)&name,sizeof(name))==-1) return -2; printf("connect server succss\n"); char buf[BUF_SIZE]; ssize_t count; for(int i=0;i<BUF_SIZE;++i)buf[i]=(char)(i%256); count=write(socket_fd,buf,BUF_SIZE); printf("in block mode, count = %d\n",count); int sock_flag; int times=0; sock_flag = fcntl(socket_fd,F_GETFL); fcntl(socket_fd,F_SETFL,sock_flag|O_NONBLOCK); count=write(socket_fd,buf,BUF_SIZE); printf("in nonblock mode, count = %d\n",count); close(socket_fd); return 0; }
相关文章推荐
- [Linux] 通过shell给unix socket发送数据
- linux非阻塞的socket发送数据出现EAGAIN错误的处理方法
- linux Socket数据发送中信号SIGPIPE及相关errno的研究
- Linux下发送与接收组播数据(socket编程)
- Linux下发送与接收组播数据(socket编程)
- 中标麒麟(linux)下QUdpSocket无法使用组播发送数据问题
- linux socket无延迟发送数据
- Linux下发送与接收组播数据(socket编程)
- [Linux] 通过shell给unix socket发送数据
- Linux Socket 自定义包 发送 解决接受任意长度数据
- 异步 SOCKET 编程 - 发送和接收数据
- 【转】Socket通过HTTP代理发送数据
- socket 发送数据需要注意的问题
- 异步SOCKET编程-发送和接收数据
- Socket数据发送中信号SIGPIPE及相关errno的研究(转)
- 成功实现Linux下Socket编程中用send发送结构体
- 使用DatagramSocket发送和读取数据
- Linux下Socket编程中用send发送结构体
- 异步SOCKET编程-发送和接收数据
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字