Hi there,
I just made a server and client programs (finally!!) but there's a problem with my client program. The problem is , when the server isn't up , the client abnormally terminates and displays a windows error (the one with "send report" ..etc). How do i check if the server is up , and if not , try another address for example?
here's my client's simple code :
Code:
#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
int main(void)
{
WSAData wsadata;
WORD ver;
char buffer[256];
ver = MAKEWORD(1,1);
WSAStartup(ver, &wsadata);
LPHOSTENT hostEntry;
hostEntry = gethostbyname("localhost");
if(!hostEntry)
{
printf("Error getting host : %s\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Host gathered ..\n");
SOCKET theSocket;
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(theSocket == INVALID_SOCKET)
{
printf("Error socket : %s\n", WSAGetLastError());
system("PAUSE");
WSACleanup();
return 1;
}
printf("Socket created ..\n");
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(666);
if(connect(theSocket, (LPSOCKADDR)&serverInfo,
sizeof(sockaddr)) == -1)
{
printf("Error connectiog : %s", WSAGetLastError());
WSACleanup();
return 1;
}
printf("connected ... \n\n\n");
printf("> ");
fgets(buffer, sizeof(buffer), stdin);
if( send(theSocket, buffer, strlen(buffer), 0) == SOCKET_ERROR)
{
printf("Error getting host : %s", WSAGetLastError());
WSACleanup();
return 1;
}
closesocket(theSocket);
WSACleanup();
return 0;
}
Edit : i forgot to mention that the program reaches "connect()" before it termiates , and it won't display an error even though i used error checking.
help is much appreciated..