C++ Server sending message back to client simple solution?

Alright, I am writing this server for my own good and I am not doing it for school or anything like that. It is pretty simple and it works great with no problems and has done what it has needed to do. Now, I am trying to take it a step further and make it to where anything that the client sends to the server will also be sent back to the clients so it could work as a chat program. So if 2 clients were connected to the server, their messages would be sent to the server and then back to them so they could chat to each other. This seems like it should be an easy task, but the reason I cannot figure out how to send messages back from the server to the client is because the way I am doing it requires you to enter a number of characters as a parameter. I have tried using sizeof() and .length() with a string and neither has proved to be successful. Could you show me an example or edit my code so that I can learn how to do this? Pseudocode would be nice as well. I appreciate the help. Once again this is not for school. This is life long learning.

Thanks in advance

If this topic does not fit in regard to the other forums let me know. I am pretty new here and I am still learning how things work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// simple telnet server that can receive commands and messages from multiple clients

DWORD WINAPI receive_cmds(LPVOID lpParam) 
{
  char buf[256] = " ";
  char sendData[256];
  int res;
  string test;

  printf(">A client has connected\n");
  SOCKET current_client = (SOCKET)lpParam; 
  Beep(523,500);

   strcpy(sendData,"(0 to QUIT)\n\rcasey.provost@eagles.usm.edu\n\n\r");
   send(current_client,sendData,45,0);

  while(true) 
  {
	 res = recv(current_client,buf,sizeof(buf[100]),0); 
	 Sleep(10);
     
	 if(res == 0)
	 {
	  closesocket(current_client);
	  ExitThread(0);
	 }
	 
	 if(buf != "0"){ // 0 to exit
	  printf(buf);
	 }

	 if(strstr(buf,"0"))
	 { 
	   printf("\n>Received exit command\n");

	   strcpy(sendData,"Good Bye\n");
	   send(current_client,sendData,8,0);
	   Sleep(1000);

	   closesocket(current_client);
	   printf(">A client has disconnected\n");
	   Beep(659,500);
	   ExitThread(0);

	 }

	   strcpy(sendData,"");
	   strcpy(buf,"");
   }

}   

int main()
{

	
 printf(">Server starting...\r\n");
 
 SOCKET sock;
 printf(">Socket created...\r\n");
 DWORD thread; 
 
 WSADATA wsaData;
 sockaddr_in server;
 
 int ret = WSAStartup(0x101,&wsaData);
 printf(">Winsock started...\r\n");

 if(ret != 0)
 {
	return 0;
 }
  
 server.sin_family=AF_INET; 
 server.sin_addr.s_addr=INADDR_ANY; 
 server.sin_port=htons(23); 
 
 sock=socket(AF_INET,SOCK_STREAM,0); 
 
 if(sock == INVALID_SOCKET)
 {
	return 0;
 }
  
 if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 )
 {
	return 0;
 }
  printf(">Server is online\r\n");
  
 if(listen(sock,5) != 0)
 {
	return 0;
 }
 
 SOCKET client; // socket to be used
 
 sockaddr_in from;
 int fromlen = sizeof(from); 

 while(true) // loop forever
 { 
  client = accept(sock,(struct sockaddr*)&from,&fromlen);
  // accept connections
  CreateThread(NULL, 0,receive_cmds,(LPVOID)client, 0, &thread);  
  // create thread and parse client
 }

 closesocket(sock); 
 WSACleanup(); 
 printf(">Socket closed\r\n");
 return 0;
 // exit
}





Last edited on
If anyone knows what I could do please inform me.
Last edited on
I think my question is being overlooked I am still trying to figure this out though
I've personally done very little network programming and it was in C#, but the overall concept should be the same. What I did was have the server should keep parallel lists (HashTables to be precise)of connections and users. Server can then either broadcast the message to all clients or a specified client. Here is a link to the C# tutorial I used, this is part 2 (part 1 is the client) http://www.geekpedia.com/tutorial240_Csharp-Chat-Part-2---Building-the-Chat-Server.html

EDIT: Here is one in C++ I just found (have not looked it over) http://www.codeproject.com/Articles/14032/Chat-Client-Server
Last edited on
http://www.lacewing-project.org/
As simple as it gets. And trust me, it's very simple.
I am reading about the lacewing library now but I usually have trouble making these libraries work. I am going to also check out that tutorial. I really appreciate it guys, and if anyone else can help let me know. I will let you know if I get it figured out. I would prefer not to have to change the code I have but just make a small change somewhere but I am going to check this stuff out.
Last edited on
Topic archived. No new replies allowed.