Click here to Skip to main content
16,016,605 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am working on TCP Client Simulator on C++(windows and Linux).Here can simulate 1000 client.each client has some status information which server may require.Server can get the status of a particular client by issuing command with client ID.

Server may issue these command
1."start-stream"-streams the data to server continually.
2."stop stream"-stops the streaming
3."Status"-asking for status

client respond with
1.streams the data
2.stops the streaming
3.send status information to server.


We can't use third party libraries(boost etc) to do this.I need to implement in C++.I came across the Several I/O model like Completion Port,Overlapped I/O etc.I am confused which one will suitable for the client application.

1.I wanted to know what I/O model is suitable for Client to manage these number instance.

2. how can I ensure the state of the each client instance.
3. data structure to maintain the status information of the each client.
Posted
Updated 16-Mar-11 0:16am
v2
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 14:22pm    
I did not vote on the answers because the use of textspeak, which is considered impolite.
I did not consider the references, may be they are good (just for your information).
--SA
Harrison H 18-Mar-11 19:02pm    
Why is that you can't use third party libraries? You're going to have to roll more than just your own socket abstraction for different OSes, you'll also need to take care of threading, and you won't be able to fork on windows...

This just sounds like a bad situation to me. If this isn't some wacky school project, boost is basically your best friend and used in plenty of commercial software.

If you can't use open source libraries - like ACE[^], you obviously have to roll your own alternative.
Check out the POSA2 book, Patterns for Concurrent and Networked Objects[^]

Patterns are recipes on how to implement functional software based on successful implementations.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 14:18pm    
Fundamental. What, ACE again? My 5.
--SA
Espen Harlinn 16-Mar-11 15:11pm    
As Douglas C. Schmidt, one of the authors of the POSA2 book, is the principal architect and driving force behind ACE - it was an obvious recommendation :)
Sergey Alexandrovich Kryukov 16-Mar-11 15:25pm    
Thank you. As I'm not familiar with him or his works yet, it does not say anything to me...
Got any good references?
--SA
Espen Harlinn 16-Mar-11 15:49pm    
You're kidding, his one of the most widely published "serious" scientists/authors in the industry - http://www.cs.wustl.edu/~schmidt/resume.html
Sergey Alexandrovich Kryukov 16-Mar-11 17:05pm    
No kidding. Thank you.
--SA
Hi, lok.vikram ,go through this link i think this will help u


Single Server With Multiple Clients : a Simple C++ Implementation[^]
 
Share this answer
 
v2
hi lok.vikram,go through this link i think this will help u

www.madwizard.org/programming/tutorials/netcpp/5
 
Share this answer
 
v2
Comments
lok.vikram 16-Mar-11 7:30am    
It describes all the I/O model windows platform.what about linux TCP Client Simulator need to run on both windows and linux.
lok.vikram 18-Mar-11 2:59am    
Client may need to stream data continually until stop command sent by server.Application may need simulate 1000 client.
Himansu sekhar bal 18-Mar-11 7:53am    
http://www.kegel.com/c10k.html#nb.edge,
sir go to this side.u got some idea,or see solution-4
C++
Write and compile the Server

The server listens on port 1101 for a connection. When it receives a connection it creates a thread via pthread_create to handle it. This thread then reads from the socket, appends " SERVER ECHO" to it and sends it back to the client. As the server doesn't require an IP address, we assign INADDR_ANY to the sockaddr_in struct. Save this code as LinSever.cpp.
 1. #include <fcntl.h>
 2. #include <string.h>
 3. #include <stdlib.h>
 4. #include <errno.h>
 5. #include <stdio.h>
 6. #include <netinet/in.h>
 7. #include <resolv.h>
 8. #include <sys/socket.h>
 9. #include <arpa/inet.h>
10. #include <unistd.h>
11. #include <pthread.h>
12.
13. void* SocketHandler(void*);
14.
15. int main(int argv, char** argc){
16.
17.     int host_port= 1101;
18.
19.     struct sockaddr_in my_addr;
20.
21.     int hsock;
22.     int * p_int ;
23.     int err;
24.
25.     socklen_t addr_size = 0;
26.     int* csock;
27.     sockaddr_in sadr;
28.     pthread_t thread_id=0;
29.
30.     int childpid;
31.
32.     signal(SIGCHLD, SIG_IGN);
33.
34.     hsock = socket(AF_INET, SOCK_STREAM, 0);
35.     if(hsock == -1){
36.         printf("Error initializing socket %d\n", errno);
37.         goto FINISH;
38.     }
39.
40.     p_int = (int*)malloc(sizeof(int));
41.     *p_int = 1;
42.
43.     if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
44.         (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
45.         printf("Error setting options %d\n", errno);
46.         free(p_int);
47.         goto FINISH;
48.     }
49.     free(p_int);
50.
51.
52.     my_addr.sin_family = AF_INET ;
53.     my_addr.sin_port = htons(host_port);
54.
55.     memset(&(my_addr.sin_zero), 0, 8);
56.     my_addr.sin_addr.s_addr = INADDR_ANY ;
57.
58.     if( bind( hsock, (sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
59.         fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",errno);
60.         goto FINISH;
61.     }
62.     if(listen( hsock, 10) == -1 ){
63.         fprintf(stderr, "Error listening %d\n",errno);
64.         goto FINISH;
65.     }
66.
67.     //Now lets do the server stuff
68.
69.     addr_size = sizeof(sockaddr_in);
70.
71.     while(true){
72.         printf("waiting for a connection\n");
73.         csock = (int*)malloc(sizeof(int));
74.         if((*csock = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1){
75.             printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr));
76.             switch( childpid=fork()){
77.                 case -1://error
78.                     fprintf(stderr, "Error spawning the child %d\n",errno);
79.                     exit(0);
80.                     break;
81.                 case 0://in the child
82.                     SocketHandler(csock);
83.                     exit(0);
84.                 default://in the server
85.                     close(*csock);
86.                     free(csock);
87.                     break;
88.             }
89.         }
90.         else{
91.             fprintf(stderr, "Error accepting %d\n", errno);
92.         }
93.     }
94.
95. FINISH:
96. ;
97. }
98.
99. void* SocketHandler(void* lp){
100.     int *csock = (int*)lp;
101.
102.     char buffer[1024];
103.     int buffer_len = 1024;
104.     int bytecount;
105.
106.     memset(buffer, 0, buffer_len);
107.     if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
108.         fprintf(stderr, "Error receiving data %d\n", errno);
109.         goto FINISH;
110.     }
111.     printf("Received bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
112.     strcat(buffer, " SERVER ECHO");
113.
114.     if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
115.         fprintf(stderr, "Error sending data %d\n", errno);
116.         goto FINISH;
117.     }
118.
119.     printf("Sent bytes %d\n", bytecount);
120.
121.
122. FINISH:
123.     free(csock);
124.     return 0;
125. }Hide line numbers

Line 32: When a child process exits without a call to 'wait()' from the parent, it turns into a 'zombie.' You can prevent this by setting signal(SIGCHLD, SIG_IGN);

Line 76: Fork returns -1 in the parent on failure. It return the child pid in the parent and 0 in the child.

Line 85: When you fork, you must close the client connection in the parent(server) process, otherwise you will hit the "open socket descriptor limit" in Linux.
`
Open a prompt to the working directory and compile the code using your C++ compiler

..workspace\SocketExample>g++ -o server LinServer.cpp -lpthread


Write and compile the Client

The client reads a line from the console and sends this to the server. It then reads from the server and displays it on the console. You can view the client in this tutorial Linux C++ Socket Example with Client Server and Mulit-Threading .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900