#include"udp_socket.h" int udp_client(char *sendData, char *recvData,int recvBuffLen,int port,char *ip){ int cclient ; //创建udp socket /*******Begin 1 ****/ /******End 1 ******/ if(cclient == -1) { … } close(cclient); return 0; }补全代码
时间: 2023-06-29 21:11:58 浏览: 83
#include "udp_socket.h"
int udp_client(char *sendData, char *recvData, int recvBuffLen, int port, char *ip) {
int cclient ; //创建udp socket
struct sockaddr_in server_addr;
/*******Begin 1 ****/
cclient = socket(AF_INET, SOCK_DGRAM, 0); //创建UDP socket
if(cclient == -1) {
perror("socket error");
return -1;
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
/******End 1 ******/
sendto(cclient, sendData, strlen(sendData),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
socklen_t len = sizeof(server_addr);
int n = recvfrom(cclient, recvData, recvBuffLen, 0, (struct sockaddr *)&server_addr, &len);
if(n < 0) {
perror("recvfrom error");
return -1;
}
close(cclient);
return 0;
}
阅读全文