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