-
一、UDP服务器
-
创建
-
3、recvfrom--等待接收数据(无连接)(TCP用recv(有连接的))
- ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
- struct sockaddr *src_addr, socklen_t *addrlen);
-
功能:从sockfd指向的端点读取数据,如果sockfd所指向的端点没有数据则阻塞
-
参数:
- len :希望从sockfd里面读取len个字节的数据
功能代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
//创建套接字
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == sockfd)
{
perror("spck