Object

SocketCore Class

The base class for TCPSocket, and UDPSocket. The SSLSocket class is derived from TCPSocket.

Events
DataAvailable
Error
SendComplete

Properties
Handle
IsConnected
LastErrorCode
LocalAddress
NetworkInterface
Port

Methods
Close
Connect
Poll
Purge

# Notes

SocketCore is an abstract class and cannot be instantiated directly.

# Error Codes

The LastErrorCode property contains an integer value specifying what the last error code is. These error codes provide you with key information about your socket, and it is not advisable to ignore them.

Each error code is associated with a class constant. When you need to check whether a particular error occured, you can check the value of the LastErrorCode property against these class constants.

Error Code | Class Constant | Description
--- | --- | ---
| NoError | No error occurred.
100 | OpenDriverError | There was an error opening and initializing the drivers. It may mean that either Open Transport (on the Macintosh), or WinSock (on Windows) is not installed, or the version is too early.
101 |  | This error code is no longer used.
102 | LostConnection | This code means that you lost your connection. You will get this error if the remote side disconnects (whether its forcibly-- by pulling their ethernet cable out of the computer), or gracefully (by calling SocketCore's Close method). This may or not be a true error situation. If the remote side closed the connection, then it is not truly an error; it's just a status indication. But if they pulled the ethernet cable out of the computer, then it really is an error; but the results are the same. The connection was lost. You will also get this error if you call the Disconnect method of TCPSocket.
103 | NameResolutionError | REALbasic was unable to resolve the address that was specified. A prime example of this would be a mistyped IP address, or a domain name of an unknown or unreachable host.
104 |  | This error code is no longer used.
105 | AddressInUseError | The address is currently in use. This error will occur if you attempt to bind to a port that you have already bound to. An example of this would be setting up two listening sockets to try to listen on the same port.
106 | InvalidStateError | This is an invalid state error, which means that the socket is not in the proper state to be doing a certain operation. An example of this is calling the Write method before the socket is actually connected.
107 | InvalidPortError | This error means that the port you specified is invalid. This could mean that you entered a port number less than 0, or greater than 65,535. It could also mean that you do not have enough privileges to bind to that port. This happens under Mac OS X and Linux if you are not running as root and try to bind to a port below 1024. You can only bind to ports less than 1024 if you have root privileges. A normal "Admin" user does not have root privileges.
108 | OutOfMemoryError | This error indicates that your application has run out of memory.

Use the constants in the table below to compare to the LastErrorCode property.

These are not the only errors that are returned by LastErrorCode. If REALbasic cannot adequately map the provider's error code to one of the above codes, it will pass you the provider's error code. For Windows, these error codes are usually positive numbers in the range [10004, 11004]. For the Macintosh, these error codes are negative numbers in the range [-3211, -3285]. For a description of the Macintosh error codes, please see MacErrors.h. For Windows error codes, see WinSock.h.

There are some instances where you would like the system to pick a port for you. These needs can range from needing a port for passive FTP file transfers, or perhaps you wrote a class to auto-discover other applications on the network and you would like to negotiate a port to connect over using TCP/IP. If you specify a Port of 0 and then call TCPSocket.Listen or UDPSocket.Connect, REALbasic will pick a random open port for you. Most often, these ports will be in the range of 49512 to 65535 (inclusive).

If you use this method to obtain an open port, then you probably need to use the following procedure to determine which port was chosen. After calling the Connect method or the Listen method of the TCPSocket class, you can check the Port property to see which port was assigned. This means that the port number will change from 0 to the port number that you are bound to.

There's another benefit checking the Port property. There is a hacking technique called port hijacking where the hacker steals a port out from under you. If this happens, checking the Port property will tell you if someone has hijacked the port. It can be a good idea (though paranoid) to periodically check to make sure the Port property returns a port that you expect to see. For instance, if you were listening on port 80 for HTTP connections, but the Port property says you're listening on port 2113, then something may be wrong.

The SendComplete event's parameter lets you know whether the transfer has completed or has been cancelled by returning True from the SendProgress event of the TCPSocket class. You can use this information to update different status variables, or alert the user of transfer success or failure. If the user aborted, this parameter is True, and if the send was completed, this value is False. The SendComplete event's parameter is always False for UDPSockets since there is not a SendProgress event for that class.

# Endianess

There are two different endian standards.Windows and Linux use "little" endianess and Macintosh uses "big" endianess. Sockets work with streams of data and do not change the endianness of the data you are transferring. This is fine in many cases if you are transferring strings from one platform to another (like from Mac OS X to Windows XP). But if you are transferring binary data, such as from a BinaryStream or a MemoryBlock, you will want to ensure that the endianess matches from server to client regardless of what platform you are on. You can do this by setting the LittleEndian property on MemoryBlocks or BinaryStreams to the same value for your client and your server. Failure to ensure that the endianess is consistent will result in a possible byte-order conflict in your application. You can use the TargetBigEndian and TargetLittleEndian constants to determine the endian standard for the current computer.

# See Also

Datagram, ServerSocket, TCPSocket, UDPSocket classes.