Thread: Client abnormally terminates when server isn't found

  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

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	printf("Error connectiog : %s", WSAGetLastError());
    WSAGetLastError does not return a string.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    GAH!! stupid me *smacks forehead on table*. Turns out my error checking needs error checking .. hehe

    btw , whats a good resource to learn about Asynchronous sockets? (except for Johnnie's tutorial)
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    This one was decent. Although I think there were a few minor errors in there if I remember correctly..
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    looks good JaWiB , thanks
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    From the tutorial :
    Non-blocking sockets can be used in console apps, asynchronous sockets cannot.
    *gasps* WHAT????

    how come i can't use asynchronous sockets in console apps? does this force me to learn Windows programming , or is there another way around it??
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Only WSAAsyncSelect requires the windows program stuff (i.e. a window procedure and message pump). Other async. methods don't.

  8. #8
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I can only find tutorials using WSAAsyncSelect() function. Where can i find tutorials teaching the "other" methods for windows?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    MSDN basically explains how to use them..

  10. #10
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    how about you name those functions so i can research them ?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Sorry -

    WSAEventSelect
    GetQueuedCompletionStatus with CreateIOCompletionPort
    Standard blocking sockets in multiple threads is async too, because threads are async by definition.

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