Thread: Server Client Messaging Program

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    Server Client Messaging Program

    Im trying to make a program that allows 1 server to have multiple clients connected to it.

    So far I have the server set up quite well, it allows many clients to connect at once and the clients can all send data to the server.

    Im having 1 little problem on each side, the server cant send data and the client side cant receive data, I have no idea how to go about fixing this since this is the first time I ever multithread.

    For the server program, to send data to the client (assuming you have like 10 clients connected), how can you pick out 1 specific client and actually send the data to him?

    Heres the code I use for the server side to send data:

    Code:
    bool SendtoClient(char *Buffer)
    {
    	unsigned long messageSize = strlen(Buffer);
    
    	messageSize = htonl(messageSize);
    
    	if (send(Client, (char*)&messageSize, sizeof(messageSize), 0) == SOCKET_ERROR)
    		return false;
    
    	messageSize = ntohl(messageSize);
    
    	if (send(Client, Buffer, messageSize, 0) == SOCKET_ERROR)
    		return false;
    }
    Client is a SOCKET, i guess it should point to the actual client socket?

    This function is called when a user selects the Clients SOCKET address from a list, and clicks send. I think this function should work but im not really sure if your supposed to put the Server's SOCKET or the Client's SOCKET in the first parameter of the send function.

    For the Client side i have similar code but instead of using the Server's SOCKET, i use the Client's SOCKET and it works..

    Code:
    bool SendtoServer(char *Buffer)
    {
    	unsigned long messageSize = strlen(Buffer);
    
    	messageSize = htonl(messageSize);
    
    	if (send(Client, (char*)&messageSize, sizeof(messageSize), 0) == SOCKET_ERROR)
    		return false;
    
    	messageSize = ntohl(messageSize);
    
    	if (send(Client, Buffer, messageSize, 0) == SOCKET_ERROR)
    		return false;
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    Also i was wondering if this function returns the SOCKET for the client or the server??

    Code:
    SOCKET StartClient(char *Address, unsigned short Port) 
    {
    	WSAData wsaData;
    
    	if (WSAStartup(WINSOCK_VERSION, &wsaData) == SOCKET_ERROR) 
    	{
    		MessageBox(NULL, "Failed to Startup Winsock", "", MB_OK);
    		return SOCKET_ERROR;
    	}
    
    	SOCKET Socket = socket(AF_INET, SOCK_STREAM, 0);
    
    	if (Socket == SOCKET_ERROR) 
    	{
    		MessageBox(NULL, "Error Creating Socket", "", MB_OK);
    		return SOCKET_ERROR;
    	}
    
    	struct hostent *HostEntry;
    
    	if ((HostEntry = gethostbyname(Address)) == NULL) 
    	{
    		MessageBox(NULL, "Could not find host", "", MB_OK);
    		return SOCKET_ERROR;
    	}
    
    	struct sockaddr_in Server;
    
    	ZeroMemory(&Server, sizeof(Server));
    
    	Server.sin_family = AF_INET;
    	Server.sin_port = htons(Port);
    	Server.sin_addr.s_addr = *(unsigned long*)HostEntry->h_addr;
    
    	if (connect(Socket, (sockaddr*)&Server, sizeof(Server)) == SOCKET_ERROR) 
    	{
    		MessageBox(NULL, "Error connecting to server", "", MB_OK);
    		return SOCKET_ERROR;
    	}
    
    	return Socket;
    }

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Also i was wondering if this function returns the SOCKET for the client or the server??

    SOCKET StartClient(char *Address, unsigned short Port)
    This function creates a socket and connects it to Address.
    For example, if Address equals "192.168.0.55":

    When you send() on the returned socket the data will be sent to "192.168.0.55".

    When you recv() on the returned socket you will receive data sent to you by "192.168.0.55".

    It looks like you use this function in the client to create a connection to the server.

    Client is a SOCKET, i guess it should point to the actual client socket?
    Yes, this should be the SOCKET returned from the call to accept(). Maybe you could post your accept() loop as this sounds like it may be the problem.

    If the code is not too massive you may want to post it all as an attachment.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Just a comment, I believe the proper value to check for after calling socket() is INVALID_SOCKET rather than SOCKET_ERROR.
    Code:
    if (Socket == INVALID_SOCKET) 
    {
    	MessageBox(NULL, "Error Creating Socket", "", MB_OK);
    	return INVALID_SOCKET;
    }
    //and other instances of "return SOCKET_ERROR"
    //should also be "return INVALID_SOCKET"
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  3. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  4. Commerical MMORPG Developer Opp
    By Th3Guy in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 01-22-2007, 11:28 AM
  5. Client abnormally terminates when server isn't found
    By Brain Cell in forum Networking/Device Communication
    Replies: 10
    Last Post: 03-16-2005, 04:29 AM