#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    18
    Rep Power
    0

    sorry for mistake


    now I attempt to make a server that send messages to clients that connected to it.I use TCP protocol my algorithm is use thread to accept client (1 thread/1 connection) and collect each client informations (socket,address) in an array and put send()
    in for loop to send to all connected clients .I code what I think but the result still not correct.Here is my code ..if you don't mind I may request your suggestion again
    Thank you so much..

    Code:
    #include <winsock2.h>
    #include <stdio.h>
    #include <string.h>
    #define MAX_CLIENTS 6
    #define MAX_MESSAGE_SIZE 4096
    int startupServerForListening(unsigned short port);
    void shutdownServer(int socket) ;
    HANDLE threadHandle;
    // a buffer to hold my data
    char buffer[MAX_MESSAGE_SIZE];
    char srBuffer[MAX_MESSAGE_SIZE];
    int numConnect = 0;
    int mySocket;
    int nBytes;
    int num = 0;
    char welcomemessage[500] = "Hello Clients!!\0";
    SOCKET clientlist[MAX_CLIENTS];
    //client network data
    struct client_data
    {
    	SOCKET clientsock;
    	struct sockaddr_in client_address;
    };
    int numClients;
    struct hostent *client_entry;
    char hostname[80];
    char *clientName;
    HANDLE mutexHandle;
    struct sockaddr_in client;
    void acceptingThreadProcedure(int* serverSocket) 
    {
    	int nBytesSend;
    	int mySocket = *serverSocket;
    	struct client_data client_handle[MAX_CLIENTS];
    	SOCKET clientSocket;
    	for (;;) {
    	int size = sizeof(struct sockaddr);
                    clientSocket = accept(mySocket,(struct sockaddr*)&client,&size);
    	if (clientSocket == SOCKET_ERROR) {
    	printf("Accept Failed!\n");
    	printf("%d\n",WSAGetLastError());
    	gQuitFlag = true;
    	return;
    	} else {
    	WaitForSingleObject(mutexHandle, INFINITE);
    	clientlist[numClients] = clientSocket;
    	client_handle[numClients].clientsock = clientSocket;
    client_handle[numClients].client_address.sin_addr.s_addr = client.sin_addr.s_addr;
    	printf("client_handle[%d] address is %s\n",numClients,inet_ntoa(client.sin_addr));
    	FD_SET(clientSocket, &masterSet);
    	numClients++;
    	client_entry = gethostbyname(clientName);
    	if (client_entry == 0){
    	printf("Error gethostbyname:%d\n",WSAGetLastError());		
    	}else{
    	ReleaseMutex(mutexHandle);}
                    printf("client on %d connected\n", clientSocket);
    	for (int i=num;i < numClients;i++){			nBytesSend = strlen(welcomemessage);
    	printf("strlen(welcomemessage):: %d\n",nBytesSend);nBytesSend = send(clientlist[numClients],welcomemessage,sizeof(welcomemessage),0);
    	printf("send :%d\n",nBytesSend);			if (nBytesSend == SOCKET_ERROR){
    	WaitForSingleObject(mutexHandle, INFINITE);
    	printf("Some error from winsock :%d\n",WSAGetLastError());
    	ReleaseMutex(mutexHandle);
    	}else
    	{						printf("send message on client on %d connected\n", clientSocket);
    	printf("what I have sent :%s\n",welcomemessage);}
    	}
    	num = i;
    	}
    	}
    }
    
    void main()
    {
    int threadId;
    threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptingThreadProcedure, &serverSocket, 0, (LPDWORD)&threadId);
    if (threadHandle == NULL) 
    {
           printf("Could not start acceptance thread\n");
           shutdownServer(serverSocket);
           return;
    }
    
    
    }
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    can you please tell us exactly what is happening? are you able to send any messages from the server? do the clients just not recieve them? how many mesages can u send?
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    18
    Rep Power
    0
    server can send message to client and client can recieve but
    ,for example,I have clientA, clientB connected to server when I
    send "I'm clientA" from clientA the message should send to server
    and server send the message send to all connected client (clientA and B should recieve message "I'm clientA" too) but when I run server and client program server recieve message send but not send to clientA or clientB. I wonder why server didn't send the message to all clients
  6. #4
  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 thanks, that makes it much easier to help :) how would the server be recieving this message? i dont see any calls to recv() in that thread? it looks like the client connects, and then the server sends a welcome message to each client, where is the server supposed to be recieving "I'm clientA" ? also, i dont understand the logic of your thread... usually you would accept clients in the main, and then pass your thread an already connected client. in your thread you call accept, this means you will have a bunch of threads all competing to lock into accept. not exactly a race condition, but it makes more sense to accept in main and then pass your thread a handle to the client. i'll post some code for a chat roomprogram i wrote in school last year, perhaps that will illustrate what i mean.
    please keep in mind i wrote this more than 6 months ago, it does work but prolly isnt perfect. i havent looked at in a loooong time.
    Last edited by infamous41md; September 12th, 2003 at 02:07 PM.
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    here you go.

    edit:7/27/04 LOL this code is ****in hideous. there are race conditions galore, use at your own risk!!!!!!!!!!
    Attached Files
    Last edited by infamous41md; July 27th, 2004 at 07:51 PM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    18
    Rep Power
    0
    thank you so much..I think I can post my correct code next time
    I want to share my idea to others too

IMN logo majestic logo threadwatch logo seochat tools logo