1 #ifndef _NET_SOCKET_H__
2 #define _NET_SOCKET_H__
5 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <arpa/inet.h>
91 return ((sid = socket(PF_INET, type, 0)) != -1);
102 return ((sid = socket(PF_UNIX, type, 0)) != -1);
114 setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &flags,
sizeof(flags));
116 if(listen(sid, nstack) != 0)
return false;
147 socklen_t len = from_address->
GetSize();
148 return accept(sid, from_address->
GetSockAddr(), &len);
157 Socket& SetBlockingMode(
bool state =
true);
162 bool IsBlockingMode();
173 int Receive(
void *buf,
int len,
bool prevent_block =
false);
185 int ReceiveFrom(
Address *address,
void *buf,
int len,
186 bool prevent_block =
false);
197 int Send(
void *buf,
int len,
bool prevent_block =
false);
209 int SendTo(
const Address& address,
void *buf,
int len,
210 bool prevent_block =
false);
219 bool SendDescriptor(
const Address& address,
int fd,
int aux = 0);
232 int WaitForInput(
int time_out = -1);
239 int WaitForOutput(
int time_out = -1);
249 return !setsockopt(sid, IPPROTO_TCP, TCP_NODELAY, &val,
sizeof(val));
258 bool ReceiveDescriptor(
int *fd,
int *aux = NULL);
265 if(sid != -1) close(sid);
Socket & operator=(int nsid)
Copy asignment.
Definition: socket.h:77
bool IsValid() const
Definition: socket.h:69
void Close()
Closes the socket.
Definition: socket.h:263
bool BindTo(const Address &address)
Binds the socket to the specified address.
Definition: socket.h:135
bool OpenInet(int type=SOCK_STREAM)
This method creates a new Internet socket, storing its identifier in the object.
Definition: socket.h:89
bool OpenUnix(int type=SOCK_STREAM)
This method creates a new UNIX socket, storing its identifier in the object.
Definition: socket.h:100
bool SetNoDelay(int val=1)
Configures the parameter TCP_NODELAY of the socket.
Definition: socket.h:247
int sid
Socket id.
Definition: socket.h:31
Socket()
Initializes the socket id with an invalid value.
Definition: socket.h:37
Contains classes to easy the utilization of sockets, specially implemented for UNIX systems...
Definition: address.h:15
~Socket()
The destructor does not closes the socket!.
Definition: socket.h:272
Socket(const Socket &xs)
Copy constructor.
Definition: socket.h:53
virtual int GetSize() const =0
Returns the size in bytes of the sockaddr structure returned by the previous method.
int Accept(Address *from_address)
If it is a server socket, it accepts a new connection.
Definition: socket.h:145
virtual sockaddr * GetSockAddr() const =0
Returns a pointer to a sockaddr structure.
Abstract base class to wrap the sockaddr derived structures.
Definition: address.h:28
bool ListenAt(const Address &address, int nstack=10)
Configures the socket for listening incoming connections.
Definition: socket.h:111
Socket(int s)
Initializes the socket id with an integer value.
Definition: socket.h:45
bool ConnectTo(const Address &to_address)
Connects the socket to a server.
Definition: socket.h:125
This class has been designed to work with UNIX sockets in an easy and object oriented way...
Definition: socket.h:28