![]() | |
![]() |
![]() |
![]() |
![]() |
overview |
The qsocket class handles the aquisition and clean-up associated with socket communications. It works with unix and Windows 95, depending on what is #defined. Most functionality is self-explanatory, but note that if QTHROW is #defined, exceptions will be thrown if there are errors opening or closing the socket. Also note that if QTHROW is turned off, then errors will print a message and exit with code -1.Also, constructing with the qsocket::qsocket( char *, int ) constructor binds to any local address, with any local port while setting the destination of packets to the specified IP and port.
The other constructor binds to the specified local and remote addresses, on the (optional) port specified (default is 26000).
member variables |
class qsocket { private: struct sockaddr_in remote; struct sockaddr_in local; int fd; public: };
- remote
- The address of the target for packet sends.
- local
- The local address the socket is bound to.
- fd
- The file descriptor of the socket.
member functions |
class qsocket { private: struct in_addr * atoaddr( char * address ); void winStartup(); void winShutdown(); void winError(); public: qsocket( char * addr, int port=QCS_DEFAULT_PORT ); qsocket( struct sockaddr_in * x, struct sockaddr_in * y, int port=26000 ); ~qsocket(); int send( char *, int ); int receive( char *, int ); struct in_addr lastPacketIP() { return remote.sin_addr; } int lastPacketPort() { return remote.sin_port; } struct in_addr getAddress() { return local.sin_addr; } struct sockaddr_in * getLocalAddress() { return &local; } struct sockaddr_in * getRemoteAddress() { return &remote; } void changePort( int p ); int getLocalPort() { return ntohs( local.sin_port ); } int getRemotePort() { return ntohs( remote.sin_port ); } int getFD() { return fd; } };
- atoaddr
- Returns the sockaddr_in corresponding to the IP or name passed in (i.e. 136.159.102.88 or quake.oanet.com). NULL returned on failure.
- winStartup, winShutdown, winError
- Functions for Windows 95. These are NULL functions unless WIN32 is #defined.
- qsocket( char *, int )
- Constructor; opens socket, binds to any local address and sets remote and local appropriatly. Throws char * exceptions or prints error and exits.
- qsocket( struct sockaddr_in *, struct sockaddr_in *, int )
- Bind to the specified local and remote addresses and port. Same error handling as the first contructor.
- send, receive
- Send and receive buffers of the specified size. Each returns the number of bytes sent/received, or -1 on error.
- lastPacketIP, lastPacketPort
- Returns the IP address or port of the the sender of the last received packet.
- GetAddress
- Returns the local address.
- getLocalAddress, getRemoteAddress
- Local and remote addresses.
- ChangePort
- Changes the destination port for packets and the local port binding.
- getLocalPort, getRemotePort
- Local and remote ports.
- getFD
- Returns the file descriptor used by the socket. Usefull for doing select() calls before you call qsocket::receive()...