1 | #include "cf_socket.h"
|
---|
2 | #include "cf_debug.h"
|
---|
3 | #include "cf_log.h"
|
---|
4 | #include "cf_thread.h"
|
---|
5 |
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <errno.h>
|
---|
8 | #include <string.h>
|
---|
9 |
|
---|
10 | #define LOG_SOCKET 0
|
---|
11 | #define THREAD_POOL 1
|
---|
12 | #define PORT 1234
|
---|
13 |
|
---|
14 | int socket_echo_server (void * arg)
|
---|
15 | {
|
---|
16 | int srvsock = *((int *)arg);
|
---|
17 | int clntsock = 0;
|
---|
18 | int recvd = 0;
|
---|
19 | char buf[1024] = {0x00,};
|
---|
20 | int i = 0;
|
---|
21 |
|
---|
22 | /*------------------------------------------------------------*/
|
---|
23 | clntsock = CF_Socket_Accept (srvsock);
|
---|
24 | if (clntsock < 0)
|
---|
25 | {
|
---|
26 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
27 | return -1;
|
---|
28 | }
|
---|
29 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
30 |
|
---|
31 | for (i = 0 ; i < 5 ; i++)
|
---|
32 | {
|
---|
33 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
34 | {
|
---|
35 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
36 | return -2;
|
---|
37 | }
|
---|
38 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
39 | CF_DEBUG_PRINT (stderr, "[SERVER] recv {%s}\n", buf);
|
---|
40 |
|
---|
41 | if (CF_Socket_Send (clntsock, buf, (size_t)recvd) < 0)
|
---|
42 | {
|
---|
43 | return -3;
|
---|
44 | }
|
---|
45 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
46 | CF_DEBUG_PRINT (stderr, "[SERVER] resp {%s}\n", buf);
|
---|
47 | }
|
---|
48 |
|
---|
49 | CF_Socket_Close (clntsock);
|
---|
50 |
|
---|
51 | return 0;
|
---|
52 | /*------------------------------------------------------------*/
|
---|
53 | }
|
---|
54 |
|
---|
55 | int socket_echo_client (void * arg)
|
---|
56 | {
|
---|
57 | int sock = 0;
|
---|
58 | int recvd = 0;
|
---|
59 | char buf[1024] = {0x00,};
|
---|
60 | int i = 0;
|
---|
61 |
|
---|
62 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
63 |
|
---|
64 | /*------------------------------------------------------------*/
|
---|
65 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
66 | if (sock < 0)
|
---|
67 | {
|
---|
68 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
69 | return -1;
|
---|
70 | }
|
---|
71 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
72 |
|
---|
73 | for (i = 0 ; i < 5 ; i++)
|
---|
74 | {
|
---|
75 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
76 | {
|
---|
77 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
78 | return -2;
|
---|
79 | }
|
---|
80 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
81 | CF_DEBUG_PRINT (stderr, "[CLIENT] sent {%s}\n", buf);
|
---|
82 |
|
---|
83 | memset (buf, 0x00, sizeof (buf));
|
---|
84 |
|
---|
85 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
86 | {
|
---|
87 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
88 | return -3;
|
---|
89 | }
|
---|
90 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
91 | CF_DEBUG_PRINT (stderr, "[CLIENT] recv {%s}\n", buf);
|
---|
92 | }
|
---|
93 |
|
---|
94 | CF_Socket_Close (sock);
|
---|
95 |
|
---|
96 | return 0;
|
---|
97 | /*------------------------------------------------------------*/
|
---|
98 | }
|
---|
99 |
|
---|
100 | int main ()
|
---|
101 | {
|
---|
102 | CF_Thread_Ctx stid[THREAD_POOL];
|
---|
103 | CF_Thread_Ctx ctid[THREAD_POOL];
|
---|
104 |
|
---|
105 | int sock = 0;
|
---|
106 | int iter = 0;
|
---|
107 |
|
---|
108 | /*------------------------------------------------------------*/
|
---|
109 | if (CF_Log_Initialize (1) < 0)
|
---|
110 | {
|
---|
111 | fprintf (stderr, "failed to init. log\n");
|
---|
112 | return -1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_DEFAULT_BUFFER) < 0)
|
---|
116 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
117 |
|
---|
118 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
119 |
|
---|
120 | if (CF_Socket_Initialize () < 0)
|
---|
121 | {
|
---|
122 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
123 | return -2;
|
---|
124 | }
|
---|
125 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
126 |
|
---|
127 | sock = CF_Socket_Server (PORT, 5);
|
---|
128 | if (sock < 0)
|
---|
129 | {
|
---|
130 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
131 | return -3;
|
---|
132 | }
|
---|
133 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
134 |
|
---|
135 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
136 | {
|
---|
137 | if (CF_Thread_CreateCtx (&stid[iter], socket_echo_server, &sock) < 0)
|
---|
138 | {
|
---|
139 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
140 | return -4;
|
---|
141 | }
|
---|
142 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
143 | }
|
---|
144 |
|
---|
145 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
146 | {
|
---|
147 | if (CF_Thread_CreateCtx (&ctid[iter], socket_echo_client, &sock) < 0)
|
---|
148 | {
|
---|
149 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
150 | return -5;
|
---|
151 | }
|
---|
152 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
153 | }
|
---|
154 |
|
---|
155 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
156 | {
|
---|
157 | if (CF_Thread_Start (stid[iter]) < 0)
|
---|
158 | {
|
---|
159 | CF_DEBUG_PRINT (stderr, "failed to start %dth thread\n", iter);
|
---|
160 | return -6;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
165 | {
|
---|
166 | if (CF_Thread_Start (ctid[iter]) < 0)
|
---|
167 | {
|
---|
168 | CF_DEBUG_PRINT (stderr, "failed to start %dth thread\n", iter);
|
---|
169 | return -7;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
174 | {
|
---|
175 | CF_Thread_Join (ctid[iter]);
|
---|
176 | CF_Thread_DestroyCtx (ctid[iter]);
|
---|
177 |
|
---|
178 | CF_Thread_Join (stid[iter]);
|
---|
179 | CF_Thread_DestroyCtx (stid[iter]);
|
---|
180 |
|
---|
181 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
182 | }
|
---|
183 |
|
---|
184 | CF_Socket_Close (sock);
|
---|
185 |
|
---|
186 | CF_Socket_Finalize ();
|
---|
187 |
|
---|
188 | CF_Log_Close (LOG_SOCKET);
|
---|
189 |
|
---|
190 | CF_Log_Finalize ();
|
---|
191 | /*------------------------------------------------------------*/
|
---|
192 |
|
---|
193 | return 0;
|
---|
194 | }
|
---|