Thread: Client abnormally terminates when server isn't found

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Client abnormally terminates when server isn't found

    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..
    Last edited by Brain Cell; 03-13-2005 at 05:54 PM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming chat client, need some help (sockets & threads)
    By lalilulelo17 in forum Linux Programming
    Replies: 1
    Last Post: 04-19-2008, 04:01 AM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  3. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  4. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  5. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM