#include #include /* 使用BSD socket,需要包含socket.h头文件 */ #include #include #include #include #define BUFSZ 1024 /* 指向线程控制块的指针 */ static rt_thread_t tid1 = RT_NULL; static int sock = 0; char *recv_data; static char send_data[100] = {0}; int ret; static void tcpClient_entry(void* parameter) { int bytes_received; while (1) { /* 从sock连接中接收最大BUFSZ - 1字节数据 */ bytes_received = recv(sock, recv_data, BUFSZ - 1, 0); if (bytes_received < 0) { /* 接收失败,关闭这个连接 */ closesocket(sock); rt_kprintf("received error,close the socket. "); /* 释放接收缓冲 */ rt_free(recv_data); rt_thread_delete(tid1); } else if (bytes_received == 0) { /* 默认 recv 为阻塞模式,此时收到0认为连接出错,关闭这个连接 */ closesocket(sock); rt_kprintf("received error,close the socket. "); /* 释放接收缓冲 */ rt_free(recv_data); rt_thread_delete(tid1); } /* 有接收到数据,把末端清零 */ recv_data[bytes_received] = '\0'; /* 在控制终端显示收到的数据 */ rt_kprintf("Received data = %s ", recv_data); } } static void tcpclient_init(void) { struct hostent *host; struct sockaddr_in server_addr; const char *url; int port; url = "183.193.243.90"; //argv[1]; port = 5000; //strtoul(argv[2], 0, 10); /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = gethostbyname(url); /* 分配用于存放接收数据的缓冲 */ recv_data = rt_malloc(BUFSZ); if (recv_data == RT_NULL) { rt_kprintf("No memory"); return; } /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /* 创建socket失败 */ rt_kprintf("Socket error"); /* 释放接收缓冲 */ rt_free(recv_data); return; } /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *)host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); /* 连接到服务端 */ if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { /* 连接失败 */ rt_kprintf("Connect fail!"); closesocket(sock); /*释放接收缓冲 */ rt_free(recv_data); return; } /* 创建线程1 */ tid1 = rt_thread_create("tcpClient", tcpClient_entry, (void*)1, /* 线程入口是thread_entry, 入口参数是1 */ 1024, 11, 5); //THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE if (tid1 == RT_NULL) { rt_kprintf("tcpclient thread init fail "); /*释放接收缓冲 */ rt_free(recv_data); return; } rt_thread_startup(tid1); return; } static void login(int argc, char *argv[]) { const char *usrID; usrID = argv[1]; sprintf(send_data, "login %s ", usrID); ret = send(sock, send_data, strlen(send_data), 0); if (ret < 0) { //* 接收失败,关闭这个连接 closesocket(sock); rt_kprintf("send error,close the socket. "); rt_free(recv_data); return; } else if (ret == 0) { //* 打印send函数返回值为0的警告信息 rt_kprintf(" Send warning,send function return 0. "); } } static void say(int argc, char *argv[]) { sprintf(send_data, "say %s ", argv[1]); ret = send(sock, send_data, strlen(send_data), 0); if (ret < 0) { //* 接收失败,关闭这个连接 closesocket(sock); rt_kprintf("send error,close the socket. "); rt_free(recv_data); return; } else if (ret == 0) { //* 打印send函数返回值为0的警告信息 rt_kprintf(" Send warning,send function return 0. "); } } static void look(void) { sprintf(send_data, "look "); ret = send(sock, send_data, strlen(send_data), 0); if (ret < 0) { //* 接收失败,关闭这个连接 closesocket(sock); rt_kprintf("send error,close the socket. "); rt_free(recv_data); return; } else if (ret == 0) { //* 打印send函数返回值为0的警告信息 rt_kprintf(" Send warning,send function return 0. "); } } static void who(void) { sprintf(send_data, "who "); ret = send(sock, send_data, strlen(send_data), 0); if (ret < 0) { //* 接收失败,关闭这个连接 closesocket(sock); rt_kprintf("send error,close the socket. "); rt_free(recv_data); return; } else if (ret == 0) { //* 打印send函数返回值为0的警告信息 rt_kprintf(" Send warning,send function return 0. "); } } static void logout(void) { sprintf(send_data, "logout "); ret = send(sock, send_data, strlen(send_data), 0); if (ret < 0) { //* 接收失败,关闭这个连接 closesocket(sock); rt_kprintf("send error,close the socket. "); rt_free(recv_data); return; } else if (ret == 0) { //* 打印send函数返回值为0的警告信息 rt_kprintf(" Send warning,send function return 0. "); } /*删除线程*/ rt_thread_delete(tid1); /*关闭连接*/ closesocket(sock); /*释放接收缓冲 */ rt_free(recv_data); } MSH_CMD_EXPORT(tcpclient_init, tcpclient_init); MSH_CMD_EXPORT(login, a tcp client login); MSH_CMD_EXPORT(say, a tcp client say); MSH_CMD_EXPORT(look, a tcp client look); MSH_CMD_EXPORT(who, a tcp client who); MSH_CMD_EXPORT(logout, a tcp client logout); ``` 自己添加了C文件,然后在SConscript中添加 ![1.png](/uploads/201810/23/140236ou1u2sr5rtx5zhtk.png) 编译运行后没有发现相关的命令 ![2.png](/uploads/201810/23/140343h107ff1z0dhmv717.png) 这是为什么??
查看更多
回到 顶部
发布 问题
分享 好友
手机 浏览
投诉 建议
回到 底部