-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
169 lines (147 loc) · 4.05 KB
/
Copy pathclient.c
File metadata and controls
169 lines (147 loc) · 4.05 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* This is a sample client program using the JRDP library.
*
* The client sends requests to the server.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "rudp.h"
#define SAMPLE_DEFAULT_SUPER_SERVER_HOST (myhostname())
#define SAMPLE_DEFAULT_SERVER_PORT 4007
void p_command_line_preparse( int *argcp, char **argv );
void child_process( int num_loops );
int
main( int argc, char** argv )
{
int j;
int num_of_children = 0;
int status = 0;
int total_messages = 0;
pid_t* child_pid = NULL;
pid_t pid;
p_command_line_preparse( &argc, argv );
jrdp_initialize();
for ( j = 0; j < argc; j++ )
{
if ( argv[j][0] == '-' )
{
if ( argv[j][1] == 'p' )
num_of_children = atoi( &(argv[j][2]) );
else if ( argv[j][1] == 'm' )
total_messages = atoi( &(argv[j][2]) );
}
}
if ( !total_messages )
{
printf( "Need p and m parameters.\n" );
exit(0);
}
child_pid = (pid_t*)malloc( sizeof(pid_t) * num_of_children );
memset( child_pid, 0, sizeof(pid_t) * num_of_children );
for ( j = 0; j < num_of_children; j++ )
{
//child_pid[j] = fork();
//if ( !child_pid[j] )
{
child_process( total_messages / num_of_children );
exit(0);
}
}
for ( j = 0; j < num_of_children; j++ )
{
pid = wait(&status);
if ( ( pid == -1 ) || ( status % 256 ) )
{
perror("client");
exit(1);
}
}
exit(0);
}
void
p_command_line_preparse( int* argcp, char** argv )
{
int num_args_used = 0;
char** scanning;
char** writing;
/* modifies global data */
for ( scanning = writing = argv; *scanning; ++scanning )
{
/* If a - by itself, or --, then no more arguments */
if ( strcmp( *scanning, "-" ) == 0 || strncmp( *scanning, "--", 2 ) == 0 )
break;
if ( strncmp( *scanning, "-D", 2 ) == 0 )
{
printf( "This option enables debug mode, but we do not support this now.\n" );
// eat this argument
++num_args_used;
}
#ifdef MAX_PRI
else if ( strncmp( *scanning, "-N", 2 ) == 0 )
{
jrdp_priority = MAX_PRI; // Use this if no # given
sscanf( *scanning, "-N%d", &jrdp_priority );
if( jrdp_priority > MAX_SPRI )
jrdp_priority = MAX_PRI;
if( jrdp_priority < MIN_PRI )
jrdp_priority = MIN_PRI;
// eat this argument
++num_args_used;
}
#endif
else if ( strncmp( *scanning, "-pc", 3 ) == 0 )
{
printf( "Do not support -pc now.\n" );
//p_command_line_config(*scanning + 3);
++num_args_used;
}
else
{
/* shift the arguments forward */
assert( scanning >= writing );
if ( scanning > writing )
{
*writing = *scanning;
}
writing++;
}
}
/* shift remaining arguments */
while ( *scanning )
*writing++ = *scanning++;
*writing = 0;
*argcp -= num_args_used;
return;
}
void child_process(int num_loops)
{
int cltsock;
int length;
int ret_val;
int flag = JRDP_PACK_COMPLETE;
char mach_name[80];
char BUFFER[256];
int i = 0;
pid_t pid;
//void *dummy;
sprintf( mach_name, "%s(%d)", SAMPLE_DEFAULT_SUPER_SERVER_HOST, SAMPLE_DEFAULT_SERVER_PORT );
cltsock = rudp_connect( mach_name, NULL );
/* Loop N times */
pid = getpid();
for ( i = 1; i < num_loops + 1; ++i )
{
sprintf( BUFFER, "This is the client #%d sending to super server msg #%d\n", (int)pid, i );
length = strlen(BUFFER);
if ( ( ret_val = rudp_send( cltsock, flag, BUFFER, length, -1 ) ) )
{
printf( "jrdp_send, status err.\n" );
exit(1);
}
}
rudp_disconnect(cltsock);
}