函数参数无效
我正在编写一个使用套接字的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能调用了错误的约定。
在 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.
如果“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.