- Inherits From:
- EDIPSocket
- Declared In:
- EDTCPSocket.h
The following code example shows how to set up a client socket, and connect it to Apple's web server:
EDTCPSocket *socket;
socket = [EDTCPSocket socket];
[socket connectToHost:[NSHost hostWithName:hostname] port:80]
The following code example shows how to set up a server socket, listening on the standard web port on all interfaces of this host. If we are debugging it sets the "re-use address" option so that we don't have to wait to set up the socket after restarting our application:
EDTCPSocket *socket;
socket = [EDTCPSocket socket];
#if DEBUG
[socket setAllowsAddressReuse:YES];
#endif
[socket startListeningOnLocalPort:80];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newClientNotification:)
name:NSFileHandleConnectionAcceptedNotification
object:socket];
[socket acceptConnectionInBackgroundAndNotify];
Note that, generally, working with EDStreams is a better choice than working directly with sockets.
Setting socket optionsServer sockets
- - setSendsDataImmediately:
- - startListening
- - startListeningOnLocalPort:
- - startListeningOnLocalPort:andAddress:
- (void)setSendsDataImmediately:(BOOL)flag
Controls wether data written to the socket is sent immediatly or whether the operating system should try to accumulate some data before sending a packet to reduce overhead. This is commonly known as Nagle's algorithm (RFC 896) and corresponds to TCP_NODELAY.
- (void)startListening
Starts listening for incoming connections. This requires that a local port has been set before. Note that acceptConnectionInBackgroundAndNotify must be called afterwards so that incoming connections are handled.
- (void)startListeningOnLocalPort:(unsigned short)aPort
Starts listening for incoming connections on aPort on all local IP addresses (interfaces). Note that acceptConnectionInBackgroundAndNotify must be called afterwards so that incoming connections are handled.
- (void)startListeningOnLocalPort:(unsigned short)aPort andAddress:(NSString *)addressString
Starts listening for incoming connections on the aPort on the specified local address; the latter being in the "typical" dotted numerical notation. Note that acceptConnectionInBackgroundAndNotify must be called afterwards so that incoming connections are handled.