小编use*_*197的帖子

Linux Socket:如何在客户端程序中检测断开的网络?

我正在调试基于ac的linux socket程序.正如网站上提供的所有示例一样,我应用了以下结构:

sockfd= socket(AF_INET, SOCK_STREAM, 0);

connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

send_bytes = send(sockfd, sock_buff, (size_t)buff_bytes, MSG_DONTWAIT);
Run Code Online (Sandbox Code Playgroud)

当删除服务器关闭其服务器程序时,我可以检测到断开连接.但是如果我拔掉以太网电缆,send函数仍会返回正值而不是-1.

假设我无法更改服务器端,如何检查客户端程序中的网络连接?

c sockets linux send disconnection

13
推荐指数
1
解决办法
5万
查看次数

C函数中的指针赋值

为什么我不能在函数中指定一个点.正如您在以下代码中注意到的那样.函数返回后,我无法将指针p1指向正确的地址.但是使用全局指针*p,我可以存储地址信息.

#include <stdio.h>
#include <stdlib.h>

int *p = NULL;
void test(int * pt1, int**pt2){
    p = (int*)malloc(sizeof(int));    
    pt1 = p;
    *pt2 = p;
    printf("p points to %p\n", p);
    printf("pt1 points to %p\n", pt1);
    printf("pt2 points to %p\n", *pt2);
}

int main(void) {
    int *p1 = NULL; 
    int *p2 = NULL;

    printf("p points to %p\n", p);
    printf("p1 points to %p\n", p1);
    printf("p2 points to %p\n", p2);

    test(p1, &p2);

    printf("p points to %p\n", p);
    printf("p1 points to %p\n", p1);
    printf("p2 points to …
Run Code Online (Sandbox Code Playgroud)

c pointers function variable-assignment

5
推荐指数
1
解决办法
4797
查看次数