函数参数无效

发布于 2024-11-13 00:33:51 字数 2844 浏览 0 评论 0原文

我正在编写一个使用套接字的 C 代码。我在Windows和Linux上使用这个,所以我尝试动态地编写它。我编写了两个函数来获取服务器/客户端套接字。我在编译时遇到错误:

debian:~/cross_socket# make
gcc *.c -Wall
i586-mingw32msvc-gcc *.c -Wall -lws2_32
socket.c: In function 'get_client_socket':
socket.c:28: warning: passing argument 3 of 'get_socket' from incompatible pointer type
socket.c:13: note: expected 'int (*)(SOCKET,  const struct sockaddr *, int)' but argument is of type 'int (*)(SOCKET,  const struct sockaddr *, int)'
socket.c: In function 'get_server_socket_addr':
socket.c:43: warning: passing argument 3 of 'get_socket' from incompatible pointer type
socket.c:13: note: expected 'int (*)(SOCKET,  const struct sockaddr *, int)' but argument is of type 'int (*)(SOCKET,  const struct sockaddr *, int)'

本地系统的 gcc 工作得很好,但如果我使用 mingw,我会收到此错误?!? 它说: 我使用 int (*)(SOCKET, const struct sockaddr *, int) 而不是 int (*)(SOCKET, const struct sockaddr *, int)...?

这里是相关代码:

inline SOCKET get_socket(uint16_t port, in_addr_t socket_in_addr, int (*create)(SOCKET, 
const struct sockaddr*, socklen_t)) {
        SOCKADDR_IN addr;
        SOCKET result_socket = GET_SOCKET;
        if (SOCKET_NOK(result_socket))
                return SOCKET_INIT_ERROR;
        memset(&addr, 0, sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = socket_in_addr;
        if (CONNECT_NOK(create(result_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR))))
                return SOCKET_INIT_ERROR;
        return result_socket;
}

SOCKET get_client_socket(char* target, uint16_t port) {
        return get_socket(port, inet_addr(target), &connect);
/*      SOCKADDR_IN addr;
        SOCKET client_socket = GET_SOCKET;
        if (SOCKET_NOK(client_socket))
                return SOCKET_INIT_ERROR;
        memset(&addr,0,sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(target);
        if (CONNECT_NOK(connect(client_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR))))
                return SOCKET_INIT_ERROR;
        return client_socket;*/
}

SOCKET get_server_socket_addr(uint16_t port, in_addr_t socket_in_addr) {
        return get_socket(port, socket_in_addr, &bind);
/*      SOCKADDR_IN addr;
        SOCKET server_socket = GET_SOCKET;
        if (SOCKET_NOK(server_socket))
                return SOCKET_INIT_ERROR;
        memset(&addr, 0, sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = socket_in_addr;
        if (CONNECT_NOK(bind(server_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN))))
                return SOCKET_INIT_ERROR;
        return server_socket;*/
}

有人有想法吗?

I 'm writing a C-Code in which i use sockets. I use this on windows and on linux, so i try to write it dynamicly. I wrote two functions to get a server/client-socket. There i have an error on compiling:

debian:~/cross_socket# make
gcc *.c -Wall
i586-mingw32msvc-gcc *.c -Wall -lws2_32
socket.c: In function 'get_client_socket':
socket.c:28: warning: passing argument 3 of 'get_socket' from incompatible pointer type
socket.c:13: note: expected 'int (*)(SOCKET,  const struct sockaddr *, int)' but argument is of type 'int (*)(SOCKET,  const struct sockaddr *, int)'
socket.c: In function 'get_server_socket_addr':
socket.c:43: warning: passing argument 3 of 'get_socket' from incompatible pointer type
socket.c:13: note: expected 'int (*)(SOCKET,  const struct sockaddr *, int)' but argument is of type 'int (*)(SOCKET,  const struct sockaddr *, int)'

gcc for my local system works great, but if i use mingw i get this error?!?
It says:
I use int (*)(SOCKET, const struct sockaddr *, int) instead of int (*)(SOCKET, const struct sockaddr *, int)... ?

Here the relevant code:

inline SOCKET get_socket(uint16_t port, in_addr_t socket_in_addr, int (*create)(SOCKET, 
const struct sockaddr*, socklen_t)) {
        SOCKADDR_IN addr;
        SOCKET result_socket = GET_SOCKET;
        if (SOCKET_NOK(result_socket))
                return SOCKET_INIT_ERROR;
        memset(&addr, 0, sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = socket_in_addr;
        if (CONNECT_NOK(create(result_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR))))
                return SOCKET_INIT_ERROR;
        return result_socket;
}

SOCKET get_client_socket(char* target, uint16_t port) {
        return get_socket(port, inet_addr(target), &connect);
/*      SOCKADDR_IN addr;
        SOCKET client_socket = GET_SOCKET;
        if (SOCKET_NOK(client_socket))
                return SOCKET_INIT_ERROR;
        memset(&addr,0,sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(target);
        if (CONNECT_NOK(connect(client_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR))))
                return SOCKET_INIT_ERROR;
        return client_socket;*/
}

SOCKET get_server_socket_addr(uint16_t port, in_addr_t socket_in_addr) {
        return get_socket(port, socket_in_addr, &bind);
/*      SOCKADDR_IN addr;
        SOCKET server_socket = GET_SOCKET;
        if (SOCKET_NOK(server_socket))
                return SOCKET_INIT_ERROR;
        memset(&addr, 0, sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = socket_in_addr;
        if (CONNECT_NOK(bind(server_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN))))
                return SOCKET_INIT_ERROR;
        return server_socket;*/
}

Has anyone an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-11-20 00:33:51

可能调用了错误的约定。

在 Windows 上,大多数 API 函数都定义为 WINAPI,即 __stdcall,又名 PASCAL。即参数从左到右传递,并且堆栈由被调用者清理。

OTOH 默认编译调用约定是 __cdecl。

PS 如果 mingw 打印出更详细的错误信息就好了。

Probably calling wrong convention.

On Windows most of the API functions are defined as WINAPI, which is __stdcall, a.k.a. PASCAL. i.e. parameters are passed left-to-right, and the stack is cleaned by the callee.

OTOH default compilation calling convention is __cdecl.

P.S. It'd be nice if mingw printed a more detailed error information.

写下不归期 2024-11-20 00:33:51

如果“connect”是一个函数,您应该将其传递为“connect”,而不是“&connect”。函数的名称已经是指向它的指针。

If "connect" is a function, you should just pass it as "connect", not "&connect". The name of the function is already a pointer to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文