刚封装的,用于屏蔽windows和linux差异的socket库函数,大家给点意见,谢谢
程序代码:
/******************************************************************************* * * File Name: u_socket.c -- lib of socket fuctions * Copyright: * Author : *** *** * Date : 2010-07-01 * History : * Mender : *** *** * Date : 2010-07-01 * Content: create * *******************************************************************************/ #include "u_def.h" #include <stdio.h> #if (U_OS == OS_WIN32) #include <stdlib.h> #include <winsock2.h> #include <ws2tcpip.h> #pragma comment(lib,"ws2_32.lib") #elif (U_OS == OS_LINUX) #include <sys/socket.h> #include <sys/ioctl.h> #include <systypes.h> #include <netinet/in.h> #include <netinet/tcp.h> #include <arpa/inet.h> #include <netdb.h> #include <unistd.h> #endif #include "u_socket.h" #include "log.h" // static BOOL g_bEnableIpv6 = FALSE; /***************************************************************************/ /*************************************************************************** * @function initialize the win-sock library * @return success:0 ; fail:1 ***************************************************************************/ UINT32 SOCKET_Init(void) { #if (U_OS == OS_WIN32) INT32 iRet = FALSE; WSADATA wsaData; if ((iRet = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) { printf("WSAStartup() failed with error code %d\n", WSAGetLastError()); } return (UINT32)iRet; #elif (U_OS == OS_LINUX) return U_OK; #endif } /*************************************************************************** * @function create a new socket * @param * type IPPROTO_UDP stand for UDP,IPPROTO_TCP for TCP * @return success:the socket fd; fail:0 ***************************************************************************/ SOCKET SOCKET_Create(int type) { SOCKET sock; if(type == IPPROTO_UDP) { sock = socket(AF_INET, SOCK_DGRAM, 0); } else if(type == IPPROTO_TCP) { sock = socket(AF_INET, SOCK_STREAM, 0); } else // IPPROTO_IP { sock = socket(AF_INET, SOCK_RAW, 0); } if(sock < 0) { log_write("error:failed to creat socket\n"); return FALSE; } else { return sock; } } /*************************************************************************** * @function close the socket * @param * sockfd the socket to be closed * @return success:the socket fd; fail:0 ***************************************************************************/ UINT32 SOCKET_Close(SOCKET sockfd) { int n; if((n = closesocket(sockfd)) < 0) { log_write("error:close failed\n"); return U_ERR; } #if (U_OS == OS_WIN32) return (UINT32)WSACleanup(); #elif (U_OS == OS_LINUX) return U_OK; #endif } /*************************************************************************** * @function bind the socket * @param * stun_sock the socket to be bind addr the socket address to be bind * @return success:0; fail:~0 ***************************************************************************/ UINT32 SOCKET_Bind(SOCKET stun_sock, SocketAddress* addr) { int n; SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr = addr-& gt;ip; //用inet_addr转化,不用(htonl) addrSrv.sin_port = htons(addr->port); addrSrv.sin_family = addr->family; if( (n=bind(stun_sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR))) < 0 ) { log_write("error:bind failed%d \n", WSAGetLastError()); return U_ERR; } else { return U_OK; } } /*************************************************************************** * @function send data by UDP * @param * stun_sock The socket to send or receive data * data Buffer used to store send or received data * len The length of the buffer * addr The destination address * @return success:The data size; fail:~0 ***************************************************************************/ UINT32 SOCKET_Sendto(SOCKET stun_sock, void *data,int len,SocketAddress* addr) { int n; SOCKADDR_IN remoteaddr; remoteaddr.sin_addr.S_un.S_addr = addr->ip; remoteaddr.sin_port = htons(addr->port); remoteaddr.sin_family = addr->family; if( (n=sendto(stun_sock,data, len, 0, (SOCKADDR*)&remoteaddr,sizeof(SOCKADDR))) < 0 ) { log_write("sendto error, %d\n", WSAGetLastError()); return U_ERR; } return (UINT32)n; } /*************************************************************************** * @function recv data by the UDP * @param * stun_sock The socket to send or receive data * data Buffer used to store send or received data * len The length of the buffer * addrclient The source address * @return success:the socket fd; fail:~0 ***************************************************************************/ UINT32 SOCKET_Recvfrom(SOCKET stun_sock, void *data, int len, SocketAddress* addrclient) { int n; int addrlen; SOCKADDR_IN addr; addrlen = sizeof(addr); if( (n = recvfrom(stun_sock, data, len, 0, (SOCKADDR*)&addr, &addrlen)) < 0) { log_write("warning:recvfrom error%d\n", WSAGetLastError()); return U_ERR; } addrclient->ip = addr.sin_addr.S_un.S_addr; addrclient->family = (unsigned char)addr.sin_family; addrclient->port = ntohs(addr.sin_port); return (UINT32)n; } /*************************************************************************** * @function send data by TCP * @param * stun_sock The socket to send or receive data * data Buffer used to store send or received data * len The length of the buffer * @return success:The data size; fail:~0 ***************************************************************************/ UINT32 SOCKET_Send(SOCKET stun_sock, void *data, int len) { int n; if( (n=send(stun_sock,data, len, 0)) < 0 ) { log_write("send error, %d\n", WSAGetLastError()); return U_ERR; } return (UINT32)n; } /*************************************************************************** * @function receive data from net by TCP * @param * stun_sock The socket to send or receive data * data Buffer used to store send or received data * len The length of the buffer * @return success:the socket fd; fail:~0 ***************************************************************************/ UINT32 SOCKET_Recv(SOCKET stun_sock, void * data, int len) { int n; if( (n = recv(stun_sock, data, len, 0)) < 0 ) { log_write("error:recv error\n"); return U_ERR; } return (UINT32)n; }