SOCKETS
Classes
- vorlib::tcp_socket
- vorlib::base_socket (virtual)
- vorlib::socket_e (exception)
Files
Theese files contains server as well.
base_socket
This socket is virtual class designed as parent for more kind of socket. At this time, only tcp_socket is written. Don't use it directly, it is useless (and the compiler won't let you to create it anyway).
Interface
-
base_socket
Constructor, nothing interesting.
-
~base_socket
Destructor. Again, nothing interesting.
-
write
Write some data to socket. Prototype os like this: write(const void *data, int size), where data is pointer to the data you want to send and size is count of bytes, how large it is.
-
read
The prototype is int read(void *data, int maxsize). data is block of memory, where you want the data to be read, maxsize says, how many bytes max. can be placed there. The function returns count of bytes read. (If it returns 0, no data were ready).
-
readblocking
Same as read, but if there are no data, it waits until some data arrives.
-
readexact
This always waits, until there are enough data to fill the buffer.
-
isconnected
Returns, weather the socket is connected.
-
getsocket
Returns socket descriptor of the socket used by system functions like select. Please, use it wisely, or you get some stupid errors.
tcp_socket
This is socket class for tcp protocol. It have to be connected before sending or receiving any data.
Interface
-
tcp_socket
It is constructor. You can pass it block boolean parameter, witch can allow or forbid the socket to block. (That means, it can wait for data and stop processing the program until some data arrives). The socket may not work properly with block = false. If you need non-blocking sockets, try using server.
-
~tcp_socket
This closes the socket and destroys the class. It is called automatically if the class dies.
-
disconnect
If the socket is connected, you may want to connect it to other host. Before that, you must close actual connection, witch you can do by this function.
-
open
Connects to listening host (or, at last, ties to). The prototype is this: open(address, port). The port is alway an integer (int host format, if you don't know what it is, just ignore this note). The address may be an unsigned int - an ip address put together into integer. The address may be a std::string, witch can contain ip address in dot-format, or host name.
-
listenonport
The socket listens for incoming connection on given port. You may provide an address (as an unsigned int in host format) and maximal qeue for waitong connections.
-
acceptconnection
If the socket is listening, you can accept the connection by this function. If there is no connection waiting and the socket can block, it waits until some arrives. It returns pointer to new tcp_socket (witch has to be destroyed by delete after use). After that, the socket is still listening.
-
write
Send some data, see write function in base_socket for details. If not all data can be sent, it throws socket_e id = 7 (see error handling and resend below).
-
resend
If write throws socket_e id = 7 (not all data sent), you can send the rest by this function. It also can return this error.
-
resetwaiting
If not all data from write were sent, you can send them by resend above or storno it by this function.
-
iswaiting
Anwsers, weather there are some data waiting.
-
addwaiting
Put more data into qeue of data waiting to be sent (you can place them, if not all data were send, or create some longer message you want to send together
-
read
Reads some data from socket. See read in base_socket above for details. Does not block even with blocking socket.
-
readblocking
Like read, lets non-blocking sosket to block. Does not work with non-blocking socket.
-
readexact
Reads exactly wanted bunch of data, waiting for all to arrive. Does not work with non-blocking socket.
Error handling
It has exception system, using exception and its heirs. Error ids are here.
- -1: This is the "fixme" error. It is used for debugging, if you get one, it means there is a bug in the socket code. You aren't interested in it unless you want to debug it.
- 1: Trying to connect already connected socket. You can not connect it twice (you must disconnect it first).
- 2: Could not allocate the handle. This means that the system is not able to assign your program the socket. The errno should be still set to the last error, you can find out why it is so.
- 3: Trying to disconnect socket that is not connected.
- 4: Could not convert host name to ip address. The network may be down or you misspeled it.
- 5: Could not bind the socket. Binding is done before the socket is able to listen. It failed for some reason, the most common is that you want to use port address that is already used by some other application.
- 6: Socket not connected. You tried to send or receive data trough socket that is not connected. Look into errno to see details.
- 7: Some data not sent yet, resend it. It is not an error, just that some data did not fit into system qeue. Just call resend after while and it will try to send the rest again.
- 8: Send error. System returned an error while sending data. You may find the reason in errno.
- 9: No data to send. You try to resend, but there are no data waiting.
- 10: Receive error. You tried to read, but the system returned an error.
- 11: Closed by peer. The remote endpoint was closed, this end is closed automatically.
- 12: Didn't read enough data. It can be throwed by readexact and it means there was an error and it could no longer wait for data.
- 13: Invalid operation with blocking or non-blocking socket. You tried to do some operation witch does not go by with the blocking setup. See the message for detailes.
- 14: Error while accepting connection. The connection could not be connected for some reason.
- 15: Can not block and no connection waiting, while you requested accept.
- 16: Invalid operation with listening/non-listening socket. You want accept on non-listening socket or trying to send/receive data on trough listening socket.
Other errors are only for server, because it uses the same exception class.