{
int error = EXT_NO_ERROR;
error = ExtInit(CD);
if (error != EXT_NO_ERROR) goto EXIT_POINT; printf(\EXIT_POINT: return(error); } /* end ModeInit */
/* Function: ExtInit * Abstract:
* Called once at program startup to do any initialization. * A socket is created to listen for
* connection requests from the client. EXT_NO_ERROR is returned * on success, EXT_ERROR on failure. * NOTES:
* This function should not block. */
int ExtInit(ConnectData *UD) {
int sockStatus; struct sockaddr_in serverAddr;
int sFdAddSize = sizeof(struct sockaddr_in); int option = 1; int port = 17725;
int error = EXT_NO_ERROR; SOCKET sFd = INVALID_SOCKET; #ifdef WIN32 WSADATA data;
if (WSAStartup((MAKEWORD(1,1)),&data)) { fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; } #endif /*
* Create a TCP-based socket. */
memset((char *) &serverAddr,0,sFdAddSize); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sFd = socket(AF_INET, SOCK_STREAM, 0); if (sFd == INVALID_SOCKET) { fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; } /*
* Listening socket should always use the SO_REUSEADDR option * (\ * Volume 1, 2nd edition, by W. Richard Stevens). */
sockStatus =
setsockopt(sFd,SOL_SOCKET,SO_REUSEADDR,(char*)&option,sizeof(option));
if (sockStatus == SOCK_ERR) {
fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }
sockStatus =
bind(sFd, (struct sockaddr *) &serverAddr, sFdAddSize); if (sockStatus == SOCK_ERR) {
fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }
sockStatus = listen(sFd, 1); if (sockStatus == SOCK_ERR) { fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }
EXIT_POINT:
UD->msgFd = INVALID_SOCKET; UD->port=17725;
if (error == EXT_ERROR) {
if (sFd != INVALID_SOCKET) { close(sFd); }
UD->sFd = INVALID_SOCKET; } else {
UD->sFd = sFd; }
return(error); } /* end ExtInit */
int OpenConnection(ConnectData *UD) {
struct sockaddr_in clientAddr;
int sFdAddSize = sizeof(struct sockaddr_in);
int error = EXT_NO_ERROR; SOCKET msgFd = INVALID_SOCKET; const SOCKET sFd = UD->sFd; /*
* Wait to accept a connection on the message socket. */
msgFd = accept(sFd, (struct sockaddr *)&clientAddr, &sFdAddSize); if (msgFd == INVALID_SOCKET) {
fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }
connectionMade = 1;