//udp_client.h #include<iostream> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<string.h> #include<string> #include<stdlib.h> #include"comm.h" #define _IP_ "127.0.0.1" #define _PORT_ 8080 class udp_client { public: udp_client(string _ip=_IP_,short _port=_PORT_); void init(); int send_data(string& _in); int recv_data(string& _out); ~udp_client(); private: string ip; short port; int sock; }; //udp_client.cpp #include"udp_client.h" udp_client::udp_client(string _ip,short _port) :ip(_ip) ,port(_port) ,sock(-1) {} void udp_client::init() { sock=socket(AF_INET,SOCK_DGRAM,0); if(sock<0) { print_log(strerror(errno),__FUNCTION__,__LINE__); exit(1); } } int udp_client::send_data(string& _in) { struct sockaddr_in client; client.sin_family=AF_INET; client.sin_port=htons(port); client.sin_addr.s_addr=inet_addr(ip.c_str()); socklen_t len=sizeof(client); ssize_t _s=sendto(sock,_in.c_str(),_in.size(),0,(struct sockaddr*)&client,len); if(_s<0) { print_log(strerror(errno),__FUNCTION__,__LINE__); } return _s; } int udp_client::recv_data(string& _out) { struct sockaddr_in remote; socklen_t len=sizeof(remote); char buf[_SIZE_]; ssize_t _s=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&remote,&len); if(_s>0) { buf[_s]=‘\0‘; _out=buf; } else { print_log(strerror(errno),__FUNCTION__,__LINE__); } return _s; } udp_client::~udp_client() { if(sock>0) { close(sock); } } //main_client.cpp #include"udp_client.h" int main() { string str="helloworld"; udp_client _cl; _cl.init(); _cl.send_data(str); _cl.recv_data(str); cout<<"server: "<<str<<endl; } //udp_server.h #include<iostream> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<string.h> #include<string> #include<stdlib.h> #include"comm.h" #define _DEFAULT_IP_ "127.0.0.1" #define _DEFAULT_PORT_ 8080 class udp_server { public: udp_server(string _ip=_DEFAULT_IP_,short _port=_DEFAULT_PORT_); void init(); int send_data(string& _in,struct sockaddr_in& r); int recv_data(string& _out,struct sockaddr_in& r); int broadcast(); ~udp_server(); private: string ip; short port; int sock; }; //udp_server.cpp #include"udp_server.h" udp_server::udp_server(string _ip,short _port) :ip(_ip) ,port(_port) ,sock(-1) {} void udp_server::init() { sock=socket(AF_INET,SOCK_DGRAM,0); if(sock<0) { print_log(strerror(errno),__FUNCTION__,__LINE__); exit(1); } struct sockaddr_in local; local.sin_family=AF_INET; local.sin_port=htons(port); if(ip=="any") local.sin_addr.s_addr=htonl(INADDR_ANY); else local.sin_addr.s_addr=inet_addr(ip.c_str()); if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0) { print_log(strerror(errno),__FUNCTION__,__LINE__); exit(2); } } int udp_server::send_data(string& _in,struct sockaddr_in& r) { struct sockaddr_in client; client=r; socklen_t len=sizeof(client); ssize_t _s=sendto(sock,_in.c_str(),_in.size(),0,(struct sockaddr*)&client,len); if(_s<0) { print_log(strerror(errno),__FUNCTION__,__LINE__); } cout<<"send"<<_in<<endl; return _s; } int udp_server::recv_data(string& _out,struct sockaddr_in& r) { struct sockaddr_in remote; socklen_t len=sizeof(remote); char buf[_SIZE_]; ssize_t _s=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&remote,&len); if(_s>0) { buf[_s]=‘\0‘; cout<<"client: "<<buf<<endl; _out=buf; } else if(_s==0) { print_log("client is close",__FUNCTION__,__LINE__); } else { print_log(strerror(errno),__FUNCTION__,__LINE__); } r=remote; return _s; } int udp_server::broadcast() { return 0; } udp_server::~udp_server() { if(sock>0) { close(sock); } } //main_server.cpp #include"udp_server.h" int main() { udp_server _se; _se.init(); string out; struct sockaddr_in r; _se.recv_data(out,r); _se.send_data(out,r); cout<<inet_ntoa(r.sin_addr)<<r.sin_port<<endl; return 0; }
时间: 2024-10-12 21:10:44