21 #include <sys/types.h> 22 #include <sys/socket.h> 23 #include <arpa/inet.h> 41 if(
init){
return false; }
43 sock = socket(AF_INET, SOCK_DGRAM, 0);
44 if(
sock < 0){
return false; }
49 serv.sin_family = AF_INET;
50 serv.sin_addr.s_addr = INADDR_ANY;
51 serv.sin_port = htons(port_);
53 if(bind(
sock, (
struct sockaddr *)&
serv,
length) < 0){
return false; }
55 fromlen =
sizeof(
struct sockaddr_in);
64 if(!
init){
return -1; }
67 bzero(message_, length_);
69 int nbytes = (int)recvfrom(
sock, message_, length_, 0, (
struct sockaddr *)&
from, &
fromlen);
74 if(!
init){
return -1; }
76 return (
int)sendto(
sock, message_, length_, 0, (
struct sockaddr *)&
from,
fromlen);
104 if(
init){
return false; }
106 sock = socket(AF_INET, SOCK_DGRAM, 0);
107 if(
sock < 0){
return false; }
109 serv.sin_family = AF_INET;
110 hp = gethostbyname(address_);
111 if(!hp){
return false; }
113 bcopy((
char *)hp->h_addr, (
char *)&
serv.sin_addr, hp->h_length);
114 serv.sin_port = htons(port_);
115 length =
sizeof(
struct sockaddr_in);
121 if(!
init){
return -1; }
124 bzero(message_, length_);
126 return (
int)recvfrom(
sock, message_, length_, 0, (
struct sockaddr *)&
from, &
length);
130 if(!
init){
return -1; }
132 return (
int)sendto(
sock, message_, length_, 0, (
const struct sockaddr *)&
serv,
length);
void Close()
Close the socket.
void Close()
Close the socket.
Provides network connectivity for poll2.
int RecvMessage(char *message_, size_t length_)
int SendMessage(char *message_, size_t length_)
bool Init(const char *address_, int port_)
int RecvMessage(char *message_, size_t length_)
int SendMessage(char *message_, size_t length_)
bool Init(int port_, int sec_=10, int usec_=0)
Initialize the serv object and open a specified port. Returns false if the socket fails to open or th...