Sockets....

This is a discussion on Sockets.... within the Networking/Device Communication forums, part of the General Programming Boards category; Hi All, I am having trouble with Beej's Guide to Network programming. I understand the tutorial but... I have (tried) ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Smile Sockets....

    Hi All,

    I am having trouble with Beej's Guide to Network programming. I understand the tutorial but...

    I have (tried) written a client and server app. That is. 1 exe for
    client and one for server.

    They both compile and run, however the clien is not able to connect to the server. A few questions.

    I am running them both locally on my computer. Can one do that?
    To get the address of the computer, its under propeties for network...?

    Bit confused about where to get the address required, since i think thats the one that gotta be wrong here. Anyway...

    I have attached the zip with both files.
    could anyone give me a hit to what is wrong and what to do about it..

    Cheers,
    G'n'R
    Attached Files Attached Files

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I am running them both locally on my computer. Can one do that?
    Yes.

    >>To get the address of the computer
    Just use 127.0.0.1 (that is localhost)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Yep, cool. However, the server side wont bind to it. Keep getting error: Unalble to bind to port??

    Here is the code. I am real new to sockets (so i have probably left out a few bits here)....but this compiles and runs. Except for the error on bind. If i use any other number for the ip it runs...


    Code:
    #include <winsock.h>
    #include <stdio.h> 
    #include <conio.h>
    
    #define SERVERPORT 4545
    #define BACKLOG 20 
    
    struct sockaddr_in sAddress;
    int clientHandles[BACKLOG];
    struct sockaddr_in cAddress[BACKLOG];
    int connectionCount=0;
    struct hostent *he;
    int socketHandle;
    
            
    WSADATA wsaData; // use to inititalize winsock
    
    int main(void){
    
    	if(!WSAStartup(MAKEWORD(1,1), &wsaData)==0)
    		return (0);
    
    	he = gethostbyname("lucifer"); 
    
    	
    	if(!(socketHandle= socket(AF_INET, SOCK_STREAM, 0))){
    		printf("\nERR: Unable to create socket...");
    		while(!kbhit());
    		return(0); 
    	}
        
    
            
    	
    	sAddress.sin_family=AF_INET;
    	sAddress.sin_port = htons(SERVERPORT);
    	sAddress.sin_addr.S_un.S_addr= inet_addr ("127.0.0.1");//16177343;
    	memset(&(sAddress.sin_zero), '\0', 8);
    
    // THIS IS WHERE IT STOPS WITH IP 127.0.0.1 ??   
     if(!bind(socketHandle, (struct sockaddr *)&sAddress, sizeof(struct sockaddr))){
    		int errorCode=WSAGetLastError();
    		printf("\nERR %i: Unable to bind to address...", errorCode);
    		while(!kbhit());
    		return (0);
    	}
    
        // set the backlog for how many clients the server accepts
    	if(!listen(socketHandle, BACKLOG)){
    		printf("\nERR: Unable to listen to socket...");
    		while(!kbhit());
    		return (0); 
    	}
        
    
       
    	char hostname[50];
    	gethostname(hostname, sizeof(hostname)); 
    	printf("Server Information:\n Family: %i --> Port: %i --> Address: %s ServerName: %s\n\n Waiting for connections...\n\n",    sAddress.sin_family,
    		                                         sAddress.sin_port,
    												inet_ntoa(sAddress.sin_addr),
    												hostname);
    	
        
        
        
    	
    	while(1){
    	   int sin_size = sizeof(struct sockaddr_in);
           if((clientHandles[connectionCount] =  
    		   accept(socketHandle, (struct sockaddr *)&cAddress[connectionCount], &sin_size))!=-1){ 
    	      printf("\n\nAccepted Connection: \n\n ClintId: %i\n", clientHandles[connectionCount]);
    		  
    		  send(clientHandles[0], "Welcome to RIO Server!", sizeof("Welcome to RIO Server"), 0);
    
    		  connectionCount++;
           }
    
    	   if(kbhit())
    		   break;
        }
        
    	closesocket (socketHandle);
    
    	
    	
    	
    	WSACleanup();
    	return(0);
    }

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Solved but....

    Yo,

    Dont worry about it

    After actually concentrating and looking threw the code without fear i managed it. Got rid of heaps of wrong if testing and typos. Its working

    But who can give me a hint on how to create non bloking servers. e.g. so the accept command dosnt loop internally?(its not in the tutorial)

    Cheers,
    G'n'R

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    ...
    
    if (bind(socketHandle, (struct sockaddr *) &sAddress, sizeof(struct sockaddr)) == SOCKET_ERROR)
    
    ...
    
    if (listen(socketHandle, BACKLOG) == SOCKET_ERROR)
    
    ...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Sockets, threads and stuff

    Hi all,

    This socket thing is going great. I solved the blocking thing by using threads. My question is if the way i am doing it is the best way. What i am doing is:

    I create a thread a beginning of program to poll on the accept function. Check to see if a new client is trying to connect

    If one is I spawn a new thread (from the accept thread) for this client and inc client count. Clients and clientThreads are arrays.

    The client thread checks to see if the client disconnects etc, using recv function.

    ----------------------------------------------------------------------------------

    My questions is:

    Calling threads from threads. Is this common practice and is it safe?

    How many threads can one have? I have a max thread count at 25 at the moment.

    Can you terminate a thread from within the source thread it self? e.g When the client exits, my server program catches that and exist the client thread loop. Can i use closehandle after i break out of the thread loop and before the thread function pointer exist? Right now close all threads before server exit. I just loop and close on the thread counter.

    I need to find a better way using the client threads as since i am closing all threads at server exit the thread counter (for clients) at 25 wont last long.

    Anyway....anyone got any suggestions?

    G'n'R

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sounds like you have lots of questions. Maybe a tutorial or book would help you.

    In essence, what you're doing sounds about right.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Internet

    Yep, and i got a lot more

    But what i have done so far seems to work pretty good. But could you answer this.

    Does this work over the internet. e.g. if i connect to the internet
    with my modem. I then get the internet ip address (check my dial up connection, client id) and use that in my server app. I then get someone on the net to run my client program (connecting with the ip i am using). Will it work?

    I guess what i am trying to find out is:
    When binding a socket to an ip, what does the function do? Check threw a current internet connection, local networkcards etc.

    If so, is there a function to check for a internet connection and if so get the internet address you are connected with?

    Cheers,
    G'n'R

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Will it work?
    Yes, providing you code it right.

    >>check for a internet connection
    OS/compiler specific, check your local documentation

    >>if so get the internet address
    See the FAQ for examples, or again your local docs.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Baluba

    Hi Hammer,

    Thanks for you replies. Could you be a little more specific about
    your first answer. >>yes, provide....

    Is there anything specific i need to do or take into account when trying to implement that?

    Cheers,
    G'n'R

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, all I really meant was for you to code it and test it locally, then try to use a net based connection. As long as you're bound on an interface that is connected to the net, you should do OK. Check out beej's for INADDR_ANY.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Yep

    Yep, will do. It all works locally. And i have also tried locally using the internet address i get when i connect to the internet. That work to. So i guess the next test is to send the client to a friend and test it remotly...... That will be pretty cool if it works....

    Cheers,
    G'n'R

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just make sure you're code is secure. There are plenty of buffer overflow bugs that leave servers exposed to attack. I'm sure you'll be safe, and no-one will target you, but it's good practice to write safe code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Overflow?

    hmmmm, that dosnt sound cool at all Buffer overflows? I am currently reading in the tutorial about writing read/write functions that ensures all data is sent and recieved. e.g. loop on send/recieve until you get all the bytes you should get (as descriped in a packet struct). Is that what you mean? (I am not familiar with the terms yet). I will eventually encrypt the data being sent but that far off yet...Could you give a specific example on buffer overflow and online targeting etc...(in general terms etc)



    G'n'R

  15. #15
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    If you have an array of 25 chars:
    char blah[25];

    and someone can put data in that array:
    scanf("%s",blah);

    they could put more than 25 characters in it. (Unless you use a safer method of input, such as fgets). When an array is overflowed, they can overwrite the code of the program with their own machine language instructions*, causing the program to do whatever they want.

    If it is possible to overflow an array remotely, it can be disasterous. The blaster worm is an example of this.

    * The only thing overwritten normally is the return address for a function, and when the program returns, it returns to the beginning code you have entered into the overflowed array (on the stack). That is why if you overflow an array normally, the program will crash, because the return address will be invalidated.
    Last edited by Brian; 09-18-2003 at 11:44 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 02:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 06:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 05:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21