| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1784 人关注过本帖
标题:刚封装的,用于屏蔽windows和linux差异的socket库函数,大家给点意见,谢谢
取消只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏
 问题点数:0 回复次数:2 
刚封装的,用于屏蔽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
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
快速回复:刚封装的,用于屏蔽windows和linux差异的socket库函数,大家给点意见, ...
数据加载中...
 
   



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

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