标题:刚封装的,用于屏蔽windows和linux差异的socket库函数,大家给点意见,谢谢
只看楼主
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
 问题点数:0 回复次数:4 
刚封装的,用于屏蔽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;
}

搜索更多相关主题的帖子: windows socket linux 函数 意见 
2010-07-01 00:44
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
socket.rar (1.06 MB)


网上看到的一个C++ 封装的socket库

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-07-04 23:25
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
得分:0 
等级太低了,完全看不懂

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2010-07-05 00:05
parkour
Rank: 2
等 级:论坛游民
帖 子:63
专家分:39
注 册:2009-1-3
得分:0 
最好不要把出错信息在你的函数中输出

因为要输出什么东西是应用层来告诉用户的

比如我要隐藏细节的话不管它有什么错误我都只要输出 “Fail in trans....”

还有就是在recvfrom中完全不必要把后面的东西给算出来

人家又不一定就要那些东西,再说了那些信息完全可以在recv_buf中还原出来
2010-07-05 21:21
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
以下是引用parkour在2010-7-5 21:21:31的发言:

最好不要把出错信息在你的函数中输出

因为要输出什么东西是应用层来告诉用户的

比如我要隐藏细节的话不管它有什么错误我都只要输出 “Fail in trans....”

还有就是在recvfrom中完全不必要把后面的东西给算出来

人家又不一定就要那些东西,再说了那些信息完全可以在recv_buf中还原出来
这样的话,在程序调试的时候不是不好找错误了?

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-07-05 22:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-311785-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.203483 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved