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

    Join Date
    Sep 2003
    Posts
    18
    Rep Power
    0

    Unhappy hello again ..for broadcasting TCP server


    For my last problem I solved it.thanks a lot for suggestion

    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;
    	}
    	}
    }
     //*******************************************//
       in function main I call thread to handle multiple clients
     //*******************************************//
    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. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Please edit your post and replace the quote tags with code tags; i.e. within the square brackets, replace the word "quote" with "code".

    You had the right idea, but the wrong tag.

    Also, please click on the "Disable Smilies in This Post" check box beneath the edit box.
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    and you also havent said what the actual problem is?

IMN logo majestic logo threadwatch logo seochat tools logo