xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | #include <ctype.h>
|
| 2 | #include <errno.h>
|
| 3 | #include <unistd.h>
|
| 4 | #include <getopt.h>
|
| 5 | #include <limits.h>
|
| 6 | #include <netdb.h>
|
| 7 | #include <stdbool.h>
|
| 8 | #include <stdint.h>
|
| 9 | #include <stdio.h>
|
| 10 | #include <stdlib.h>
|
| 11 | #include <string.h>
|
| 12 | #include <time.h>
|
| 13 | #include <syslog.h>
|
| 14 | #include <pthread.h>
|
| 15 | #include <sys/socket.h>
|
| 16 | #include <sys/types.h>
|
| 17 | #include <sys/un.h>
|
| 18 | #include <sys/ioctl.h>
|
| 19 | #include <fcntl.h>
|
| 20 | #include "linklist.h"
|
| 21 | #include "socket_tok.h"
|
| 22 |
|
| 23 | /*command max len 64*/
|
| 24 | #define SOCKET_CMD_MAX_LEN 1024
|
| 25 | #define BUF_SIZE 1024
|
| 26 |
|
| 27 | #define EXIT_CMD_STOP "stop\n"
|
| 28 | #define EXIT_CMD_Q "q\n"
|
| 29 | #define EXIT_CMD_EXIT "exit\n"
|
| 30 |
|
| 31 | #define SOCKET_CREATE "sock_create"
|
| 32 | #define SOCKET_CREATECLIENT "sock_createclient"
|
| 33 | #define SOCKET_CREATESERVER "sock_createserver"
|
| 34 | #define SOCKET_BIND "sock_bind"
|
| 35 | #define SOCKET_LISTEN "sock_listen"
|
| 36 | #define SOCKET_ACCEPT "sock_accept"
|
| 37 | #define SOCKET_CONN "sock_conn"
|
| 38 | #define SOCKET_SEND "sock_send"
|
| 39 | #define SOCKET_SENDTO "sock_sendto"
|
| 40 | #define SOCKET_WRITE "sock_write"
|
| 41 | #define SOCKET_RECV "sock_recv"
|
| 42 | #define SOCKET_RECVFROM "sock_recvfrom"
|
| 43 | #define SOCKET_READ "sock_read"
|
| 44 | #define SOCKET_CLOSE "sock_close"
|
| 45 | #define SOCKET_LOCALTEST "sock_localtest"
|
| 46 |
|
| 47 | #define sock_param_len 4096
|
| 48 |
|
| 49 | typedef unsigned short UINT16;
|
| 50 | typedef unsigned long int UINT32;
|
| 51 |
|
| 52 | static int g_sockfd = -1;
|
| 53 |
|
| 54 | static char* sms_convert_cardinfo_state(int type, int value);
|
| 55 |
|
| 56 | static void printUsage(const char *Opt)
|
| 57 | {
|
| 58 | printf("Usage: %s\n", Opt);
|
| 59 | printf("sock_create:type create a new socket(eg. sock_create:1)\n");
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 60 | printf("sock_createclient:type,addr,data create a new client socket(eg. sock_createclient:1,/var/sockclient,data)\n");
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 61 | printf("sock_createserver:type,addr,backlog create a new server socket(eg. sock_createserver:1,/var/sockserver,30)\n");
|
| 62 | printf("sock_bind:fd,addr socket bind(eg. sock_bind:fd,/var/sockclient)\n");
|
| 63 | printf("sock_listen:fd,backlog socket listen(eg. sock_listen:fd,30)\n");
|
| 64 | printf("sock_accept:fd socket accept(eg. sock_accept:fd)\n");
|
| 65 | printf("sock_conn:fd,srvaddr conn server socket(eg. sock_conn:fd, /var/sockserver)\n");
|
| 66 | printf("sock_send:fd,message send socket message(eg. sock_send:fd,ipc test)\n");
|
| 67 | printf("sock_sendto:fd,srvaddr,message sendto socket message(eg. sock_sendto:fd,/var/sockserver,ipc test)\n");
|
| 68 | printf("sock_write:fd,message write socket message(eg. sock_write:fd,ipc test)\n");
|
| 69 | printf("sock_recv:fd recv socket message(eg. sock_recv: fd)\n");
|
| 70 | printf("sock_recvfrom:fd recvfrom socket message(eg. sock_recvfrom: fd)\n");
|
| 71 | printf("sock_read:fd read socket message(eg. sock_read: fd)\n");
|
| 72 | printf("sock_close:fd close socket message(eg. sock_close: fd)\n");
|
| 73 | printf("sock_localtest: test local socket operation\n");
|
| 74 | printf("\n");
|
| 75 | }
|
| 76 |
|
| 77 | static int parseOpts(int argc, char *argv[])
|
| 78 | {
|
| 79 | int rc = 0;
|
| 80 | int c;
|
| 81 | while ((c = getopt(argc, argv, "?n:a:")) != -1) {
|
| 82 | switch (c) {
|
| 83 | case 'a':
|
| 84 | break;
|
| 85 | case 'n':
|
| 86 | break;
|
| 87 | case '?':
|
| 88 | default:
|
| 89 | printUsage(argv[0]);
|
| 90 | return -1;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | return rc;
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | enum sock_type {
|
| 99 | SOCK_STREAM = 1,
|
| 100 | SOCK_DGRAM = 2,
|
| 101 | SOCK_RAW = 3,
|
| 102 | SOCK_RDM = 4,
|
| 103 | SOCK_SEQPACKET = 5,
|
| 104 | SOCK_DCCP = 6,
|
| 105 | SOCK_PACKET = 10,
|
| 106 | };
|
| 107 | **/
|
| 108 | static int socket_create(char *data, int data_len)
|
| 109 | {
|
| 110 | int sockfd = -1;
|
| 111 | int type = 0;
|
| 112 | printf("socket_create test \n");
|
| 113 |
|
| 114 | if (at_tok_start(&data) < 0) {
|
| 115 | printf("socket_create:at_tok_start error\n");
|
| 116 | }
|
| 117 |
|
| 118 | if(at_tok_nextint(&data, &type) < 0) {
|
| 119 | printf("socket_create:at_tok_nextstr error\n");
|
| 120 | }
|
| 121 | printf("socket_create:type=%d\n", type);
|
| 122 |
|
| 123 | sockfd = socket(AF_UNIX, type, 0);
|
| 124 | if(sockfd < 0){
|
| 125 | printf("socket_create error: %s(errno: %d) \n", strerror(errno), errno);
|
| 126 | }else{
|
| 127 | printf("socket_create sucess:sockfd=%d\n", sockfd);
|
| 128 | }
|
| 129 | g_sockfd = sockfd;
|
| 130 | return 0;
|
| 131 | }
|
| 132 |
|
| 133 | static int socket_test_local()
|
| 134 | {
|
| 135 | int clientfd = -1;
|
| 136 | int serverfd = -1;
|
| 137 | int numRecv;
|
| 138 | char data[BUF_SIZE] = {0};
|
| 139 | struct sockaddr_un my_addr = {0};
|
| 140 |
|
| 141 | if((clientfd = socket(AF_UNIX, 2, 0))<0){
|
| 142 | printf("socket_test_local: create client socket error \n");
|
| 143 | return -1;
|
| 144 | }
|
| 145 |
|
| 146 | if((serverfd = socket(AF_UNIX, 2, 0))<0){
|
| 147 | printf("socket_test_local: create server socket error \n");
|
| 148 | return -1;
|
| 149 | }
|
| 150 |
|
| 151 | memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
| 152 | my_addr.sun_family = AF_UNIX;
|
| 153 | strncpy(my_addr.sun_path, "/var/socktest", sizeof(my_addr.sun_path)-1);
|
| 154 |
|
| 155 | //·þÎñ¶Ë°ó¶¨µØÖ·
|
| 156 | if(bind(serverfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
|
| 157 | printf("server socket °ó¶¨³É¹¦ \n");
|
| 158 | }
|
| 159 | else{
|
| 160 | printf("server socket °ó¶¨Ê§°Ü \n");
|
| 161 | }
|
| 162 |
|
| 163 | //δÁ¬½Ó£¬¿Í»§¶ËÖ±½Ó¸ø·þÎñ¶Ë·¢ËÍÊý¾Ý
|
| 164 | if(sendto(clientfd, "send data to server test one", 28, 0, &my_addr, sizeof(my_addr)) < 0){
|
| 165 | printf("send data to server test one failed \n");
|
| 166 | }
|
| 167 |
|
| 168 | //¶ÁÈ¡Êý¾Ý
|
| 169 | if(read(serverfd, data, BUF_SIZE-1) < 0){
|
| 170 | printf("recv data test one failed \n");
|
| 171 | }else{
|
| 172 | printf("recv data test one sucess \n");
|
| 173 | }
|
| 174 |
|
| 175 | //Á¬½Ó·þÎñ¶Ë
|
| 176 | if(connect(clientfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0){
|
| 177 | printf("connect error: %s(errno: %d) \n", strerror(errno), errno);
|
| 178 | return -1;
|
| 179 | }
|
| 180 |
|
| 181 | if(send(clientfd, "send data to server test two", 28, 0) < 0){
|
| 182 | printf("send data to server test two failed: %s(errno: %d) \n", strerror(errno), errno);
|
| 183 |
|
| 184 | }else{
|
| 185 | printf("send data to server test two sucess \n");
|
| 186 | }
|
| 187 |
|
| 188 | memset(data, 0, sizeof(data));
|
| 189 | if(numRecv = recv(serverfd, data, sizeof(data)-1, 0) == -1){
|
| 190 | printf("recv data test two failed \n");
|
| 191 | }else{
|
| 192 | printf("recv data test two sucess \n");
|
| 193 | }
|
| 194 |
|
| 195 | //²âÊÔ·þÎñ¶Ë¸ø¿Í»§¶Ë·¢ÏûÏ¢ÊÇ·ñÄÜͨ
|
| 196 | if(send(serverfd, "send data to server test three", 30, 0) < 0){
|
| 197 | printf("send data to server test three failed: %s(errno: %d) \n", strerror(errno), errno);
|
| 198 | }else{
|
| 199 | printf("send data to server test three sucess \n");
|
| 200 | }
|
| 201 | fcntl(clientfd, F_SETFL, O_NONBLOCK);
|
| 202 | memset(data, 0, sizeof(data));
|
| 203 | if(numRecv = recv(clientfd, data, sizeof(data)-1, 0) == -1){
|
| 204 | printf("recv data test three failed \n");
|
| 205 | }else{
|
| 206 | printf("recv data test three sucess \n");
|
| 207 | }
|
| 208 |
|
| 209 | //¸ø¿Í»§¶Ë°ó¶¨µØÖ·
|
| 210 | memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
| 211 | my_addr.sun_family = AF_UNIX;
|
| 212 | strncpy(my_addr.sun_path, "/var/sockclient", sizeof(my_addr.sun_path)-1);
|
| 213 |
|
| 214 | //¿Í»§¶Ë°ó¶¨µØÖ·
|
| 215 | if(bind(clientfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
|
| 216 | printf("client socket °ó¶¨³É¹¦ \n");
|
| 217 | }
|
| 218 | else{
|
| 219 | printf("client socket °ó¶¨Ê§°Ü \n");
|
| 220 | }
|
| 221 |
|
| 222 | //δÁ¬½Ó£¬·þÎñ¶ËÖ±½Ó¸ø¿Í»§¶Ë·¢ËÍÊý¾Ý
|
| 223 | if(sendto(serverfd, "send data to server test four", 29, 0, &my_addr, sizeof(my_addr)) < 0){
|
| 224 | printf("send data to server test four failed \n");
|
| 225 | }else{
|
| 226 | printf("send data to server test four sucess \n");
|
| 227 | }
|
| 228 |
|
| 229 | //¶ÁÈ¡Êý¾Ý
|
| 230 | if(read(clientfd, data, BUF_SIZE-1) < 0){
|
| 231 | printf("recv data test four failed \n");
|
| 232 | }else{
|
| 233 | printf("recv data test four sucess \n");
|
| 234 | }
|
| 235 |
|
| 236 | close(clientfd);
|
| 237 | close(serverfd);
|
| 238 | }
|
| 239 |
|
| 240 | void socket_test_dgram_data(int fd, char* paddr, char* pdata){
|
| 241 | struct sockaddr_un other_addr = {0};
|
| 242 | ssize_t numRead;
|
| 243 | ssize_t numRecv;
|
| 244 | char buf[2048] = {0};
|
| 245 | int sockfd = -1;
|
| 246 | sockfd = fd;
|
| 247 | int ret;
|
| 248 | memset(&other_addr, 0, sizeof(struct sockaddr_un));
|
| 249 | other_addr.sun_family = AF_UNIX;
|
| 250 | strncpy(other_addr.sun_path, paddr, sizeof(other_addr.sun_path)-1);
|
| 251 |
|
| 252 | //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
|
| 253 | if(other_addr.sun_path[0] == 'z' || other_addr.sun_path[0]=='Z'){
|
| 254 | printf("socket_test_dgram_data: anonymous socket\n");
|
| 255 | other_addr.sun_path[0] = '\0';
|
| 256 | }
|
| 257 | //δÁ¬½Ó£¬¿Í»§¶ËÖ±½Ó¸ø·þÎñ¶Ë·¢ËÍÊý¾Ý
|
| 258 | if(sendto(sockfd, "send dgram data to server test one", 34, 0, &other_addr, sizeof(other_addr)) < 0){
|
| 259 | printf("sendto dgram data to server test one failed \n");
|
| 260 | }else{
|
| 261 | printf("sendto dgram data to server test one sucess \n");
|
| 262 | }
|
| 263 | #if 0
|
| 264 | memset(buf, 0, sizeof(buf));
|
| 265 | if(recvfrom(sockfd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&other_addr, sizeof(other_addr)) <= 0){
|
| 266 | printf("recvfrom: dgram data to server test one failed \n");
|
| 267 | }else{
|
| 268 | printf("recvfrom: dgram data to server test one sucess \n");
|
| 269 | }
|
| 270 | #endif
|
| 271 | //Á¬½Ó·þÎñ¶Ë
|
| 272 | if(ret = connect(sockfd, (struct sockaddr*)&other_addr, sizeof(other_addr)) < 0){
|
| 273 | printf("socket_create_client: connect error:ret=%d, %s(errno: %d) \n", ret, strerror(errno), errno);
|
| 274 |
|
| 275 | return -1;
|
| 276 | }else{
|
| 277 | printf("³É¹¦Óë·þÎñÆ÷½¨Á¢Á¬½Ó: sockfd=%d\n", sockfd);
|
| 278 | }
|
| 279 |
|
| 280 |
|
| 281 | printf("send msg to server: \n");
|
| 282 | memset(buf, 0, sizeof(buf));
|
| 283 |
|
| 284 | if(sendto(sockfd, "send dgram data to server test two", 34, 0, &other_addr, sizeof(other_addr)) < 0){
|
| 285 | printf("sendto dgram data to server test two failed \n");
|
| 286 | }else{
|
| 287 | printf("sendto dgram data to server test two sucess \n");
|
| 288 | }
|
| 289 |
|
| 290 | #if 0
|
| 291 | if(recvfrom(sockfd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&other_addr, sizeof(other_addr)) <= 0){
|
| 292 | printf("recvfrom: dgram data to server two failed \n");
|
| 293 | }else{
|
| 294 | printf("recvfrom: dgram data to server two sucess \n");
|
| 295 | }
|
| 296 | #endif
|
| 297 |
|
| 298 | memset(buf, 0, sizeof(buf));
|
| 299 | // send¡¢recv²âÊÔ
|
| 300 | if(send(sockfd, "send dgram data to server test three", 36, 0) < 0){
|
| 301 | printf("send dgram data to server test three failed \n");
|
| 302 | }else{
|
| 303 | printf("send dgram data to server test three sucess \n");
|
| 304 | }
|
| 305 |
|
| 306 | #if 0
|
| 307 | if(recv(sockfd, buf, sizeof(buf)-1, 0) <= 0){
|
| 308 | printf("recvfrom: dgram data to server three failed \n");
|
| 309 | }else{
|
| 310 | printf("recvfrom: dgram data to server three sucess \n");
|
| 311 | }
|
| 312 | #endif
|
| 313 | memset(buf, 0, sizeof(buf));
|
| 314 | // write¡¢read²âÊÔ
|
| 315 | if(write(sockfd, "send dgram data to server test four", 35) < 0){
|
| 316 | printf("write dgram data to server test four failed \n");
|
| 317 | }else{
|
| 318 | printf("write dgram data to server test four sucess \n");
|
| 319 | }
|
| 320 |
|
| 321 | #if 0
|
| 322 | if(read(sockfd, buf, sizeof(buf)-1) <= 0){
|
| 323 | printf("recvfrom: dgram data to server four failed \n");
|
| 324 | }else{
|
| 325 | printf("recvfrom: dgram data to server four sucess \n");
|
| 326 | }
|
| 327 | #endif
|
| 328 |
|
| 329 | if(pdata != NULL){
|
| 330 | if(send(sockfd, pdata, strlen(pdata), 0) < 0){
|
| 331 | printf("socket_create_client: send dgram msg error: %s(errno: %d) \n", strerror(errno), errno);
|
| 332 | return -1;
|
| 333 | }
|
| 334 | printf("send sucess: %s \n", pdata);
|
| 335 | }else{
|
| 336 | if(send(sockfd, "send dgram msg to server test", 29, 0) < 0){
|
| 337 | printf("socket_create_client: send dgram msg error: %s(errno: %d) \n", strerror(errno), errno);
|
| 338 | return -1;
|
| 339 | }
|
| 340 | printf("send msg to server test \n");
|
| 341 | }
|
| 342 |
|
| 343 | #if 0
|
| 344 | if(numRecv = recv(sockfd, buf, sizeof(buf), 0) == -1){
|
| 345 | printf("socket_create_client: recv error \n");
|
| 346 | return -1;
|
| 347 | }
|
| 348 | printf("socket_create_client Receieved: %s \n", buf);
|
| 349 | #endif
|
| 350 | if(close(sockfd) == 0){
|
| 351 | printf("clsoe sockfd:%d sucess \n", sockfd);
|
| 352 | }else{
|
| 353 | printf("clsoe sockfd:%d failed \n", sockfd);
|
| 354 | }
|
| 355 | }
|
| 356 |
|
| 357 | void socket_test_stream_data(int fd, char* paddr, char* pdata){
|
| 358 | struct sockaddr_un other_addr = {0};
|
| 359 | ssize_t numRead;
|
| 360 | ssize_t numRecv;
|
| 361 | char buf[2048] = {0};
|
| 362 | int sockfd = -1;
|
| 363 | sockfd = fd;
|
| 364 | memset(&other_addr, 0, sizeof(struct sockaddr_un));
|
| 365 | other_addr.sun_family = AF_UNIX;
|
| 366 | strncpy(other_addr.sun_path, paddr, sizeof(other_addr.sun_path)-1);
|
| 367 |
|
| 368 | //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
|
| 369 | if(other_addr.sun_path[0] == 'z' || other_addr.sun_path[0]=='Z'){
|
| 370 | printf("socket_test_stream_data: anonymous socket\n");
|
| 371 | other_addr.sun_path[0] = '\0';
|
| 372 | }
|
| 373 | //Á¬½Ó·þÎñ¶Ë
|
| 374 | if(connect(sockfd, (struct sockaddr*)&other_addr, sizeof(other_addr)) < 0){
|
| 375 | printf("socket_create_client: connect error: %s(errno: %d) \n", strerror(errno), errno);
|
| 376 | close(fd);
|
| 377 | return -1;
|
| 378 | }else{
|
| 379 | printf("³É¹¦Óë·þÎñÆ÷½¨Á¢Á¬½Ó: sockfd=%d\n", sockfd);
|
| 380 | }
|
| 381 |
|
| 382 | printf("send msg to server: \n");
|
| 383 |
|
| 384 | memset(buf, 0, sizeof(buf));
|
| 385 | // send¡¢recv²âÊÔ
|
| 386 | if(send(sockfd, "send stream data to server test one", 35, 0) < 0){
|
| 387 | printf("send stream data to server test one failed \n");
|
| 388 | }else{
|
| 389 | printf("send stream data to server test one sucess \n");
|
| 390 | }
|
| 391 |
|
| 392 | if(recv(sockfd, buf, sizeof(buf)-1, 0) <= 0){
|
| 393 | printf("recv: stream data to server one failed \n");
|
| 394 | }else{
|
| 395 | printf("recv: stream data to server one sucess \n");
|
| 396 | }
|
| 397 |
|
| 398 | memset(buf, 0, sizeof(buf));
|
| 399 | // write¡¢read²âÊÔ
|
| 400 | if(write(sockfd, "send stream data to server test two", 35) < 0){
|
| 401 | printf("write stream data to server test two failed \n");
|
| 402 | }else{
|
| 403 | printf("write stream data to server test two sucess \n");
|
| 404 | }
|
| 405 |
|
| 406 | if(read(sockfd, buf, sizeof(buf)-1) <= 0){
|
| 407 | printf("read: stream data to server two failed \n");
|
| 408 | }else{
|
| 409 | printf("read: stream data to server two sucess \n");
|
| 410 | }
|
| 411 |
|
| 412 | memset(buf, 0, sizeof(buf));
|
| 413 | if(pdata != NULL){
|
| 414 | if(send(sockfd, pdata, strlen(pdata), 0) < 0){
|
| 415 | printf("socket_create_client: send stream msg error: %s(errno: %d) \n", strerror(errno), errno);
|
| 416 | close(fd);
|
| 417 | return -1;
|
| 418 | }
|
| 419 | printf("send sucess: %s \n", pdata);
|
| 420 | }else{
|
| 421 | if(send(sockfd, "send stream msg to server test", 30, 0) < 0){
|
| 422 | printf("socket_create_client: send stream msg error: %s(errno: %d) \n", strerror(errno), errno);
|
| 423 | close(fd);
|
| 424 | return -1;
|
| 425 | }
|
| 426 | printf("send stream msg to server test \n");
|
| 427 | }
|
| 428 |
|
| 429 | if(numRecv = recv(sockfd, buf, sizeof(buf), 0) == -1){
|
| 430 | printf("socket_create_client: recv error \n");
|
| 431 | close(fd);
|
| 432 | return -1;
|
| 433 | }
|
| 434 | printf("recv sucess: %s \n", buf);
|
| 435 | if(close(sockfd) == 0){
|
| 436 | printf("clsoe sockfd:%d sucess \n", sockfd);
|
| 437 | }else{
|
| 438 | printf("clsoe sockfd:%d failed \n", sockfd);
|
| 439 | }
|
| 440 | }
|
| 441 | static int socket_create_client(char *data, int data_len)
|
| 442 | {
|
| 443 | int sockfd = -1;
|
| 444 | int type = 0;
|
| 445 | char* paddr = NULL;
|
| 446 | char* pdata = NULL;
|
| 447 | struct sockaddr_un other_addr = {0};
|
| 448 | int backlog = 0;
|
| 449 | ssize_t numRead;
|
| 450 | ssize_t numRecv;
|
| 451 | char buf[2048] = {0};
|
| 452 | int i=0;
|
| 453 | printf("socket_create_client test \n");
|
| 454 |
|
| 455 | if (at_tok_start(&data) < 0) {
|
| 456 | printf("socket_create_client:at_tok_start error\n");
|
| 457 | }
|
| 458 |
|
| 459 | if(at_tok_nextint(&data, &type) < 0) {
|
| 460 | printf("socket_create_client:at_tok_nextstr error\n");
|
| 461 | }
|
| 462 | printf("socket_create_client:type=%d\n", type);
|
| 463 |
|
| 464 | if(at_tok_nextstr(&data, &paddr) < 0) {
|
| 465 | printf("socket_create_client:at_tok_nextstr error\n");
|
| 466 | }
|
| 467 |
|
| 468 | printf("socket_create_client: addr=%s \n", paddr);
|
| 469 |
|
| 470 | if(at_tok_nextstr(&data, &pdata) < 0) {
|
| 471 | printf("socket_create_client: no test data\n");
|
| 472 | }
|
| 473 |
|
| 474 | if(strlen(paddr) > sizeof(other_addr.sun_path)-1)
|
| 475 | {
|
| 476 | printf("socket_create_client:server socket path too logn: %s\n", paddr);
|
| 477 | return -1;
|
| 478 | }
|
| 479 |
|
| 480 | if(type == 1){
|
| 481 | while(true){
|
| 482 | if((sockfd = socket(AF_UNIX, type, 0))<0){
|
| 483 | printf("socket_create_client: create socket error \n");
|
| 484 | return -1;
|
| 485 | }
|
| 486 | printf("socket_create_client: socket_create:sockfd=%d(%d´Î)\n", sockfd, i);
|
| 487 | socket_test_stream_data(sockfd, paddr, pdata);
|
| 488 | sleep(1);
|
| 489 | i++;
|
| 490 | }
|
| 491 | }else if(type ==2){
|
| 492 | while(true){
|
| 493 | if((sockfd = socket(AF_UNIX, type, 0))<0){
|
| 494 | printf("socket_create_client: create socket error \n");
|
| 495 | return -1;
|
| 496 | }
|
| 497 |
|
| 498 | printf("socket_create_client: socket_create:sockfd=%d(%d´Î)\n", sockfd, i);
|
| 499 | socket_test_dgram_data(sockfd, paddr, pdata);
|
| 500 | sleep(1);
|
| 501 | i++;
|
| 502 | }
|
| 503 | }
|
| 504 |
|
| 505 | return 0;
|
| 506 | }
|
| 507 |
|
| 508 | static void do_recv(int fd)
|
| 509 | {
|
| 510 | int ret = -1;
|
| 511 | char buf[BUF_SIZE];
|
| 512 | char resp_buf[BUF_SIZE+10];
|
| 513 | printf("do_recv loop enter, fd=%d \n", fd);
|
| 514 | while(1)
|
| 515 | {
|
| 516 | memset(buf, 0, BUF_SIZE);
|
| 517 | do
|
| 518 | {
|
| 519 | ret = read(fd,buf,BUF_SIZE-1);
|
| 520 | }while(ret < 0 && errno == EINTR);//×èÈû¶Áд
|
| 521 |
|
| 522 | if(ret < 0)
|
| 523 | {
|
| 524 | printf("fd %d read error, ret=%d!\n", fd, ret);
|
| 525 | continue;
|
| 526 | }
|
| 527 | if(ret == 0)//¶Ô·½ÒѹرÕ
|
| 528 | {
|
| 529 | printf("client is close!\n");
|
| 530 | //close(fd);
|
| 531 | continue;
|
| 532 | }
|
| 533 |
|
| 534 | if(ret > 0)
|
| 535 | {
|
| 536 | printf("read sucess, fd:%d, recv data=%s \n", fd, buf);
|
| 537 |
|
| 538 | //ret = write(fd, buf, ret);
|
| 539 |
|
| 540 | //printf("write back data=%s, len=%d \n",buf, ret);
|
| 541 | }
|
| 542 | }
|
| 543 | }
|
| 544 |
|
| 545 | static void do_select(int fd)
|
| 546 | {
|
| 547 | linklist fdlist,sun_list;
|
| 548 | fdlist = create_linklist();
|
| 549 | datatype sun_data = {0};
|
| 550 | sun_data.fd = fd;
|
| 551 | int maxfd = fd;
|
| 552 | //struct timeval tout = {5,0};
|
| 553 |
|
| 554 | insert_end_linklist(fdlist,sun_data);//½«lsten()´¦ÀíºóµÄfd¼ÓÈëÁбí
|
| 555 | //show_linklist(fdlist);
|
| 556 |
|
| 557 | fd_set fdset;
|
| 558 | int newfd = -1;
|
| 559 | int ret = -1;
|
| 560 | char buf[BUF_SIZE];
|
| 561 | char resp_buf[BUF_SIZE+10];
|
| 562 | struct sockaddr_un addr_un;
|
| 563 | socklen_t cun_addr_len = sizeof(addr_un);
|
| 564 | /* ÓÃselect()º¯ÊýʵÏÖI/O¶à·¸´ÓÃ*/
|
| 565 | while(1)
|
| 566 | {
|
| 567 | int i;
|
| 568 | FD_ZERO(&fdset);
|
| 569 | if(get_length_linklist(fdlist) >= 1)//½«ÁбíÖеÄfd¼ÓÈë¶Á¼¯ºÏ½øÐд¦Àí
|
| 570 | {
|
| 571 | for(i=0;i<get_length_linklist(fdlist);i++)
|
| 572 | {
|
| 573 | sun_list = get_list_pos_linklist(fdlist,i);
|
| 574 | sun_data = sun_list->data;
|
| 575 | FD_SET(sun_data.fd,&fdset);
|
| 576 | maxfd = sun_data.fd > maxfd ? sun_data.fd : maxfd;
|
| 577 | printf("µÚ %d ¸ö£¨fd:%d£©£¨ip:%s£©£¨port:%d£©\n",i,sun_data.fd,sun_data.ipv4_addr,sun_data.port);
|
| 578 | }
|
| 579 | //show_linklist(fdlist);
|
| 580 | }
|
| 581 | else
|
| 582 | {
|
| 583 | continue ;
|
| 584 | }
|
| 585 | //switch(select(maxfd+1,&fdset,NULL,NULL,&tout))
|
| 586 | switch(ret=select(maxfd+1,&fdset,NULL,NULL,NULL))
|
| 587 | {
|
| 588 | case 0:
|
| 589 | {
|
| 590 | printf("time out!\n");
|
| 591 | goto _error1;
|
| 592 | }
|
| 593 | case -1:
|
| 594 | {
|
| 595 | printf("select error\n");
|
| 596 | goto _error1;
|
| 597 | }
|
| 598 | default:
|
| 599 | {
|
| 600 | printf("select ret=%d \n", ret);
|
| 601 | if(FD_ISSET(fd,&fdset))//Óпͻ§¶Ë·¢ËÍÁËÁ¬½ÓÇëÇó
|
| 602 | {
|
| 603 | bzero(&addr_un, sizeof(addr_un));
|
| 604 | //if((newfd = accept(fd,NULL,0)) < 0)
|
| 605 | if((newfd = accept(fd,(struct sockaddr *)&addr_un,&cun_addr_len)) < 0)
|
| 606 | {
|
| 607 | printf("connect failed: newfd=%d\n", newfd);
|
| 608 |
|
| 609 | goto _error1;
|
| 610 | }
|
| 611 | /* ½«·ÖÅä³É¹¦µÄÌ×½Ó×ÖnewfdÉèÖóɷÇ×èÈûģʽ*/
|
| 612 | int b_on = 1;
|
| 613 | ioctl(newfd, FIONBIO, &b_on);//½«·ÖÅä³É¹¦µÄÌ×½Ó×ÖnewfdÉèÖÃΪ·Ç×èÈû·½Ê½
|
| 614 | sun_data.fd = newfd;
|
| 615 | printf("get a new client->(ip:%s)(port:%d)(fd:%d)\n",sun_data.ipv4_addr,sun_data.port,sun_data.fd);
|
| 616 |
|
| 617 | insert_end_linklist(fdlist,sun_data);//½«½¨Á¢¿Í»§¶ËÁ¬½ÓµÄfd¼ÓÈëÁбí
|
| 618 | //show_linklist(fdlist);
|
| 619 | }
|
| 620 | else//ÓÐÁ¬½ÓºÃµÄ¿Í»§¶Ë·¢ËÍÁËÊý¾Ý
|
| 621 | {
|
| 622 | for(i=0;i<get_length_linklist(fdlist);i++)//½«Á´±íÖеÄfd¶¼´¦ÀíÒ»±é
|
| 623 | {
|
| 624 |
|
| 625 | sun_list = get_list_pos_linklist(fdlist,i);
|
| 626 | sun_data = sun_list->data;
|
| 627 |
|
| 628 | //printf("readung fd is ->(µÚ %d ¸ö)(fd:%d)(ip:%s)(port:%d)\n",i,sun_data.fd,sun_data.ipv4_addr,sun_data.port);
|
| 629 | if(sun_data.fd == fd)//²»Êǽ¨Á¢Á¬½Óºó·ÖÅäµÄnewfd
|
| 630 | continue ;
|
| 631 |
|
| 632 | bzero(buf,BUF_SIZE);
|
| 633 | do
|
| 634 | {
|
| 635 | ret = read(sun_data.fd,buf,BUF_SIZE-1);
|
| 636 | }while(ret < 0 && errno == EINTR);//×èÈû¶Áд
|
| 637 |
|
| 638 | if(ret < 0)
|
| 639 | {
|
| 640 | printf("fd %d read error, ret=%d, errno=%d!\n", sun_data.fd, ret, errno);
|
| 641 | continue;
|
| 642 | }
|
| 643 | if(ret == 0)//¶Ô·½ÒѹرÕ
|
| 644 | {
|
| 645 | printf("client is close, errno=%d!\n", errno);
|
| 646 | close(sun_data.fd);
|
| 647 | delete_locate_linklist(fdlist,sun_data);
|
| 648 | continue;
|
| 649 | }
|
| 650 |
|
| 651 | printf("read sucess, fd(:%d) receive data: %s \n",sun_data.fd,buf);
|
| 652 | ret = write(sun_data.fd, buf, ret);
|
| 653 | printf("write back data=%s, len=%d \n",buf, ret);
|
| 654 | }
|
| 655 | }
|
| 656 | }
|
| 657 | }
|
| 658 | }
|
| 659 |
|
| 660 | close(newfd);
|
| 661 | _error1:
|
| 662 | printf("exit cause by _error1!\n");
|
| 663 | close(fd);
|
| 664 | clear_linklist(fdlist);
|
| 665 | }
|
| 666 |
|
| 667 | //"/tmp/us_xfr" "zmysocket"
|
| 668 | static int socket_create_server(char *data, int data_len)
|
| 669 | {
|
| 670 | int sockfd = -1;
|
| 671 | int cfd = -1;
|
| 672 | int type = 0;
|
| 673 | char* paddr = NULL;
|
| 674 | struct sockaddr_un my_addr = {0};
|
| 675 | int backlog = 0;
|
| 676 | ssize_t numRead;
|
| 677 | char buf[2048] = {0};
|
| 678 | printf("socket_create_server test \n");
|
| 679 |
|
| 680 | if (at_tok_start(&data) < 0) {
|
| 681 | printf("socket_create_server:at_tok_start error\n");
|
| 682 | }
|
| 683 |
|
| 684 | if(at_tok_nextint(&data, &type) < 0) {
|
| 685 | printf("socket_create_server:at_tok_nextstr error\n");
|
| 686 | }
|
| 687 | printf("socket_create_server:type=%d\n", type);
|
| 688 |
|
| 689 | if(at_tok_nextstr(&data, &paddr) < 0) {
|
| 690 | printf("socket_create_server:at_tok_nextstr error\n");
|
| 691 | }
|
| 692 |
|
| 693 | printf("socket_create_server: addr=%s \n", paddr);
|
| 694 |
|
| 695 | if(at_tok_nextint(&data, &backlog) < 0) {
|
| 696 | printf("socket_create_server:at_tok_nextstr error\n");
|
| 697 | }
|
| 698 |
|
| 699 | if((sockfd = socket(AF_UNIX, type, 0)) < 0)
|
| 700 | {
|
| 701 | printf("socket_create_server failed, type=%d \n", type);
|
| 702 | return -1;
|
| 703 | }
|
| 704 | g_sockfd = sockfd;
|
| 705 | printf("socket_create_server:type=%d, sockfd=%d\n", type, sockfd);
|
| 706 |
|
| 707 | /*ÔÊÐí°ó¶¨µØÖ·¿ìËÙÖØÓà */
|
| 708 | int b_reuse = 1;
|
| 709 | setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&b_reuse,sizeof(int));
|
| 710 |
|
| 711 | if(strlen(paddr) > sizeof(my_addr.sun_path)-1)
|
| 712 | {
|
| 713 | printf("socket_bind:server socket path too long: %s\n", paddr);
|
| 714 | goto error;
|
| 715 | }
|
| 716 |
|
| 717 | if(access(paddr,F_OK))
|
| 718 | {
|
| 719 | unlink(paddr);//ɾ³ý¸ÃÎļþ
|
| 720 | }
|
| 721 |
|
| 722 | memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
| 723 | my_addr.sun_family = AF_UNIX;
|
| 724 | strncpy(my_addr.sun_path, paddr, sizeof(my_addr.sun_path)-1);
|
| 725 | //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
|
| 726 | if(my_addr.sun_path[0] == 'z' || my_addr.sun_path[0]=='Z'){
|
| 727 | printf("socket_create_server: anonymous socket\n");
|
| 728 | my_addr.sun_path[0] = '\0';
|
| 729 | }
|
| 730 | if(bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
|
| 731 | printf("socket°ó¶¨³É¹¦ \n");
|
| 732 | }
|
| 733 | else{
|
| 734 | printf("socket°ó¶¨Ê§°Ü errno=%d, strerror=%s\n", errno, strerror(errno));
|
| 735 | goto error;
|
| 736 | }
|
| 737 |
|
| 738 | if(type == 1){
|
| 739 | if(listen(sockfd, backlog) == 0){
|
| 740 | printf("socket¼àÌý³É¹¦ \n");
|
| 741 | }else{
|
| 742 | printf("socket¼àÌýʧ°Ü \n");
|
| 743 | goto error;
|
| 744 | }
|
| 745 | do_select(sockfd);
|
| 746 | }else{
|
| 747 | do_recv(sockfd);
|
| 748 | }
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 | #if 0
|
| 753 |
|
| 754 | for(;;){
|
| 755 | cfd = accept(sockfd, NULL, NULL);
|
| 756 | if(cfd == -1){
|
| 757 | printf("socket_create_server: accept failed");
|
| 758 | continue;
|
| 759 | }
|
| 760 |
|
| 761 | printf("socket_create_server cfd=%d \n", cfd);
|
| 762 | while((numRead = recv(cfd, buf, sizeof(buf), 0)) > 0){
|
| 763 | printf("socket_create_server:read data=%s \n", buf);
|
| 764 | }
|
| 765 |
|
| 766 | if(numRead == -1){
|
| 767 | printf("socket_create_server: read data failed \n");
|
| 768 |
|
| 769 | }
|
| 770 |
|
| 771 | if(!fork()){
|
| 772 | if(send(cfd, "you are connected! \n", 19, 0) == -1)
|
| 773 | {
|
| 774 | printf("socket_create_server: send error \n");
|
| 775 | close(cfd);
|
| 776 | exit(0);
|
| 777 | }
|
| 778 | }
|
| 779 | if(close(cfd) == -1){
|
| 780 | printf("socket_create_server: cfd close failed\n");
|
| 781 | }
|
| 782 |
|
| 783 | }
|
| 784 | #endif
|
| 785 |
|
| 786 | error:
|
| 787 | close(sockfd);
|
| 788 | return 0;
|
| 789 |
|
| 790 | }
|
| 791 |
|
| 792 | static int socket_bind(char *data, int data_len)
|
| 793 | {
|
| 794 | int sockfd = -1;
|
| 795 | char* paddr = NULL;
|
| 796 | struct sockaddr_un my_addr = {0};
|
| 797 |
|
| 798 | if (at_tok_start(&data) < 0) {
|
| 799 | printf("socket_bind:at_tok_start error\n");
|
| 800 | }
|
| 801 |
|
| 802 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 803 | printf("socket_bind:at_tok_nextstr error\n");
|
| 804 | }
|
| 805 |
|
| 806 | if(at_tok_nextstr(&data, &paddr) < 0) {
|
| 807 | printf("socket_bind:at_tok_nextstr error\n");
|
| 808 | }
|
| 809 |
|
| 810 | printf("socket_bind: sockfd=%d, addr=%s \n", sockfd, paddr);
|
| 811 |
|
| 812 | if(strlen(paddr) > sizeof(my_addr.sun_path)-1)
|
| 813 | {
|
| 814 | printf("socket_bind:server socket path too logn: %s\n", paddr);
|
| 815 | return -1;
|
| 816 | }
|
| 817 |
|
| 818 | memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
| 819 | my_addr.sun_family = AF_UNIX;
|
| 820 | strncpy(my_addr.sun_path, paddr, sizeof(my_addr.sun_path)-1);
|
| 821 |
|
| 822 | //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
|
| 823 | if(my_addr.sun_path[0] == 'z' || my_addr.sun_path[0]=='Z'){
|
| 824 | printf("socket_bind: anonymous socket\n");
|
| 825 | my_addr.sun_path[0] = '\0';
|
| 826 | }
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 827 |
|
| 828 | if(access(paddr,F_OK))
|
| 829 | {
|
| 830 | unlink(paddr);//ɾ³ý¸ÃÎļþ
|
| 831 | }
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 832 |
|
| 833 | if(bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
|
| 834 | printf("socket°ó¶¨³É¹¦ \n");
|
| 835 | }
|
| 836 | else{
|
| 837 | printf("socket°ó¶¨Ê§°Ü errno=%d, strerror=%s\n", errno, strerror(errno));
|
| 838 | }
|
| 839 | return 0;
|
| 840 | }
|
| 841 |
|
| 842 | static int socket_listen(char *data, int data_len)
|
| 843 | {
|
| 844 | int sockfd = -1;
|
| 845 | int backlog = 0;
|
| 846 | struct sockaddr_un my_addr = {0};
|
| 847 |
|
| 848 | if (at_tok_start(&data) < 0) {
|
| 849 | printf("socket_listen:at_tok_start error\n");
|
| 850 | }
|
| 851 |
|
| 852 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 853 | printf("socket_listen:at_tok_nextint error\n");
|
| 854 | }
|
| 855 |
|
| 856 | if(at_tok_nextint(&data, &backlog) < 0) {
|
| 857 | printf("socket_listen:at_tok_nextint error\n");
|
| 858 | }
|
| 859 |
|
| 860 | printf("socket_bind: sockfd=%d, backlog=%d \n", sockfd, backlog);
|
| 861 |
|
| 862 | if(listen(sockfd, backlog) == 0){
|
| 863 | printf("socket¼àÌý³É¹¦ \n");
|
| 864 | }else{
|
| 865 | printf("socket¼àÌýʧ°Ü \n");
|
| 866 | }
|
| 867 | return 0;
|
| 868 | }
|
| 869 |
|
| 870 | static int socket_accept(char *data, int data_len)
|
| 871 | {
|
| 872 | int sockfd = -1;
|
| 873 | int newfd = -1;
|
| 874 | int ret = -1;
|
| 875 | struct sockaddr_un addr_un;
|
| 876 | socklen_t cun_addr_len = sizeof(addr_un);
|
| 877 |
|
| 878 | if (at_tok_start(&data) < 0) {
|
| 879 | printf("socket_accept:at_tok_start error\n");
|
| 880 | }
|
| 881 |
|
| 882 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 883 | printf("socket_accept:at_tok_nextint error\n");
|
| 884 | }
|
| 885 |
|
| 886 | printf("socket_accept sockfd=%d \n", sockfd);
|
| 887 |
|
| 888 | if((newfd = accept(sockfd,(struct sockaddr *)&addr_un,&cun_addr_len)) < 0)
|
| 889 | {
|
| 890 | printf("socket_accept failed: newfd=%d\n", newfd);
|
| 891 | }else{
|
| 892 | printf("socket_accept sucess: newfd=%d\n", newfd);
|
| 893 | }
|
| 894 | return 0;
|
| 895 | }
|
| 896 |
|
| 897 |
|
| 898 | static int socket_connect(char *data, int data_len)
|
| 899 | {
|
| 900 | int sockfd = -1;
|
| 901 | char* paddr = NULL;
|
| 902 | struct sockaddr_un my_addr = {0};
|
| 903 |
|
| 904 | if (at_tok_start(&data) < 0) {
|
| 905 | printf("socket_connect:at_tok_start error\n");
|
| 906 | }
|
| 907 |
|
| 908 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 909 | printf("socket_connect:at_tok_nextstr error\n");
|
| 910 | }
|
| 911 |
|
| 912 | if(at_tok_nextstr(&data, &paddr) < 0) {
|
| 913 | printf("socket_connect:at_tok_nextstr error\n");
|
| 914 | }
|
| 915 |
|
| 916 | printf("socket_connect: sockfd=%d, addr=%s \n", sockfd, paddr);
|
| 917 |
|
| 918 | if(strlen(paddr) > sizeof(my_addr.sun_path)-1)
|
| 919 | {
|
| 920 | printf("socket_connect:server socket path too logn: %s\n", paddr);
|
| 921 | return -1;
|
| 922 | }
|
| 923 |
|
| 924 | memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
| 925 | my_addr.sun_family = AF_UNIX;
|
| 926 | strncpy(my_addr.sun_path, paddr, sizeof(my_addr.sun_path)-1);
|
| 927 | printf("socket_connect: my_addr.sun_path=%s \n", my_addr.sun_path);
|
| 928 |
|
| 929 | //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
|
| 930 | if(my_addr.sun_path[0] == 'z' || my_addr.sun_path[0]=='Z'){
|
| 931 | printf("socket_connect: anonymous socket\n");
|
| 932 | my_addr.sun_path[0] = '\0';
|
| 933 | }
|
| 934 |
|
| 935 | if(connect(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
|
| 936 | printf("³É¹¦Óë·þÎñÆ÷½¨Á¢Á¬½Ó \n");
|
| 937 | }
|
| 938 | else{
|
| 939 | printf("Á¬½Óʧ°Ü\n");
|
| 940 | }
|
| 941 |
|
| 942 | return 0;
|
| 943 | }
|
| 944 |
|
| 945 | static int socket_send(char *data, int data_len)
|
| 946 | {
|
| 947 | int sockfd = -1;
|
| 948 | char* pdata = NULL;
|
| 949 | int len = 0;
|
| 950 | if (at_tok_start(&data) < 0) {
|
| 951 | printf("socket_send:at_tok_start error\n");
|
| 952 | }
|
| 953 |
|
| 954 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 955 | printf("socket_send:at_tok_nextstr error\n");
|
| 956 | }
|
| 957 |
|
| 958 | if(at_tok_nextstr(&data, &pdata) < 0) {
|
| 959 | printf("socket_send:at_tok_nextstr error\n");
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 960 | return -1;
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 961 | }
|
| 962 |
|
| 963 | printf("socket_send: sockfd=%d, pdata=%s \n", sockfd, pdata);
|
| 964 |
|
| 965 | len = send(sockfd, pdata, strlen(pdata), 0);
|
| 966 |
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 967 | if(len > 0){
|
| 968 | printf("socket_send sucess, data=%s, len=%d \n", pdata, len);
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 969 | }else{
|
| 970 | printf("socket_send failed, error: %s(errno: %d) \n", strerror(errno), errno);
|
| 971 | }
|
| 972 |
|
| 973 | return 0;
|
| 974 | }
|
| 975 |
|
| 976 | static int socket_sendto(char *data, int data_len)
|
| 977 | {
|
| 978 | int sockfd = -1;
|
| 979 | char* pdata = NULL;
|
| 980 | char* paddr = NULL;
|
| 981 | struct sockaddr_un other_addr = {0};
|
| 982 | int len = 0;
|
| 983 | if (at_tok_start(&data) < 0) {
|
| 984 | printf("socket_sendto:at_tok_start error\n");
|
| 985 | }
|
| 986 |
|
| 987 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 988 | printf("socket_sendto:at_tok_nextstr error\n");
|
| 989 | }
|
| 990 |
|
| 991 | if(at_tok_nextstr(&data, &paddr) < 0) {
|
| 992 | printf("socket_sendto:at_tok_nextstr error\n");
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 993 | return -1;
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 994 | }
|
| 995 |
|
| 996 | if(at_tok_nextstr(&data, &pdata) < 0) {
|
| 997 | printf("socket_sendto:at_tok_nextstr error\n");
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 998 | return -1;
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 999 | }
|
| 1000 |
|
| 1001 | printf("socket_sendto: sockfd=%d, addr=%s \n", sockfd, paddr);
|
| 1002 |
|
| 1003 | if(strlen(paddr) > sizeof(other_addr.sun_path)-1)
|
| 1004 | {
|
| 1005 | printf("socket_sendto:server socket path too logn: %s\n", paddr);
|
| 1006 | return -1;
|
| 1007 | }
|
| 1008 |
|
| 1009 | memset(&other_addr, 0, sizeof(struct sockaddr_un));
|
| 1010 | other_addr.sun_family = AF_UNIX;
|
| 1011 | strncpy(other_addr.sun_path, paddr, sizeof(other_addr.sun_path)-1);
|
| 1012 | printf("socket_sendto: other_addr.sun_path=%s \n", other_addr.sun_path);
|
| 1013 |
|
| 1014 | //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
|
| 1015 | if(other_addr.sun_path[0] == 'z' || other_addr.sun_path[0]=='Z'){
|
| 1016 | printf("socket_sendto: anonymous socket\n");
|
| 1017 | other_addr.sun_path[0] = '\0';
|
| 1018 | }
|
| 1019 |
|
| 1020 | len = sendto(sockfd, pdata, strlen(pdata), 0, &other_addr, sizeof(other_addr));
|
| 1021 |
|
| 1022 | if(len >= 0){
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1023 | printf("socket_sendto sucess, data=%s, len=%d \n", pdata, len);
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1024 | }else{
|
| 1025 | printf("socket_sendto failed, error: %s(errno: %d) \n", strerror(errno), errno);
|
| 1026 | }
|
| 1027 | return 0;
|
| 1028 | }
|
| 1029 |
|
| 1030 | static int socket_write(char *data, int data_len)
|
| 1031 | {
|
| 1032 | int sockfd = -1;
|
| 1033 | char* pdata = NULL;
|
| 1034 | int len = 0;
|
| 1035 | if (at_tok_start(&data) < 0) {
|
| 1036 | printf("socket_write:at_tok_start error\n");
|
| 1037 | }
|
| 1038 |
|
| 1039 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 1040 | printf("socket_write:at_tok_nextstr error\n");
|
| 1041 | }
|
| 1042 |
|
| 1043 | if(at_tok_nextstr(&data, &pdata) < 0) {
|
| 1044 | printf("socket_write:at_tok_nextstr error\n");
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1045 | return -1;
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1046 | }
|
| 1047 |
|
| 1048 | printf("socket_write: sockfd=%d, pdata=%s \n", sockfd, pdata);
|
| 1049 |
|
| 1050 | len = write(sockfd, pdata, strlen(pdata));
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1051 | if(len > 0){
|
| 1052 | printf("socket_write sucess, data=%s, len=%d \n", pdata, len);
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1053 | }else{
|
| 1054 | printf("socket_write failed, error: %s(errno: %d) \n", strerror(errno), errno);
|
| 1055 | }
|
| 1056 |
|
| 1057 | return 0;
|
| 1058 | }
|
| 1059 |
|
| 1060 | static int socket_recv(char *data, int data_len)
|
| 1061 | {
|
| 1062 | int sockfd = -1;
|
| 1063 | char pdata[2048] = {0};
|
| 1064 | int len = 0;
|
| 1065 | if (at_tok_start(&data) < 0) {
|
| 1066 | printf("socket_recv:at_tok_start error\n");
|
| 1067 | }
|
| 1068 |
|
| 1069 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 1070 | printf("socket_recv:at_tok_nextstr error\n");
|
| 1071 | }
|
| 1072 |
|
| 1073 | printf("socket_recv: sockfd=%d \n", sockfd);
|
| 1074 |
|
| 1075 | len = recv(sockfd, pdata, sizeof(pdata), 0);
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1076 | if(len > 0){
|
| 1077 | printf("recv sucess, data=%s, len=%d \n", pdata, len);
|
| 1078 | }else if(len == 0){
|
| 1079 | printf("client is close! \n");
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1080 | }else{
|
| 1081 | printf("recv failed, error: %s(errno: %d) \n", strerror(errno), errno);
|
| 1082 | }
|
| 1083 | return 0;
|
| 1084 | }
|
| 1085 |
|
| 1086 | static int socket_recvfrom(char *data, int data_len)
|
| 1087 | {
|
| 1088 | int sockfd = -1;
|
| 1089 | char pdata[2048] = {0};
|
| 1090 | struct sockaddr_un other_addr = {0};
|
| 1091 | int len = 0;
|
| 1092 | if (at_tok_start(&data) < 0) {
|
| 1093 | printf("socket_recvfrom:at_tok_start error\n");
|
| 1094 | }
|
| 1095 |
|
| 1096 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 1097 | printf("socket_recvfrom:at_tok_nextstr error\n");
|
| 1098 | }
|
| 1099 |
|
| 1100 | printf("socket_recvfrom: sockfd=%d \n", sockfd);
|
| 1101 |
|
| 1102 | len = recvfrom(sockfd, pdata, sizeof(pdata)-1, 0, (struct sockaddr*)&other_addr, sizeof(other_addr));
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1103 | if(len > 0){
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1104 | printf("recvfrom sucess: other_addr.sun_path=%s\n", other_addr.sun_path);
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1105 | printf("data=%s, len=%d\n", pdata, len);
|
| 1106 | }else if(len == 0){
|
| 1107 | printf("client is close! \n");
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1108 | }else{
|
| 1109 | printf("recvfrom failed, error: %s(errno: %d) \n", strerror(errno), errno);
|
| 1110 | }
|
| 1111 | return 0;
|
| 1112 | }
|
| 1113 |
|
| 1114 | static int socket_read(char *data, int data_len)
|
| 1115 | {
|
| 1116 | int sockfd = -1;
|
| 1117 | char pdata[2048] = {0};
|
| 1118 | int len = 0;
|
| 1119 | if (at_tok_start(&data) < 0) {
|
| 1120 | printf("socket_read:at_tok_start error\n");
|
| 1121 | }
|
| 1122 |
|
| 1123 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 1124 | printf("socket_read:at_tok_nextstr error\n");
|
| 1125 | }
|
| 1126 |
|
| 1127 | printf("socket_read: sockfd=%d \n", sockfd);
|
| 1128 |
|
| 1129 | len = read(sockfd, pdata, sizeof(pdata));
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1130 | if(len > 0){
|
| 1131 | printf("read sucess, data=%s, len=%d \n", pdata, len);
|
| 1132 | }else if(len == 0){
|
| 1133 | printf("client is close! \n");
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1134 | }else{
|
| 1135 | printf("read failed, error: %s(errno: %d) \n", strerror(errno), errno);
|
| 1136 | }
|
| 1137 | return 0;
|
| 1138 | }
|
| 1139 |
|
| 1140 | static int socket_close(char *data, int data_len)
|
| 1141 | {
|
| 1142 | int sockfd = -1;
|
| 1143 |
|
| 1144 | if (at_tok_start(&data) < 0) {
|
| 1145 | printf("socket_close:at_tok_start error\n");
|
| 1146 | }
|
| 1147 |
|
| 1148 | if(at_tok_nextint(&data, &sockfd) < 0) {
|
| 1149 | printf("socket_close:at_tok_nextint error\n");
|
| 1150 | }
|
| 1151 |
|
| 1152 | printf("socket_close: sockfd=%d \n", sockfd);
|
| 1153 |
|
| 1154 | if(close(sockfd) == 0){
|
| 1155 | printf("¹Ø±Õsocket³É¹¦ \n");
|
| 1156 | }else{
|
| 1157 | printf("¹Ø±Õsocketʧ°Ü \n");
|
| 1158 | }
|
| 1159 | return 0;
|
| 1160 | }
|
| 1161 |
|
| 1162 |
|
| 1163 | static void socket_cmd_proc(char *cmdstr)
|
| 1164 | {
|
| 1165 | char *data = cmdstr;
|
| 1166 | int data_len = strlen(data) - 1;/* -strlen("\r")*/
|
| 1167 | char request = data[0];
|
| 1168 |
|
| 1169 | cmdstr[data_len] = '\0';
|
| 1170 | printf("socket_cmd_proc data=%s \n", data);
|
| 1171 | if(0 == strncmp(data, SOCKET_CREATECLIENT, strlen(SOCKET_CREATECLIENT))) {
|
| 1172 | printf("Request create client socket.\n");
|
| 1173 | socket_create_client(data, data_len);
|
| 1174 | }else if(0 == strncmp(data, SOCKET_CREATESERVER, strlen(SOCKET_CREATESERVER))) {
|
| 1175 | printf("Request create server socket.\n");
|
| 1176 | socket_create_server(data, data_len);
|
| 1177 | }else if(0 == strncmp(data, SOCKET_CREATE, strlen(SOCKET_CREATE))) {
|
| 1178 | printf("Request create socket.\n");
|
| 1179 | socket_create(data, data_len);
|
| 1180 | }else if(0 == strncmp(data, SOCKET_BIND, strlen(SOCKET_BIND))) {
|
| 1181 | printf("Request bind socket.\n");
|
| 1182 | socket_bind(data, data_len);
|
| 1183 | }else if(0 == strncmp(data, SOCKET_LISTEN, strlen(SOCKET_LISTEN))) {
|
| 1184 | printf("Request listen socket.\n");
|
| 1185 | socket_listen(data, data_len);
|
| 1186 | }else if(0 == strncmp(data, SOCKET_ACCEPT, strlen(SOCKET_ACCEPT))) {
|
| 1187 | printf("Request accept socket.\n");
|
| 1188 | socket_accept(data, data_len);
|
| 1189 | }else if(0 == strncmp(data, SOCKET_CONN, strlen(SOCKET_CONN))) {
|
| 1190 | printf("Request connect socket.\n");
|
| 1191 | socket_connect(data, data_len);
|
| 1192 | }else if(0 == strncmp(data, SOCKET_SENDTO, strlen(SOCKET_SENDTO))) {
|
| 1193 | printf("Request sendto message.\n");
|
| 1194 | socket_sendto(data, data_len);
|
| 1195 | }else if(0 == strncmp(data, SOCKET_SEND, strlen(SOCKET_SEND))) {
|
| 1196 | printf("Request send message.\n");
|
| 1197 | socket_send(data, data_len);
|
| 1198 | }else if(0 == strncmp(data, SOCKET_WRITE, strlen(SOCKET_WRITE))) {
|
| 1199 | printf("Request write message.\n");
|
| 1200 | socket_write(data, data_len);
|
| 1201 | }else if(0 == strncmp(data, SOCKET_RECVFROM, strlen(SOCKET_RECVFROM))) {
|
| 1202 | printf("Request recvfrom message.\n");
|
| 1203 | socket_recvfrom(data, data_len);
|
| 1204 | }else if(0 == strncmp(data, SOCKET_RECV, strlen(SOCKET_RECV))) {
|
| 1205 | printf("Request recv message.\n");
|
| 1206 | socket_recv(data, data_len);
|
| 1207 | }else if(0 == strncmp(data, SOCKET_READ, strlen(SOCKET_READ))) {
|
| 1208 | printf("Request read message.\n");
|
| 1209 | socket_read(data, data_len);
|
| 1210 | }else if(0 == strncmp(data, SOCKET_CLOSE, strlen(SOCKET_CLOSE))) {
|
| 1211 | printf("Request close socket.\n");
|
| 1212 | socket_close(data, data_len);
|
| 1213 | }else if(0 == strncmp(data, SOCKET_LOCALTEST, strlen(SOCKET_LOCALTEST))){
|
| 1214 | printf("Request local test.\n");
|
| 1215 | socket_test_local();
|
| 1216 | }else {
|
| 1217 | printf("Request unknow.\n");
|
| 1218 | printUsage(cmdstr);
|
| 1219 | }
|
| 1220 | }
|
| 1221 | int main(int argc, char *argv[])
|
| 1222 | {
|
| 1223 | char cmdstr[SOCKET_CMD_MAX_LEN] = {0};
|
| 1224 |
|
| 1225 | printf("socket_ipc:Demo go.\n");
|
| 1226 | if(parseOpts(argc,argv) == -1) {
|
| 1227 | printf("socket_ipc:Arg error.\n");
|
| 1228 | return -1;
|
| 1229 | }
|
| 1230 |
|
| 1231 | while(1) {
|
| 1232 | memset(cmdstr,0,SOCKET_CMD_MAX_LEN);
|
| 1233 |
|
| 1234 | printf("Please input an socket command:\n");
|
| 1235 |
|
| 1236 | if(NULL != fgets(cmdstr,SOCKET_CMD_MAX_LEN - 1,stdin)) {
|
| 1237 | if(0 == strcmp(EXIT_CMD_STOP,cmdstr) ||
|
| 1238 | 0 == strcmp(EXIT_CMD_Q,cmdstr) ||
|
| 1239 | 0 == strcmp(EXIT_CMD_EXIT,cmdstr)) {
|
| 1240 | if(g_sockfd != -1){
|
| 1241 | if(close(g_sockfd) == 0){
|
| 1242 | printf("close g_sockfd=%d sucess \n", g_sockfd);
|
| 1243 | }else{
|
| 1244 | printf("close g_sockfd=%d failed \n", g_sockfd);
|
| 1245 | }
|
| 1246 | }
|
| 1247 | break;
|
| 1248 | }
|
| 1249 |
|
| 1250 | printf("cmdstr:%d %s\n",strlen(cmdstr),cmdstr);
|
| 1251 | if(1 >= strlen(cmdstr)) {
|
| 1252 | continue;
|
| 1253 | }
|
| 1254 |
|
| 1255 | socket_cmd_proc(cmdstr);
|
| 1256 | }
|
| 1257 | }
|
| 1258 |
|
| 1259 | printf("socket_ipc:Demo end.\n");
|
| 1260 | return 0;
|
| 1261 | }
|
| 1262 |
|