当前位置: 代码迷 >> 综合 >> Linux学习笔记15 udp通信
  详细解决方案

Linux学习笔记15 udp通信

热度:52   发布时间:2023-09-29 16:03:51.0

udp通信相比与tcp通信,不再需要三次握手和四次挥手,对应到程序上就不需要服务器去accept和客户端connect

 

udp通信服务端

#include <sys/types.h> 
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>using namespace std;int main()
{int fd=socket(AF_INET, SOCK_DGRAM, 0);
//只需要一个套接字文件描述符就够了,不用像tcp,需要一个负责接收客户端,一个负责和客户端通信
//注意第二个参数和TCP不一样if (fd==-1){cout<<"socket is error!";exit(1);}struct sockaddr_in ser_addr;ser_addr.sin_family=AF_INET;ser_addr.sin_port=htons(8888);inet_pton(AF_INET, "127.0.0.1", &ser_addr.sin_addr.s_addr);bind(fd, (struct sockaddr*)&ser_addr, sizeof(ser_addr));//绑定服务端本地ip和端口char buf[256];char response[]="message is received";	struct sockaddr_in cli_addr;socklen_t cli_addr_len=sizeof(cli_addr);while (1){
/*不需要再对recvfrom返回值做判断,因为udp没有三次握手和四次挥手的过程,不需要人为判断是否传输结束,然后去结束两者之间的通信,客户端发来信息,客户端就不再和服务端有联系了*/int n=recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&cli_addr, &cli_addr_len);
//只能使用recvfrom和sendto函数去收发数据,因为需要在收发数据同时加入对方的IP和端口信息,这里在接收时,会把对方的ip和端口接收到,	cout<<buf<<endl;sendto(fd, response, strlen(response)+1, 0, (struct sockaddr*)&cli_addr, sizeof(cli_addr));		
//这里在发送时会将收到的ip和端口信息再发出去,指明发送的目标}close(fd);return 0;
}

 

udp通信客户端

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h> 
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>using namespace std;int main()
{int fd=socket(AF_INET, SOCK_DGRAM, 0);if (fd==-1){cout<<"socket is error!";exit(1);}struct sockaddr_in ser_addr;ser_addr.sin_family=AF_INET;ser_addr.sin_port=htons(8888);inet_pton(AF_INET, "127.0.0.1", &ser_addr.sin_addr.s_addr);//绑定服务端的ip和端口号char buf[256];	while (1){fgets(buf, sizeof(buf), stdin);sendto(fd, buf, strlen(buf)+1, 0, (struct sockaddr*)&ser_addr, sizeof(ser_addr));
//将数据发送到ser_addr指定的ip和端口int n=recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
//从主机接收时,因为已经知道主机的端口和IP了,所以后面两个参数可以填NULL。cout<<buf<<endl;		}close(fd);return 0;
}