here is the code that I've done so far.
Code:
#include <stdio.h>
#include <winsock.h>
#define STRSIZE 16536
void handle_error(void);
void receiveMessage(void);
void connectToWorkSpace();
char recvbuf[60000] = "";
SOCKET ConnectSocket;
struct sockaddr_in clientService;
int bytesSent;
int bytesRecv = 0;
char sendbuf[500] = "";
struct hostent *h;
char HOSTNME[20]="";
char hostname[20] = "localhost"; // for testing purposes
char hostnamebackup[20] = "local-blahblah"; // for testing purposes
int chker=0;
int main()
{
connectToWorkSpace();
while(1){
receiveMessage();
}
}
void receiveMessage()
{
int bytesRecv = 0;
bytesRecv = recv( ConnectSocket, recvbuf, 60000, 0 );
// check if server is still up
if (bytesRecv == 0 ||
bytesRecv == SOCKET_ERROR ||
WSAGetLastError() == WSAEINTR || /*(has been closed)*/
WSAGetLastError() == WSAENOTCONN || /*(not connected)*/
WSAGetLastError() == WSAENETRESET || /*(kepp-alive service failed and broke connection)*/
WSAGetLastError() == WSAESHUTDOWN || /*(the socket was shutdown() peacefully)*/
WSAGetLastError() == WSAECONNABORTED || /*(something died and the connection was broken)*/
WSAGetLastError() == WSAETIMEDOUT || /*(the network died or the other computer stopped responding)*/
WSAGetLastError() == WSAECONNRESET /*(something died on the remote computer and broke the connection)*/
){
printf("Reconnect!\n");
connectToWorkSpace();
}else{
recvbuf[bytesRecv]='\0';
if(strlen(recvbuf)>0){
printf("received message: %s \n", recvbuf);
}
}
}
void connectToWorkSpace()
{
int chkHost=0;
int loopHost=0;
int conn = 1;
WSADATA wsaData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 1, 1 );
if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ){
fprintf(stderr,"WSAStartup Error...");
handle_error();
}
// will loop until connect() is successful
while(conn != 0){
chkHost =chker%2;
if(chkHost == 0){
printf("Connect Using Primary Host\n");
strcpy(HOSTNME,hostname);
}
else {
printf("Connect Using Backup Host\n");
strcpy(HOSTNME,hostnamebackup);
}
// will loop until HOSTNME is reachable
while(loopHost==0){
chkHost =chker%2;
if((h=gethostbyname(HOSTNME))==NULL)
{
printf("%s not reachable\n",HOSTNME);
if(chkHost == 1){
printf("Connect Using Primary Host\n");
strcpy(HOSTNME,hostname);
}
else {
printf("Connect Using Backup Host\n");
strcpy(HOSTNME,hostnamebackup);
}
}else{
loopHost=1;
}
Sleep(1000);
chker++;
}
clientService.sin_addr=*((struct in_addr*)h->h_addr);
clientService.sin_port = htons(8770);
clientService.sin_family = AF_INET;
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ConnectSocket == -1) {
handle_error();
printf("Error at socket(): \n");
//return -1;
}
// Connect
if( connect( ConnectSocket, (struct sockaddr*) &clientService, sizeof(struct sockaddr) ) == -1) {
printf("Failed to connect. Workspaceserver is down.\n" );
printf("Reconnecting........\n" );
loopHost=0;
Sleep(5000);
conn = 1;
}
else {
conn = 0;
printf("Connection Success!\n");
}
}
strcpy(sendbuf,"jaro");
strcat(sendbuf, " :username");
strcat(sendbuf, "\n");
/* Send */
bytesSent = send( ConnectSocket, sendbuf, strlen(sendbuf), 0 );
printf( "\n messages Sent: %s\n", sendbuf );
}
void handle_error(void)
{
/*
* Errors are handled by calling the WSAGetLastError routine which
* will return the last error as one of the following. As we develop
* this tutorial, we will go into much more detail on what they mean
* and what caused them.
*/
int x;
x= WSAGetLastError();
switch ( x )
{
case WSANOTINITIALISED :
printf("Unable to initialise socket.\n");
break;
case WSAEAFNOSUPPORT :
printf("The specified address family is not supported.\n");
break;
case WSAEADDRNOTAVAIL :
printf("Specified address is not available from the local machine.\n");
break;
case WSAECONNREFUSED :
printf("The attempt to connect was forcefully rejected.\n");
break;
case WSAEDESTADDRREQ :
printf("address destination address is required.\n");
break;
case WSAEFAULT :
printf("The namelen argument is incorrect.\n");
break;
case WSAEINVAL :
printf("The socket is not already bound to an address.\n");
break;
case WSAEISCONN :
printf("The socket is already connected.\n");
break;
case WSAEADDRINUSE :
printf("The specified address is already in use.\n");
break;
case WSAEMFILE :
printf("No more file descriptors are available.\n");
break;
case WSAENOBUFS :
printf("No buffer space available. The socket cannot be created.\n");
break;
case WSAEPROTONOSUPPORT :
printf("The specified protocol is not supported.\n");
break;
case WSAEPROTOTYPE :
printf("The specified protocol is the wrong type for this socket.\n");
break;
case WSAENETUNREACH :
printf("The network can't be reached from this host at this time.\n");
break;
case WSAENOTSOCK :
printf("The descriptor is not a socket.\n");
break;
case WSAETIMEDOUT :
printf("Attempt timed out without establishing a connection.\n");
break;
case WSAESOCKTNOSUPPORT :
printf("Socket type is not supported in this address family.\n");
break;
case WSAENETDOWN :
printf("Network subsystem failure.\n");
break;
case WSAHOST_NOT_FOUND :
printf("Authoritative Answer Host not found.\n");
break;
case WSATRY_AGAIN :
printf("Non-Authoritative Host not found or SERVERFAIL.\n");
break;
case WSANO_RECOVERY :
printf("Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n");
break;
case WSANO_DATA :
printf("Valid name, no data record of requested type.\n");
break;
case WSAEINPROGRESS :
printf("address blocking Windows Sockets operation is in progress.\n");
break;
case WSAEINTR :
printf("The (blocking) call was canceled via WSACancelBlockingCall().\n");
break;
default :
printf("Unknown error.\n");
break;
}
}
Just need some comment (good or bad) on the program that I've written. Just need to know if I'm on the right track.
regards,
jaro