服务器端的代码(serve.c)
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
int init_socket(char* ip , short port)//UDP传输
{
//1.创建一个套接字
int fd = socket(AF_INET,SOCK_DGRAM,0);
if(fd == -1)
{
perror("create socket error");
return -1;
}
//2指定服务器的IP和端口号
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);//inet_addr的功能是将一个ip地址字符串转换成一个整数值
//3.bind
int r = bind(fd,(struct sockaddr*)&addr,sizeof(addr));