473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Multiple clients connect to server

So... I am trying to write a C++ Server that can accept multiple clients. I have already written some very simple code that will accept one client but I am having trouble getting it to accept more than one. Below is the server code I have right now, it works but only for one client.
Expand|Select|Wrap|Line Numbers
  1. #include "info.h"
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. long answer;
  8. char message[256];
  9.  
  10. WSAData wsaData;
  11. WORD DLLVERSION;
  12.  
  13. DLLVERSION = MAKEWORD(2,1);
  14. answer = WSAStartup(DLLVERSION, &wsaData);
  15.  
  16. SOCKADDR_IN addr;
  17. int addrlen = sizeof(addr);
  18. SOCKET sListen;
  19. SOCKET sConnect;
  20. bool x;
  21. sConnect = socket(AF_INET,SOCK_STREAM,NULL);
  22.  
  23. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  24.  
  25. addr.sin_family = AF_INET;
  26. addr.sin_port = htons(1001); // port
  27. sListen = socket(AF_INET,SOCK_STREAM,NULL);
  28. bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
  29. listen(sListen, SOMAXCONN);
  30.  
  31.     cout << "Chat Server" << endl;
  32.     cout << endl;
  33.  
  34.  
  35.     cout << "Waiting for a connection..." << endl;
  36.     if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
  37.     {
  38.         cout << "Connection found!" << endl;
  39.         x = true;
  40. }
  41.  
  42. while(x == true)
  43. {
  44.  
  45.         answer = recv(sConnect, message, sizeof(message)+1, NULL);
  46.         cout << message << endl;
  47.  
  48. }
  49.  
  50. return 0;
  51. }
  52.  



And, here is the client code if you wish to view it...

Expand|Select|Wrap|Line Numbers
  1. #include "header.h"
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. string confirm;
  8. char message[200];
  9. string strmessage;
  10. long answer;
  11. char address[256];
  12. short port;
  13. bool run;
  14.  
  15. WSAData wsaData;
  16. WORD DLLVersion;
  17. DLLVersion = MAKEWORD(2,1);;
  18. answer = WSAStartup(DLLVersion,&wsaData);
  19.  
  20. SOCKADDR_IN addr;
  21. int addrlen = sizeof(addr);
  22.  
  23. SOCKET sConnect;
  24. sConnect = socket(AF_INET, SOCK_STREAM, NULL);
  25.  
  26. //("127.0.0.1");// loopback
  27.  
  28. addr.sin_family = AF_INET;
  29.  
  30.  
  31.     cout << "Chat Client" << endl;
  32.     cout << "Address?" << endl;
  33.     cin.getline(address,256);
  34.     addr.sin_addr.s_addr = inet_addr(address);
  35.     cout << "Port?" << endl;
  36.     cin >> port;
  37.     addr.sin_port = htons(port); // server port
  38.     cout << endl;
  39.  
  40. run = true;
  41.  
  42.     while(run == true){
  43.  
  44.         if(connect(sConnect, (SOCKADDR*)&addr, sizeof(addr))){
  45.  
  46.             cin.getline(message,256);
  47.             answer = send(sConnect, message, sizeof(message), NULL);
  48.  
  49.         }
  50.     }
  51.  
  52. system("pause");
  53. return 0;
  54.  
  55. }
  56.  
  57.  

I did not include the header files because they are probably obvious to you guys, I am new here though and if you wish to view them I will be happy to present them for your viewing pleasure. I am doing this for a learning experience and whether you want to write code or pseudo-code or no variation of code at all I will be worthy to accept any form of advice. I figure that my client does not need to be changed, but if it does, please... let me know.

Thank you.
Dec 8 '11 #1
1 3263
Banfa
9,065 Expert Mod 8TB
The only way to handle multiple connections well is to use multiple threads of execution

Expand|Select|Wrap|Line Numbers
  1. LISTENING-THREAD
  2.  
  3.     CREATE SOCKET
  4.     BIND SOCKET
  5.     LISTEN
  6.  
  7.     DO
  8.         ACCEPT CONNECTION
  9.         START HANDLER-THREAD WITH NEW SOCKET
  10.     UNTIL SOME HALT CONDITION
  11.  
  12. END THREAD
  13.  
  14. HANDLER-THREAD ACCEPTS SOCKET
  15.     READ FROM SOCKET
  16.     HANDLE DATA
  17. END THREAD
  18.  
In theory you actually could use one handler thread but on the whole it is just as easy to have multiple threads unless you plan on have a very large number of connections.
Dec 8 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: User | last post by:
Hi, I tried to find the information over the internet but didn't find any answers. I'm looking for a server side code example of winsock accepting many clients. I know that in VB.NET it is...
2
by: User | last post by:
Hi, Let's say that I run this raw code. This is on a server side, the server will wait for client connection request. Private MyTcpListener As System.Net.Sockets.TcpListener Private MySocket...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.