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; }
61 JFile(socket(PF_INET, type, 0))
80 const int protocol = 0) :
81 JFile(socket(domain, type, protocol))
112 const int mask = FNDELAY;
118 if (((flags & mask) != mask && on) ||
119 ((flags & mask) != 0 && !on) ) {
136 const int mask = FNDELAY;
142 return ((flags & mask) != 0);
153 setOption(SOL_SOCKET, SO_KEEPALIVE,
int(on ? 1 : 0));
164 return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
208 setOption(SOL_SOCKET, SO_REUSEADDR,
int(on ? 1 : 0));
219 return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
230 setOption(IPPROTO_TCP, TCP_NODELAY,
int(on ? 1 : 0));
241 return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
252 setOption(SOL_SOCKET, SO_RCVBUF,
int(size));
263 return getOption<int>(SOL_SOCKET, SO_RCVBUF);
274 setOption(SOL_SOCKET, SO_SNDBUF,
int(size));
285 return getOption<int>(SOL_SOCKET, SO_SNDBUF);
300 virtual int read(
char* buffer,
const int length)
override
308 }
else if (pos < 0) {
339 virtual int write(
const char* buffer,
const int length)
override
347 }
else if (pos < 0) {
374 socklen_t size =
sizeof(sockaddr_in);
396 void connect(
const std::string& host_name,
const int port)
408 void connect(
const int ip_number,
const int port)
428 void setOption(
const int level,
const int option,
const T value)
430 socklen_t size =
sizeof(
T);
449 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.
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.