当前位置: 技术问答>linux和unix
socket编程,建立连接后为何必须不断地发送,只要间隔3s再发送,对方就收不到数据了?怎么办?谢谢
来源: 互联网 发布时间:2015-12-08
本文导语: //Serial Net #include /*标准输入输出定义*/ #include /*标准函数库定义*/ #include /*Unix 标准函数定义*/ #include #include #include /*文件控制定...
//Serial Net
#include /*标准输入输出定义*/
#include /*标准函数库定义*/
#include /*Unix 标准函数定义*/
#include
#include
#include /*文件控制定义*/
#include /*PPSIX 终端控制定义*/
#include /*错误号定义*/
#include
#include
#include
#include
#include
#include
#define FALSE 0
#define TRUE 1
char * host_name="192.168.3.18";
int server_port=8000;
#define MAXDATASIZE 1024
int main(int argc,char ** argv)
{
int i;
int socket_fd;
int max_fd;
struct sockaddr_in server_addr;
fd_set rd_set,wr_set,er_set;
struct timeval timeout;
int ret;
char com_recv_buf[MAXDATASIZE];
char com_send_buf[MAXDATASIZE];
char socket_recv_buf[MAXDATASIZE];
char socket_send_buf[MAXDATASIZE];
unsigned int recv_len,send_len;
for(;;)//网络通信
{
//客户端建立连接
for(;;)
{
sleep(5);
printf("nconnecting to serverhost");
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(host_name);
server_addr.sin_port=htons(server_port);
if((socket_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("nError openning socket");
close(socket_fd);
continue;
}
printf("nStart Connect ...");
fcntl(socket_fd, F_SETFL, O_NONBLOCK);//设为非阻塞
printf("nStart Connect 1 ...");
if(connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
printf("nError connect ...errno = %d ",errno);
sleep(10);
break;
}
else
{
break;
}
}
printf("nConnect OK...");
max_fd = (socket_fd > com_fd ? socket_fd : com_fd) + 1;
FD_ZERO(&rd_set);
FD_ZERO(&wr_set);
FD_ZERO(&er_set);
FD_SET(socket_fd,&rd_set);
FD_SET(socket_fd,&er_set);
timeout.tv_sec=2;
timeout.tv_usec=0;
printf("nStart select ");
while(1)
{
sleep(2);
// printf("nStart select 1 ");
ret=select(max_fd,&rd_set,&wr_set,&er_set,&timeout);
if (ret
#include /*标准输入输出定义*/
#include /*标准函数库定义*/
#include /*Unix 标准函数定义*/
#include
#include
#include /*文件控制定义*/
#include /*PPSIX 终端控制定义*/
#include /*错误号定义*/
#include
#include
#include
#include
#include
#include
#define FALSE 0
#define TRUE 1
char * host_name="192.168.3.18";
int server_port=8000;
#define MAXDATASIZE 1024
int main(int argc,char ** argv)
{
int i;
int socket_fd;
int max_fd;
struct sockaddr_in server_addr;
fd_set rd_set,wr_set,er_set;
struct timeval timeout;
int ret;
char com_recv_buf[MAXDATASIZE];
char com_send_buf[MAXDATASIZE];
char socket_recv_buf[MAXDATASIZE];
char socket_send_buf[MAXDATASIZE];
unsigned int recv_len,send_len;
for(;;)//网络通信
{
//客户端建立连接
for(;;)
{
sleep(5);
printf("nconnecting to serverhost");
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(host_name);
server_addr.sin_port=htons(server_port);
if((socket_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("nError openning socket");
close(socket_fd);
continue;
}
printf("nStart Connect ...");
fcntl(socket_fd, F_SETFL, O_NONBLOCK);//设为非阻塞
printf("nStart Connect 1 ...");
if(connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
printf("nError connect ...errno = %d ",errno);
sleep(10);
break;
}
else
{
break;
}
}
printf("nConnect OK...");
max_fd = (socket_fd > com_fd ? socket_fd : com_fd) + 1;
FD_ZERO(&rd_set);
FD_ZERO(&wr_set);
FD_ZERO(&er_set);
FD_SET(socket_fd,&rd_set);
FD_SET(socket_fd,&er_set);
timeout.tv_sec=2;
timeout.tv_usec=0;
printf("nStart select ");
while(1)
{
sleep(2);
// printf("nStart select 1 ");
ret=select(max_fd,&rd_set,&wr_set,&er_set,&timeout);
if (ret