有关socket数据传输的函数

/**
 * mysock.h
 * socket data transmission function prototype
 * @author : MJN
 * @create : 2011/11/27
 * @update : 2011/11/27
 */

#include <WinSock2.h>

int readAll(SOCKET socket, char * buf, int n);
int writeAll(SOCKET socket, char * buf, int n);
int readInt(SOCKET socket, int *value);
int writeInt(SOCKET socket, int value);
int readString(SOCKET socket, char * str, int size);
int writeString(SOCKET socket, char * str);

/**
 * mysock.c
 * socket data transmission function
 * @author : MJN
 * @create : 2011/11/27
 * @update : 2011/11/27
 */

#include "mysock.h"

/**
 * @function : read n bytes from socket to buf
 * @buf      : store bytes that read from socket
 * @return   : the number of bytes received
 * @author   : MJN
 * @create   : 2011-11-02
 * @update   : 2011-11-02
 */
int readAll(SOCKET socket, char *buf, int n)
{
    int nleft = n;          /* 剩余字节数 */
    int nbytes;             /* 已读字节数 */
    char *ptr = buf;

    while (nleft > 0)
    {
        nbytes = recv(socket, ptr, nleft, 0);
        if (nbytes < 0)
        {
            //printf("read error");
            return -1;
        }
        if (nbytes == 0)
        {
            break;    /* no more data to read */
        }
        nleft -= nbytes;
        ptr += nbytes;
    }
    return n - nleft;
}

/**
 * @function : write n bytes to socket
 * @return   : the number of bytes sent
 * @author   : MJN
 * @create   : 2011-11-02
 * @update   : 2011-11-02
 */
int writeAll(SOCKET socket, char *buf, int n)
{
    int nleft = n;          /* 剩余字节数 */
    int nbytes;             /* 已写字节数 */
    char *ptr = buf;        /* 指向正在写的字串位置 */

    while (nleft > 0)
    {
        nbytes = send(socket, ptr, nleft, 0);
        if (nbytes <= 0)
        {
            //printf("write error");
            return -1;
        }
        nleft -= nbytes;
        ptr += nbytes;
    }
    return n;
}

/**
 * @function : read an integer(4 bytes)
 * @return   : the number of bytes read
 * @author   : MJN
 * @create   : 2011-11-02
 * @update   : 2011-11-02
 */
int readInt(SOCKET socket, int *value)
{
    int n;
    int ret = readAll(socket, (char *)&n, 4);
    *value = ntohl(n);
    return ret;
}

/**
 * @function : write an integer(4 bytes)
 * @return   : the number of bytes write
 * @author   : MJN
 * @create   : 2011-11-02
 * @update   : 2011-11-02
 */
int writeInt(SOCKET socket, int value)
{
    int n = htonl(value);
    return writeAll(socket, (char *)&n, 4);
}

/**
 * @function : read string(default encoding depends on platform)
               默认中文平台的编码串--不一定是UTF8串
               可能是GB2312-80或GBK或GB18030
 * @return   : the number of bytes read
 * @author   : MJN
 * @create   : 2011-11-02
 * @update   : 2011-11-02
 */
int readString(SOCKET socket, char *str, int size)
{
    int len = 0;
    int n = readInt(socket, &len);/* read length of string */
    memset(str, 0, size);
    n = readAll(socket, str, min(len, size - 1));
    return n;
}

/**
 * @function : write string(default encoding depends on platform)
 * @return   : the number of bytes write
 * @author   : MJN
 * @create   : 2011-11-02
 * @update   : 2011-11-02
 */
int writeString(SOCKET socket, char *str)
{
    int len = strlen(str);
    writeInt(socket, len);
    return writeAll(socket, str, len);
}

注:

测试环境:Microsoft Visual Studio 2010

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值