|
simple-socket library 1.2.0
|
TCP socket class. More...
#include <TCPSocket.h>
Public Types | |
| typedef SocketHandle< TCPSocket > | Handle |
| Handle for a new socket returned by accept. | |
| enum | ShutdownDirection { STOP_SEND = SHUT_WR , STOP_RECEIVE = SHUT_RD , STOP_BOTH = SHUT_RDWR } |
Public Member Functions | |
| Handle | accept () const |
| wait for another socket to connect | |
| void | bind (std::string_view localAddress, unsigned short localPort=0) |
| bind socket to address / port | |
| void | bind (unsigned short localPort=0) |
| void | connect (std::string_view foreignAddress, unsigned short foreignPort) |
| establish a connection with the given foreign address and port | |
| void | disconnect () |
| Disconnect and unset any foreign addresses. | |
| std::string | getForeignAddress () const |
| unsigned short | getForeignPort () const |
| std::string | getLocalAddress () const |
| unsigned short | getLocalPort () const |
| void | listen (int backlog=5) |
| listen for incoming connections | |
| int | nativeHandle () |
| return the native handle of the open socket | |
| bool | peerDisconnected () const |
| returns whether a peer disconnected | |
| int | receive (void *buffer, size_t len) |
| receive data from a bound socket | |
| int | send (const void *buffer, size_t len) |
| send data through a connected socket | |
| int | sendAll (const void *buffer, size_t len) |
| send data through a connected socket | |
| void | shutdown (ShutdownDirection type) |
| shutdown the connection in the specified direction | |
| TCPSocket () | |
| TCPSocket (Handle handle) | |
| Handle | timedAccept (int timeout) const |
| wait for another socket to connect | |
| int | timedConnect (std::string_view foreignAddress, unsigned short foreignPort, int timeout) |
| establish a connection with the given foreign address and port | |
| int | timedReceive (void *buffer, size_t len, int timeout) |
| receive data from a bound socket, return after the given timespan | |
Protected Types | |
| enum | SocketDomain { INTERNET = PF_INET , UNIX = PF_LOCAL , CAN = PF_CAN } |
| enum | SocketType { RAW = SOCK_RAW , STREAM = SOCK_STREAM , DATAGRAM = SOCK_DGRAM } |
Static Protected Member Functions | |
| static void | fillAddress (std::string_view address, unsigned short port, sockaddr_in &addr) |
Protected Attributes | |
| bool | m_peerDisconnected |
| int | m_socket |
TCP socket class.
Definition at line 10 of file TCPSocket.h.
| typedef SocketHandle<TCPSocket> NET::TCPSocket::Handle |
Handle for a new socket returned by accept.
Definition at line 14 of file TCPSocket.h.
|
inherited |
|
protectedinherited |
| Enumerator | |
|---|---|
| INTERNET | |
| UNIX | |
| CAN | |
Definition at line 183 of file SimpleSocket.h.
|
protectedinherited |
| Enumerator | |
|---|---|
| RAW | |
| STREAM | |
| DATAGRAM | |
Definition at line 190 of file SimpleSocket.h.
| TCPSocket::TCPSocket | ( | ) |
Construct a TCP socket
| SocketException | thrown if unable to create the socket |
Definition at line 10 of file TCPSocket.cpp.
| TCPSocket::TCPSocket | ( | Handle | handle | ) |
Construct a Socket from a Handle returned by accept()
| SocketException | thrown if handle is invalid |
Definition at line 14 of file TCPSocket.cpp.
| int TCPSocket::sendAll | ( | const void * | buffer, |
| size_t | len ) |
send data through a connected socket
sendAll() can only be used on a socket that called connect() before. If you try to use sendAll() on a unconnected socket, a SocketException will be thrown.
When using a stream oriented socket, the operating system is allowed to send only a bit of the data you requested it to send. Because of this, you would have to check the number of sent bytes when using send(), and resend unsent data manually. sendAll() takes that responsibility from you and resends as long as it needs, to completely send the data given to it.
| buffer | data to be send |
| len | length of the data to be sent |
| SocketException |
Definition at line 18 of file TCPSocket.cpp.
| void TCPSocket::listen | ( | int | backlog = 5 | ) |
listen for incoming connections
listen() can be called on a bound socket. The socket will then go into a passive state and accept incoming connections. To answer to an incoming connection call accept() or timedAccept() on the socket.
The backlog is a buffer for incoming connections. If backlog is set to 0, the number of incoming connections is only limited by the operating system. If backlog is set to a value > 0, only the upper limit of connections is set, the OS can limit the number further.
After starting to listen, use accept to accept incoming connections.
| backlog | upper limit of pending incoming connections |
| SocketException |
Definition at line 31 of file TCPSocket.cpp.
| TCPSocket::Handle TCPSocket::accept | ( | ) | const |
wait for another socket to connect
accept() will block until a new connection arrives. It is used to create TCP server applications. On return it will return the handle to the incoming connection.
accept can be only called on a listening socket.
Use the returned handle to create a new TCPSocket to communicate with the new connected client. Regardless of what you do with a handle, accept() can be used to wait for next incoming connection after the handle was received.
| SocketException |
Definition at line 38 of file TCPSocket.cpp.
| TCPSocket::Handle TCPSocket::timedAccept | ( | int | timeout | ) | const |
wait for another socket to connect
timedAccept() will block until a new connection arrives, or the given timeout runs out. It is used to create TCP server applications. On return it will return the handle to the incoming connection.
This handle might be invalid, so it should be checked before it is used. Creating a new socket object using an invalid handle will raise an exception.
timedAccept() can only be called on a listening socket.
Use the returned handle to create a new TCPSocket to communicate with the new connected client. Regardless of what you do with a handle, timedAccept() can be used to wait for next incoming connection after the handle was returned.
| timeout | the timeout in ms after which accept will give up and return |
| SocketException |
Definition at line 46 of file TCPSocket.cpp.
|
inherited |
establish a connection with the given foreign address and port
If you are using a connection oriented socket (like TCPSocket), you have to call connect() on that socket in order to send data. However, if you are using a non-connection oriented socket (like UDPSocket), calling connect on that socket will allow you to use the send() function like on a connection oriented socket. If not connected, you would have to use sendTo
| foreignAddress | foreign address (IP address or name) |
| foreignPort | foreign port |
| SocketException | thrown if unable to establish connection |
Definition at line 19 of file InternetSocket.cpp.
|
inherited |
establish a connection with the given foreign address and port
Provides timeout for TCP socket connection establishment.
| foreignAddress | foreign address (IP address or name) |
| foreignPort | foreign port |
| SocketException | thrown if unable to establish connection |
Definition at line 30 of file InternetSocket.cpp.
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Instead of the richer function with more arguments, this bind() binds to any available interface / address
Definition at line 82 of file InternetSocket.cpp.
|
inherited |
bind socket to address / port
Set the local port to the specified port and the local address to an interface.
bind() attaches the local endpoint of the socket to the specified interface and port. Typically all servers use bind() to attach to the local port specified by the used protocol. You just need to call bind if you want to restrict the connection to a specific local interface, or if you want to listen on a specific port. If you are just a client using connect() to connect to a server, calling bind is most likely unnecessary.
To bind to a port < 1023 you need eighter superuser access, or the SUID bit set on your program.
| localAddress | local address |
| localPort | local port |
| SocketException | thrown if setting local port fails |
Definition at line 93 of file InternetSocket.cpp.
|
inherited |
Get the local address (after binding the socket).
| SocketException | thrown if fetch fails |
Definition at line 102 of file InternetSocket.cpp.
|
inherited |
Get the local port (after binding the socket).
| SocketException | thrown if fetch fails |
Definition at line 113 of file InternetSocket.cpp.
|
inherited |
Get the foreign address. Call connect() before using this function.
| SocketException | thrown if unable to fetch foreign address |
Definition at line 124 of file InternetSocket.cpp.
|
inherited |
Get the foreign port. Call connect() before using this function.
| SocketException | thrown if unable to fetch foreign port |
Definition at line 135 of file InternetSocket.cpp.
|
staticprotectedinherited |
Fill an address structure with the given address and port number. If the given address is not a valid IPv4 address, it will be resolved by hostname or DNS lookup. addr will be unchanged if this resolve fails.
| address | IPv4 domain name or address |
| port | IP port number to fill in |
| addr | address structure to fill |
| SocketException | thrown if unable to resolve a hostname |
Definition at line 146 of file InternetSocket.cpp.
|
inlineinherited |
return the native handle of the open socket
Definition at line 61 of file SimpleSocket.h.
|
inherited |
send data through a connected socket
send() can only be used on a socket that called connect() before. If you try to use send() on a not connected socket, SocketException will be thrown.
If you are using a stream oriented socket (like TCPSocket), the operating system is allowed to send only a part of the packet you told it to send, so send() will return the number of bytes actually sent. It is your responsibility to resend the data not sent by send()
If you are using a datagram oriented socket (like UDPSocket or SCTPSocket) the operating system will only send and receive complete datagrams, but send() will fail if you are trying to send too much data. In that case SocketException will be thrown.
| buffer | data to be send |
| len | length of the data to be sent |
| SocketException | if sending went wrong |
Definition at line 44 of file SimpleSocket.cpp.
|
inherited |
receive data from a bound socket
receive() can only be used on a socket that called bind() or connect() before. If you try to use receive() on a not bound socket, SocketException will be thrown.
If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.
If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.
If the remote host has closed the connection (on a connection based socket like TCP or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
| SocketException | in case an error occured |
Definition at line 62 of file SimpleSocket.cpp.
|
inherited |
receive data from a bound socket, return after the given timespan
timedReceive() can only be used on a socket that called bind() or connect before. If you try to use receive() on a not bound socket, SocketException will be thrown.
If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.
If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.
If the remote host has closed the connection (on a connection based socket like TCP or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
| timeout | the timeout in ms after which receive will give up and return |
| SocketException | in case an error occured |
Definition at line 69 of file SimpleSocket.cpp.
|
inherited |
Disconnect and unset any foreign addresses.
This will ungracefully abort the connection by sending an RST frame. disconnect() should not be used under normal circumstances, as it will terminate the connection OOB, preventing the other end of the communication from retrieving data that would still be available in the OS buffer.
For normal termination of a connection use shutdown().
| SocketException | thrown if unable to disconnect |
Definition at line 93 of file SimpleSocket.cpp.
|
inherited |
shutdown the connection in the specified direction
This will gracefully end a connection by sending a FIN frame. Use this to end a normal communication cycle. To abort a connection see disconnect().
Depending on the specified ShutdownDirection, calls for that direction will stop working. Use this function if you want to have more control than just destroing the socket. It allows you to cut the connection in one direction, or both.
If you use shutdown() on an unconnected socket, the corresponding calls will simply stop working.
| type | the ShutdownDirection that be used |
| SocketException | in case an error occured |
Definition at line 108 of file SimpleSocket.cpp.
|
inherited |
returns whether a peer disconnected
Will only work if you use a connection oriented, connected socket. Returns true if the peer disconnected. Use this function after a call to receive, returned 0 received bytes.
Definition at line 114 of file SimpleSocket.cpp.
|
protectedinherited |
Definition at line 204 of file SimpleSocket.h.
|
protectedinherited |
Definition at line 205 of file SimpleSocket.h.