OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Creating and Connecting a Stream Server and a Client

The code below creates a listening stream socket and waits for connections.

ShutDownApp() is called in the event of an error.

#define SERVICE_PORT 5001
int nError;
TStreamSocket tempListeningSocket(TINetSocketAddress(htons(SERVICE_PORT)), INADDR_ANY));
MessageBox(TSocketError(tempListeningStreamSocket.GetLastError()).
AppendError("Error on call to BindSocket"), "Error" MB_OK);
}
tempListeningStreamSocket.StartAcceptNotification();
MessageBox(TSocketError(tempListeningStreamSocket.GetLastError()).
AppendError("Error on call to BindSocket"), "Error" MB_OK);
}
for(;;){ /*Accept and service incoming connection requests indefinitely*/
MessageBox(TSocketError(tempListeningStreamSocket.GetLastError()).
AppendError("Error on call to BindSocket"), "Error" MB_OK);
}
// ...do something with the socket...
tempSocket.StartRegularNotification(); /*Converts socket to non-blocking*/
tempSocket.CloseSocket();
}

The code below creates a client stream socket that then connects to a server. There is no error checking. The code is blocking, in order to simplify reading.

#define SZ_SERVER "131.107.1.121"
TServiceManager
TServiceEntry*
TStreamSocket
TINetSocketAddress peerAddress;
myStreamSocket.CreateSocket();
myStreamSocket.BindSocket();
peerAddress.SetAddress(AF_INET,
tempServiceEntry.s_port, TINetSocketAddress::ConvertAddress(SZ_SERVER));
...