Socekt编程之send和recv

头文件:#include <sys/types.h>   #include <sys/socket.h>

1、send()
定义函数:int send(int s, void *buf, int len, unsigned, int flags);
参数s:指定发送端套接字描述符。
参数buf:存放应用程序要发送数据的缓冲区。
参数len:实际要发送的数据的字节数。

参数flag:一般设为0.

2、recv()
定义函数:int recv(int s, void *buf, int len, unsigned int flags);
函数说明:recv()用来接收远端主机经指定的socket 传来的数据, 并把数据存到由参数buf 指向的内存空间, 参数len 为可接收数据的最大长度。
参数 flags 一般设0。 其他数值定义如下:
1、MSG_OOB 接收以out-of-band 送出的数据。
2、MSG_PEEK 返回来的数据并不会在系统内删除, 如果再调用recv()会返回相同的数据内容。
3、MSG_WAITALL 强迫接收到len 大小的数据后才能返回, 除非有错误或信号产生。

4、MSG_NOSIGNAL 此操作不愿被SIGPIPE 信号中断返回值成功则返回接收到的字符数, 失败返回-1,错误原因存于errno 中。


应用:tinyhttpd

/**********************************************************************/  
/* 从socket读取一行数据。以\r或\r\n为行结束符 
* Parameters: the socket descriptor 
*             the buffer to save the data in 
*             the size of the buffer 
* Returns: the number of bytes stored (excluding null) */  
/**********************************************************************/  
int get_line(int sock, char *buf, int size)  
{  
    int i = 0;  
    char c = '\0';  
    int n;  
  
    //至多读取size-1个字符,最后一个字符置'\0'  
    while ((i < size - 1) && (c != '\n'))  
    {  
        n = recv(sock, &c, 1, 0);//单个字符接收  
  
        if (n > 0)  
        {  
            if (c == '\r')//如果是回车符,继续读取  
            {  
                /*使用 MSG_PEEK 标志使下一次读取依然可以得到这次读取的内容,可认为接收窗口不滑动*/  
                n = recv(sock, &c, 1, MSG_PEEK);  
  
                if ((n > 0) && (c == '\n'))//如果是回车换行符  
                    recv(sock, &c, 1, 0);//继续接收单个字符,实际上和上面那个标志位MSG_PEEK读取同样的字符,读完后删除输入队列的数据,即滑动窗口,c=='\n'  
                else  
                    c = '\n';//只是读取到回车符,则置为换行符,也终止了读取  
            }  
            buf[i] = c;//放入缓冲区  
            i++;  
        }  
        else//没有读取到任何数据  
            c = '\n';  
    }  
    buf[i] = '\0';  
  
    return(i);//返回读到的字符个数(包括'\0')  
}  
/**********************************************************************/  
/* 返回客户端404错误信息 404(万恶的404) */  
/**********************************************************************/  
void not_found(int client)  
{  
    char buf[1024];  
  
    sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, SERVER_STRING);  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "Content-Type: text/html\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "<BODY><P>The server could not fulfill\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "your request because the resource specified\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "is unavailable or nonexistent.\r\n");  
    send(client, buf, strlen(buf), 0);  
    sprintf(buf, "</BODY></HTML>\r\n");  
    send(client, buf, strlen(buf), 0);  
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值