我正在编写一个使用套接字的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;*/
}
有人有想法吗?
答案 0 :(得分:0)
如果“connect”是一个函数,您应该将其作为“connect”传递,而不是“& connect”。函数的名称已经是指向它的指针。
答案 1 :(得分:0)
可能是错误的约定。
在Windows上,大多数API函数都定义为WINAPI
,即__stdcall
,a.k.a。PASCAL。即参数从左向右传递,并且被调用者清理堆栈。
OTOH默认编译调用约定为__cdecl
。
P.S。如果mingw打印出更详细的错误信息,那就太好了。