在main.c中声明了一个全局socket句柄,想在send.c中使用,但在第二次循环调用就无效了,数据发送不出去。
菜鸟求高手指点
main.c:
extern int sendtest(int i);
int sock = -1;
int ret = -1;
int main(int argc,char *argv[])
{
struct sockaddr_in server_listen_addr;
bzero(&server_listen_addr,sizeof(server_listen_addr));
server_listen_addr.sin_family=AF_INET;
server_listen_addr.sin_port=htons(1234);
inet_pton(AF_INET,”10.4.30.222″,(void*)&server_listen_addr.sin_addr);
if(sock == -1)
sock=socket(AF_INET,SOCK_STREAM,0);
if(ret == -1)
ret=connect(sock,(const struct sockaddr *)&server_listen_addr, sizeof(struct sockaddr));
菜鸟求高手指点
main.c:
extern int sendtest(int i);
int sock = -1;
int ret = -1;
int main(int argc,char *argv[])
{
struct sockaddr_in server_listen_addr;
bzero(&server_listen_addr,sizeof(server_listen_addr));
server_listen_addr.sin_family=AF_INET;
server_listen_addr.sin_port=htons(1234);
inet_pton(AF_INET,”10.4.30.222″,(void*)&server_listen_addr.sin_addr);
if(sock == -1)
sock=socket(AF_INET,SOCK_STREAM,0);
if(ret == -1)
ret=connect(sock,(const struct sockaddr *)&server_listen_addr, sizeof(struct sockaddr));
int i = 0;
for(;i<100000;++i)
sendtest(i);
return 0;
}
send.c:
extern int sock;
int sendtest(int i)
{
char buf[100] = “send data”;
int sendsize = send(sock, buf, bufsize, 0);
return 1;
}
解决方案
20
errno
40
用WSAGetLastError函数判断失败原因