1 #ifndef __JNET__JSOCKET__
2 #define __JNET__JSOCKET__
4 #include <sys/socket.h>
6 #include <netinet/tcp.h>
7 #include <netinet/in.h>
29 namespace JPP {
using namespace JNET; }
62 JFile(socket(PF_INET, type, 0))
81 const int protocol = 0) :
82 JFile(socket(domain, type, protocol))
113 const int mask = FNDELAY;
119 if (((flags & mask) != mask && on) ||
120 ((flags & mask) != 0 && !on) ) {
137 const int mask = FNDELAY;
143 return ((flags & mask) != 0);
154 setOption(SOL_SOCKET, SO_KEEPALIVE,
int(on ? 1 : 0));
165 return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
209 setOption(SOL_SOCKET, SO_REUSEADDR,
int(on ? 1 : 0));
220 return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
231 setOption(IPPROTO_TCP, TCP_NODELAY,
int(on ? 1 : 0));
242 return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
253 setOption(SOL_SOCKET, SO_RCVBUF,
int(size));
264 return getOption<int>(SOL_SOCKET, SO_RCVBUF);
275 setOption(SOL_SOCKET, SO_SNDBUF,
int(size));
286 return getOption<int>(SOL_SOCKET, SO_SNDBUF);
301 virtual int read(
char* buffer,
const int length)
override
309 }
else if (pos < 0) {
340 virtual int write(
const char* buffer,
const int length)
override
348 }
else if (pos < 0) {
375 socklen_t size =
sizeof(sockaddr_in);
397 void connect(
const std::string& host_name,
const int port)
409 void connect(
const int ip_number,
const int port)
429 void setOption(
const int level,
const int option,
const T value)
431 socklen_t size =
sizeof(
T);
450 socklen_t size =
sizeof(
T);
void setReuseAddress(const bool on)
Set reuse address.
int getIPnumber(const std::string &host_name)
Get IP number.
int getSendBufferSize() const
Get send buffer size.
bool getReuseAddress() const
Get reuse address.
void setSendBufferSize(const int size)
Set send buffer size.
int getReceiveBufferSize() const
Set receive buffer size.
void setKeepIdle(const int t_s)
Set the TCP idle time.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
bool getNonBlocking() const
Get non-blocking of I/O.
std::string getIPaddress(const int ip)
Get IP address (decimal-dot notation).
virtual int read(char *buffer, const int length)
Read data from file.
void setIPnumber()
Set any IP number.
const sockaddr * getSockaddr() const
Get sockaddr.
virtual int read(char *buffer, const int length) override
Read data from socket.
bool getKeepAlive() const
Get keep alive of socket.
The JFile class extends the JAbstractFile class.
void connect(const int ip_number, const int port)
Connect to port on specified host.
int getFileDescriptor() const
Get file descriptor.
void accept(const int server)
Accept connection from a server.
void setKeepIntvl(const int t_s)
Set the TCP interval time.
int shutdown()
Shut down socket.
The JSocketAddress class encapsulates the sockaddr_in data structure.
void setKeepCnt(const int count)
Set the TCP idle count.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void setPort(const int port)
Set port number.
JSocket(const int type)
Constructor.
JSocket(const int domain, const int type, const int protocol=0)
Constructor.
void setReceiveBufferSize(const int size)
Set receive buffer size.
std::string getHostname()
Get host name.
T getOption(const int level, const int option) const
Get socket option.
void connect(const int port)
Connect to port on local host.
void setNonBlocking(const bool on)
Set non-blocking of I/O.
bool getTcpNoDelay() const
Get TCP no-delay.
void setKeepAlive(const bool on)
Set keep alive of socket.
void setTcpNoDelay(const bool on)
Set TCP no-delay.
JSocket()
Default constructor.
void connect(const std::string &host_name, const int port)
Connect to port on specified host.
void setFamily(const int family)
Set family.
virtual int write(const char *buffer, const int length) override
Write data to socket.
void setOption(const int level, const int option, const T value)
Set socket option.
virtual int write(const char *buffer, const int length)
Write data to file.
Hostname and IP address functions.