我正在寫一個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;*/
}
有沒有人的想法?
好的,這工作。感謝:D – 2011-06-02 16:22:49