Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I have SOCKET ClientList[SOMAXCONN]; and ClientCount is 0, but in this code, the clientcount goes 0, 1, 1973 or something, why is that? It only happens if I try to manipulate the ClientList array.

    Code:
    		ret = sizeof(struct sockaddr); 
    		sckClient = accept(sckListen,(LPSOCKADDR)&clientN,&ret); 
    		if(sckClient==INVALID_SOCKET)
    		{
    			puts("****ed @ accept"); 
    			return 0; 
    		} 
    		ClientList[ClientCount++; ] = sckClient; 
    		
    		//Spawn a new thread for the client, then continue listening 
    		CloseHandle(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&recvfunc,(LPVOID)sckClient,0,&recvthread));
  2. #17
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    from that code i cant tell why you would get those results, perhaps more code would help. but in the above code, why are you closing the thread handle when you create it? i've just never seen that perhaps it is some technique im unfamiliar with?
  4. #18
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I really don't know, I'v picked it up somewhere and I'v been using it like that for a while, no real problems, the code always did what I want, so I never really questioned it. Heh maybe it's come back to haunt me now.

    Everytime someone connects it should send a msg to every client that someone new connected.
    Code:
    #define WIN32_MEAN_AND_LEAN 
    #include <winsock2.h> 
    #include <windows.h> 
    #include <stdio.h> 
    #include "scr0pstring.h" 
    
    int recvfunc(SOCKET ClientSocket); 
    
    int main()
    {
    	WSADATA wsaData; 
    	SOCKET sckListen; 
    	SOCKET sckClient; 
    	SOCKADDR_IN servN; 
    	SOCKADDR_IN clientN; 
    	int ret; 
    	DWORD recvthread; 
    	int ClientCount=0; 
    	SOCKET ClientList[SOMAXCONN]; 
    
    	printf("Initializing winsock..."); 
    	WSAStartup(MAKEWORD(1,1),&wsaData); 
    	printf("success\n"); 
    
    	//Set up listening socket 
    	printf("Creating listening socket..."); 
    	sckListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    	if(sckListen==INVALID_SOCKET)
    	{
    		printf("****ing error @ sckListen\n"); 
    		return 0; 
    	}
    	printf("success\n"); 
    
    	//fill serv info 
    	servN.sin_family = AF_INET; 
    	servN.sin_addr.s_addr = INADDR_ANY; 
    	servN.sin_port = htons(1016); 
    
    	//bind 
    	printf("Binding socket..."); 
    	if(bind(sckListen,(LPSOCKADDR)&servN,sizeof(struct sockaddr))==SOCKET_ERROR)
    	{
    		puts("Error @ bind"); 
    		return 0; 
    	}
    	printf("success\n"); 
    
    	printf("Listening on port %d\n", servN.sin_port); 
    	do
    	{
    		int x; 
    		if(listen(sckListen,SOMAXCONN)==SOCKET_ERROR)
    		{
    			puts("error @ listen"); 
    			return 0; 
    		}
    
    		//when someone connects 
    		ret = sizeof(struct sockaddr); 
    		sckClient = accept(sckListen,(LPSOCKADDR)&clientN,&ret); 
    		if(sckClient==INVALID_SOCKET)
    		{
    			puts("****ed @ accept"); 
    			return 0; 
    		} 
    
    		printf("%s:%d connected!\n",inet_ntoa(clientN.sin_addr),ntohs(clientN.sin_port)); 
    		
    		//Spawn a new thread for the client, then continue listening 
    		CloseHandle(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&recvfunc,(LPVOID)sckClient,0,&recvthread)); 
    		
    		ClientList[ClientCount++] = sckClient; 
    		for(x=0; x<ClientCount; x++)
    		{ 
    			send(ClientList[x],"Someone connected",sizeof("Someone connected"),MSG_OOB); 
    		} 
    	} while(1); 
    
    	closesocket(sckListen); 
    	closesocket(sckClient); 
    	WSACleanup(); 
    	return 0; 
    }
    recvfunc just outputs whatever the client sends to the server.
    what I want is whatever the client types to be sent to the server adn broadcasted to every other client.
  6. #19
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ok one thing i noticed quick is that you are including listen() in your loop. listen is only called once, not every time. listen should be moved out of the loop. also you keep declaring x over and over in the loop,
    Code:
     int x;
  8. #20
  9. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Ok, fixed those. I got the socket array to work, I had to do it before creating the thread. Now I have 1 question, If a user disconnects, I don't want to broadcast to her, how would I remove that socket from the list? right now the broadcast works, but If like ClientList[4] leaves, I want to use that space for someone else, not use ClientList[5] even tho 4 left.
  10. #21
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
    	//decrement the # of clients connected
    	num_clients--;
    	
    	//remove the client from the client_array
    	for(int m = 0; m < num_clients + 1; m++)
    	{
    		if(client_list[m] == hNewServSocket)
    		{
    			client_list[m] = client_list[num_clients];
    			client_list[num_clients] = NULL;
    		}
    	}
    that's how i did it. num_clients and client_list are both globals and hNewServSocket is that clients socket.
    Last edited by infamous41md; August 20th, 2003 at 12:00 PM.
  12. #22
  13. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Thanks, about the closing thread thing, I remember the link I picked it up at.

    http://spiff.tripnet.se/~iczelion/tut15.html

    "After the successful call, CreateThread returns immediately and ThreadProc begins to run. Since we do not use thread handle, we should close it else there'll be some leakage of memory. Note that closing the thread handle doesn't terminate the thread. Its only effect is that we cannot use the thread handle anymore. "
  14. #23
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ah ok gotcha. i figured it was something i wasnt aware of. im used to creating handle arrays and then calling WaitForMultipleObjects() to make sure they all are terminated properly. so how did your project turn out?
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo