![]() |
| | #1 |
| Registered User Join Date: Aug 2003
Posts: 288
| Server Client Messaging Program 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;
}
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;
}
|
| X PaYnE X is offline | |
| | #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;
}
|
| X PaYnE X is offline | |
| | #3 | ||
| Yes, my avatar is stolen Join Date: Dec 2002
Posts: 2,544
| Quote:
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. Quote:
If the code is not too massive you may want to post it all as an attachment. | ||
| anonytmouse is offline | |
| | #4 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,865
| 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. |
| Hunter2 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Programming chat client, need some help (sockets & threads) | lalilulelo17 | Linux Programming | 1 | 04-19-2008 04:01 AM |
| server client application - (i really need your help) | sarahnetworking | C Programming | 3 | 03-01-2008 10:54 PM |
| Socket Programming Problem!!!! | bobthebullet990 | Networking/Device Communication | 2 | 02-21-2008 07:36 PM |
| Commerical MMORPG Developer Opp | Th3Guy | Projects and Job Recruitment | 19 | 01-22-2007 11:28 AM |
| Client abnormally terminates when server isn't found | Brain Cell | Networking/Device Communication | 10 | 03-16-2005 04:29 AM |