gpt4 book ai didi

c - 已接受来自 00 :00:00:00:00:00; after connect 的连接

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:17 25 4
gpt4 key购买 nike

我有一个服务器套接字,它使用 setsockopt 监听超时读取。服务器在连接上等待一段时间,一段时间后接受来自“从 00:00:00:00:00:00 接受的连接”的连接。如何解决这个问题。粘贴服务器代码。出现此错误时我没有运行客户端代码。

void * serverMainThread(void* arg)
{
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
char*tokenCommand,*tokenValue;
int s, client, bytes_read, bytes_written, count = 0, distance;
socklen_t opt = sizeof(rem_addr);
struct timeval tv;
int socketflag=true, loopflag=true;

// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if(s<0)perror("socket call error");
// bind socket to port 1 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = (uint8_t) 1;
int b = bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
if(b<0) perror("error in bind");
// put socket into listening mode
int l = listen(s, 1);
if(l<0)perror("error in listen");
tv.tv_sec = 65; // 30 seconds
tv.tv_usec = 0; // microsecs, set to 0 (or ...)
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv,sizeof(struct timeval));

while(true)
{
printf("for new process->socket count = %d", count);
sleep(10);
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
printf("enter a number to continue\n");
scanf("%d",&distance);
if(client<0) perror("server thread accept");
int forkRet = fork();
loopflag= socketflag = true;
if(forkRet >= 0)
{
if(forkRet == 0)//child thread
{
childPid = getpid();
printf("pid of new child created =%d\n",(int)childPid);
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
printf("enter a number to continue\n");
scanf("%d",&distance);
memset(buf, 0, sizeof(buf));
while(socketflag)
{
// read data from the client
printf("\nbefore read\n");
bytes_read = read(client, buf, sizeof(buf));
if(bytes_read>=0) buf[bytes_read] ='\0';
printf("%d bytes read\n",bytes_read);
if(bytes_read> 0)
{
loopflag = false;
printf("\npid of current =%d\n",(int)childPid);
distance = findDistance(buf);
if(distance >= 10)
{
ringAlarm();
strcpy(buf,"Ring");
bytes_written = write(client, buf , strlen(buf));
}
else
{
bytes_written = write(client, "No Ring" , 8);

}
}
else
{
if(!loopflag)
{
printf("as loop flag is false, make socketflag false");
socketflag = false;
}
}
}
close(client);
close(s);
printf("\n child process exit\n");
exit(0);
}
else//parent
{
wait(0);
close(client);
// close(s);
printf("\n parent exit\n");
}
}
}

// close connection
//close(client);
//close(s);
}

最佳答案

问题是您没有检查来自 accept() 的错误。你应该这样写:

client = accept(s, (struct sockaddr *)&rem_addr, &opt);
if (client < 0)
perror("error in accept");

(顺便说一句,您应该在检测到这些错误后返回或中止或采取其他措施,继续就好像它没有发生一样会产生不良后果。)

如果您以这种方式运行程序,您将获得:

error in accept: Resource temporarily unavailable

这对应于 EAGAIN 代码,这是一个没有很好记录的 timeout 错误代码。这是意料之中的,因为您在监听套接字上设置了 SO_RCVTIMEO!全零地址的发生是因为您没有真正获得连接,因此 sockaddr 结构保持零初始化。

您有多种选择:

  • 从监听套接字中删除 SO_RCVTIMEO,如果需要,将其添加到 client 套接字。
  • 检查 accept() 和循环中的 EAGAIN 错误。
  • 检查 accept() 中的 EAGAIN 错误(或任何其他错误代码)并返回给调用者。

关于c - 已接受来自 00 :00:00:00:00:00; after connect 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47512308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com