客户端
请问socket客户端与虚拟机服务器是如何进行通信的?有哪些步骤?如何对其进行测试?
回帖(1)
2021-12-28 15:44:37
- 2、创建socket连接服务器。
3、创建终端接收数据线程。
wifi_connect.c
- #include
#include
#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "kernel/os/os.h"
int wifi_device_connect(const char *ssid, const char *psk_key)
{
const char *ssid_want_connect = ssid;
const char *psk = psk_key;
printf("rn=========== Connect Test Start ===========rn");
if (WIFI_SUCCESS != EnableWifi()) {
printf("Error: EnableWifi fail.rn");
return -1;
}
printf("EnableWifi Success.rn");
if (WIFI_STA_ACTIVE == IsWifiActive())
printf("Wifi is active.rn");
OS_Sleep(1);
if (WIFI_SUCCESS != Scan()) {
printf("Error: Scan fail.rn");
return -1;
}
printf("Wifi Scan Success.rn");
OS_Sleep(1);
WifiScanInfo scan_results[30];
unsigned int scan_num = 30;
if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) {
printf("Error: GetScanInfoList fail.rn");
return -1;
}
WifiDeviceConfig config = { 0 };
int netId = 0;
int i;
for (i = 0; i < scan_num; i++) {
if (0 == strcmp(scan_results.ssid, ssid_want_connect)) {
memcpy(config.ssid, scan_results.ssid,
WIFI_MAX_SSID_LEN);
memcpy(config.bssid, scan_results.bssid,
WIFI_MAC_LEN);
strcpy(config.preSharedKey, psk);
config.securityType = scan_results.securityType;
config.wapiPskType = WIFI_PSK_TYPE_ASCII;
config.freq = scan_results.frequency;
break;
}
}
if (i >= scan_num) {
printf("Error: No found ssid in scan_resultsrn");
return -1;
}
printf("GetScanInfoList Success.rn");
if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) {
printf("Error: AddDeviceConfig Failrn");
return -1;
}
printf("AddDeviceConfig Success.rn");
if (WIFI_SUCCESS != ConnectTo(netId)) {
printf("Error: ConnectTo Failrn");
return -1;
}
printf("ConnectTo Successrn");
OS_Sleep(3);
WifiLinkedInfo get_linked_res;
if (WIFI_SUCCESS != GetLinkedInfo(&get_linked_res)) {
printf("Error: GetLinkedInfo Failrn");
return -1;
}
printf("GetLinkedInfo Success.rn");
printf("ssid: %srn", get_linked_res.ssid);
printf("bssid: ");
for (int j = 0; j < WIFI_MAC_LEN; j++) {
printf("%02X", get_linked_res.bssid[j]);
}
printf("rn");
printf("rssi: %drn", get_linked_res.rssi);
unsigned char get_mac_res[WIFI_MAC_LEN];
if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
printf("Error: GetDeviceMacAddress Failrn");
return -1;
}
printf("GetDeviceMacAddress Success.rn");
for (int j = 0; j < WIFI_MAC_LEN - 1; j++) {
printf("%02X:", get_mac_res[j]);
}
printf("%02Xrn", get_mac_res[WIFI_MAC_LEN - 1]);
return 0;
}
tcp_socket.c
- 创建发送(send_task)和接收线程(read_task)。
- #include
#include
#include
#include "lwip/sockets.h"
#include "ohos_init.h"
#include "kernel/os/os.h"
int _PROT_ = 6666;
char _TCP_SERVER_IP[] = "192.168.1.109";
int sock;
int ret;
struct sockaddr_in server_addr;
char send_buffer[50] = "Hello RX806 Open_Harmany!rn";
char recv_buffer[50];
extern OS_Semaphore_t tcp_wd;
static OS_Thread_t send_thread;
static OS_Thread_t read_thread;
void send_task(void *argv){
while(1){
if(OS_SemaphoreWait(&tcp_wd,OS_WAIT_FOREVER)==OS_OK)
{
printf("OS_SemaphoreWaitrn");
if((ret = send(sock,send_buffer,strlen(send_buffer),0)) == -1){
printf("send_errorrn");
}
bzero(&send_buffer,sizeof(send_buffer));
}
OS_MSleep(50);
}
}
void read_task(void *argv){
while(1){
if((ret = recv(sock,recv_buffer,sizeof(recv_buffer),0)) != -1){
printf("recv:rn%s,ret=%drn",recv_buffer,ret);
bzero(&recv_buffer,sizeof(recv_buffer));
}else{
printf("recv_errorrn");
}
OS_MSleep(1000);
}
}
int tcp_demo_test(){
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket_errorrn");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(_PROT_);
server_addr.sin_addr.s_addr = inet_addr(_TCP_SERVER_IP);
if(connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
printf("connect_errorrn");
return -1;
}
printf("connect_server_successrn");
if (OS_ThreadCreate(&send_thread, "send_task", send_task, NULL,
6, 2 * 1024) != OS_OK) {
printf("[ERR] send_task Failedn");
}
printf(" send_task n");
if (OS_ThreadCreate(&read_thread, "read_task", read_task, NULL,
6, 2 * 1024) != OS_OK) {
printf("[ERR] read_task Failedn");
}
printf(" read_task n");
}
- main.c
- void consol_task(void *argv){
int ret;
char uart_buffer[UART_RECEIVE_DATALEN];
IoTFlashInit();
while(1){
/*读取串口接收到的数据*/
ret = IoTUartRead(0, uart_buffer, UART_RECEIVE_DATALEN);
if(ret!=0){
/*指令*/
if(strncmp(uart_buffer,"IP:",3)==0)
{
// if(uart_buffer[3] == '?')
// {
// }else{
// printf("IP:%srn",uart_buffer+3);
// }
}
/*从服务器获得当前时间*/
else if(strncmp(uart_buffer,"time:",5)==0)
{
printf("time:%srn",uart_buffer+5);
memcpy(send_buffer,uart_buffer,strlen(uart_buffer));
if(OS_SemaphoreRelease(&tcp_wd)!=OS_OK){
printf("OS_SemaphoreRelease_errorrn");
}
}
/*发送数据到服务器*/
else if(strncmp(uart_buffer,"send:",5)==0)
{
printf("send:%srn",uart_buffer+5);
memcpy(send_buffer,uart_buffer+5,strlen(uart_buffer)-5);
if(OS_SemaphoreRelease(&tcp_wd)!=OS_OK){
printf("OS_SemaphoreRelease_errorrn");
}
}
/* wifi SSID以及PSK重置 并复位连接
指令格式: wifi:@:@SSID@:@PSK
*/
else if(strncmp(uart_buffer,"wifi:",5)==0){
if(uart_buffer[5] == '?'){//wifi:? 回复当前
printf("connect_wifi_ssid:%s,connect_wifi_psk:%srn",connect_wifi_ssid,connect_wifi_psk);
}else{
bzero(connect_wifi_psk,sizeof(connect_wifi_psk));
bzero(connect_wifi_ssid,sizeof(connect_wifi_ssid));
printf("DisableWifi Successn");
Find_string((char*)uart_buffer,"@:@","@:@",connect_wifi_ssid);
strcpy(connect_wifi_psk,strstr(strstr(uart_buffer, "@:@")+3,"@:@")+3);
printf("reconnect_wifi_ssid:%s,connect_wifi_psk:%srn",connect_wifi_ssid,connect_wifi_psk);
if(OS_SemaphoreRelease(&wifi_reconnect)!=OS_OK){
printf("OS_SemaphoreRelease_wifi_reconnect_errorrn");
}
/*wifi wifi SSID以及PSK存储到ROM*/
IotFlash_write(0x100000 ,connect_wifi_ssid,strlen(connect_wifi_ssid)+1);
IotFlash_write(0x100000 + 0x4000,connect_wifi_psk,strlen(connect_wifi_psk)+1);
/*重启*/
RebootDevice(0);
printf("reboot_reset_wifirn");
}
}else{
printf("command error,please again! rn%sn",uart_buffer);
}
bzero(uart_buffer,sizeof(uart_buffer));
}
OS_MSleep(10);
}
}
- 注意:如果自己定义使用UART0接收数据时,需要注释原有的终端接收#"adapter/console:app_console",
效果:
TCP数据通信:
串口助手发送:time:? 接收获取当前的时间:now datetime: 2021-12-24 23:19:55,ret=33
串口助手发送:send:hello_XR806_OPEHARMANY! 服务端接收到:hello_XR806_OPEHARMANY!
服务端发送:hello world! 客户端接收:hello world!,ret=12
wifi的SSID和PSK获取以及重启
发送wifi:? 获取此时连接wifi的SSID和PSK
发送wifi:@:@hello@:@89898989 连接SSID为hello的新wifi
以上具体代码以及服务端代码已经上传到 gitee
补充:
1、每个工程下都有对应的BUILD.gn文件(业务),用来告诉编译器工程需要用到的头文件的路径;同样的,该工程外也有一个BUILD.gn文件(模块),用来告诉编译器你要编译哪个工程;该目录外还有一个BUILD.gn文件(模块),作用也是大同小异。
2、每个人工程下static_library的命名必须以app_的前缀命名。
- 出现的问题:
1、hb build -f 没有反应
需要安装对应的工具,执行 python3 -m pip install --user ohos-build (首先你得有python3的环境)
- 2、使用git时,出现以下错误
错误:服务器证书验证失败。CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none·
说明证书有问题。执行export GIT_SSL_NO_VERIFY=1 可以解决
- 3、烧写程序一直失败。可以尝试设置,勾上红色花圈部分。
4、当XR806开始连接连接wifi时,尝试断开wifi,(不是断电重启)其他wifi,此时开始重新设置SSID和PSK并初始化wifi,但一直不上,我的呼吸呼吸到底层SSID和PSK值是恒定的,无法被修改,我一股往深处追。为了实现这个功能,我利用了内部ROM空间,将SSID和PSK存入到ROM中掉电不丢失),再软件复位,通过串口输入SSID和PSK(成功连接到对应wifi)。
- 2、创建socket连接服务器。
3、创建终端接收数据线程。
wifi_connect.c
- #include
#include
#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "kernel/os/os.h"
int wifi_device_connect(const char *ssid, const char *psk_key)
{
const char *ssid_want_connect = ssid;
const char *psk = psk_key;
printf("rn=========== Connect Test Start ===========rn");
if (WIFI_SUCCESS != EnableWifi()) {
printf("Error: EnableWifi fail.rn");
return -1;
}
printf("EnableWifi Success.rn");
if (WIFI_STA_ACTIVE == IsWifiActive())
printf("Wifi is active.rn");
OS_Sleep(1);
if (WIFI_SUCCESS != Scan()) {
printf("Error: Scan fail.rn");
return -1;
}
printf("Wifi Scan Success.rn");
OS_Sleep(1);
WifiScanInfo scan_results[30];
unsigned int scan_num = 30;
if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) {
printf("Error: GetScanInfoList fail.rn");
return -1;
}
WifiDeviceConfig config = { 0 };
int netId = 0;
int i;
for (i = 0; i < scan_num; i++) {
if (0 == strcmp(scan_results.ssid, ssid_want_connect)) {
memcpy(config.ssid, scan_results.ssid,
WIFI_MAX_SSID_LEN);
memcpy(config.bssid, scan_results.bssid,
WIFI_MAC_LEN);
strcpy(config.preSharedKey, psk);
config.securityType = scan_results.securityType;
config.wapiPskType = WIFI_PSK_TYPE_ASCII;
config.freq = scan_results.frequency;
break;
}
}
if (i >= scan_num) {
printf("Error: No found ssid in scan_resultsrn");
return -1;
}
printf("GetScanInfoList Success.rn");
if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) {
printf("Error: AddDeviceConfig Failrn");
return -1;
}
printf("AddDeviceConfig Success.rn");
if (WIFI_SUCCESS != ConnectTo(netId)) {
printf("Error: ConnectTo Failrn");
return -1;
}
printf("ConnectTo Successrn");
OS_Sleep(3);
WifiLinkedInfo get_linked_res;
if (WIFI_SUCCESS != GetLinkedInfo(&get_linked_res)) {
printf("Error: GetLinkedInfo Failrn");
return -1;
}
printf("GetLinkedInfo Success.rn");
printf("ssid: %srn", get_linked_res.ssid);
printf("bssid: ");
for (int j = 0; j < WIFI_MAC_LEN; j++) {
printf("%02X", get_linked_res.bssid[j]);
}
printf("rn");
printf("rssi: %drn", get_linked_res.rssi);
unsigned char get_mac_res[WIFI_MAC_LEN];
if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
printf("Error: GetDeviceMacAddress Failrn");
return -1;
}
printf("GetDeviceMacAddress Success.rn");
for (int j = 0; j < WIFI_MAC_LEN - 1; j++) {
printf("%02X:", get_mac_res[j]);
}
printf("%02Xrn", get_mac_res[WIFI_MAC_LEN - 1]);
return 0;
}
tcp_socket.c
- 创建发送(send_task)和接收线程(read_task)。
- #include
#include
#include
#include "lwip/sockets.h"
#include "ohos_init.h"
#include "kernel/os/os.h"
int _PROT_ = 6666;
char _TCP_SERVER_IP[] = "192.168.1.109";
int sock;
int ret;
struct sockaddr_in server_addr;
char send_buffer[50] = "Hello RX806 Open_Harmany!rn";
char recv_buffer[50];
extern OS_Semaphore_t tcp_wd;
static OS_Thread_t send_thread;
static OS_Thread_t read_thread;
void send_task(void *argv){
while(1){
if(OS_SemaphoreWait(&tcp_wd,OS_WAIT_FOREVER)==OS_OK)
{
printf("OS_SemaphoreWaitrn");
if((ret = send(sock,send_buffer,strlen(send_buffer),0)) == -1){
printf("send_errorrn");
}
bzero(&send_buffer,sizeof(send_buffer));
}
OS_MSleep(50);
}
}
void read_task(void *argv){
while(1){
if((ret = recv(sock,recv_buffer,sizeof(recv_buffer),0)) != -1){
printf("recv:rn%s,ret=%drn",recv_buffer,ret);
bzero(&recv_buffer,sizeof(recv_buffer));
}else{
printf("recv_errorrn");
}
OS_MSleep(1000);
}
}
int tcp_demo_test(){
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket_errorrn");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(_PROT_);
server_addr.sin_addr.s_addr = inet_addr(_TCP_SERVER_IP);
if(connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
printf("connect_errorrn");
return -1;
}
printf("connect_server_successrn");
if (OS_ThreadCreate(&send_thread, "send_task", send_task, NULL,
6, 2 * 1024) != OS_OK) {
printf("[ERR] send_task Failedn");
}
printf(" send_task n");
if (OS_ThreadCreate(&read_thread, "read_task", read_task, NULL,
6, 2 * 1024) != OS_OK) {
printf("[ERR] read_task Failedn");
}
printf(" read_task n");
}
- main.c
- void consol_task(void *argv){
int ret;
char uart_buffer[UART_RECEIVE_DATALEN];
IoTFlashInit();
while(1){
/*读取串口接收到的数据*/
ret = IoTUartRead(0, uart_buffer, UART_RECEIVE_DATALEN);
if(ret!=0){
/*指令*/
if(strncmp(uart_buffer,"IP:",3)==0)
{
// if(uart_buffer[3] == '?')
// {
// }else{
// printf("IP:%srn",uart_buffer+3);
// }
}
/*从服务器获得当前时间*/
else if(strncmp(uart_buffer,"time:",5)==0)
{
printf("time:%srn",uart_buffer+5);
memcpy(send_buffer,uart_buffer,strlen(uart_buffer));
if(OS_SemaphoreRelease(&tcp_wd)!=OS_OK){
printf("OS_SemaphoreRelease_errorrn");
}
}
/*发送数据到服务器*/
else if(strncmp(uart_buffer,"send:",5)==0)
{
printf("send:%srn",uart_buffer+5);
memcpy(send_buffer,uart_buffer+5,strlen(uart_buffer)-5);
if(OS_SemaphoreRelease(&tcp_wd)!=OS_OK){
printf("OS_SemaphoreRelease_errorrn");
}
}
/* wifi SSID以及PSK重置 并复位连接
指令格式: wifi:@:@SSID@:@PSK
*/
else if(strncmp(uart_buffer,"wifi:",5)==0){
if(uart_buffer[5] == '?'){//wifi:? 回复当前
printf("connect_wifi_ssid:%s,connect_wifi_psk:%srn",connect_wifi_ssid,connect_wifi_psk);
}else{
bzero(connect_wifi_psk,sizeof(connect_wifi_psk));
bzero(connect_wifi_ssid,sizeof(connect_wifi_ssid));
printf("DisableWifi Successn");
Find_string((char*)uart_buffer,"@:@","@:@",connect_wifi_ssid);
strcpy(connect_wifi_psk,strstr(strstr(uart_buffer, "@:@")+3,"@:@")+3);
printf("reconnect_wifi_ssid:%s,connect_wifi_psk:%srn",connect_wifi_ssid,connect_wifi_psk);
if(OS_SemaphoreRelease(&wifi_reconnect)!=OS_OK){
printf("OS_SemaphoreRelease_wifi_reconnect_errorrn");
}
/*wifi wifi SSID以及PSK存储到ROM*/
IotFlash_write(0x100000 ,connect_wifi_ssid,strlen(connect_wifi_ssid)+1);
IotFlash_write(0x100000 + 0x4000,connect_wifi_psk,strlen(connect_wifi_psk)+1);
/*重启*/
RebootDevice(0);
printf("reboot_reset_wifirn");
}
}else{
printf("command error,please again! rn%sn",uart_buffer);
}
bzero(uart_buffer,sizeof(uart_buffer));
}
OS_MSleep(10);
}
}
- 注意:如果自己定义使用UART0接收数据时,需要注释原有的终端接收#"adapter/console:app_console",
效果:
TCP数据通信:
串口助手发送:time:? 接收获取当前的时间:now datetime: 2021-12-24 23:19:55,ret=33
串口助手发送:send:hello_XR806_OPEHARMANY! 服务端接收到:hello_XR806_OPEHARMANY!
服务端发送:hello world! 客户端接收:hello world!,ret=12
wifi的SSID和PSK获取以及重启
发送wifi:? 获取此时连接wifi的SSID和PSK
发送wifi:@:@hello@:@89898989 连接SSID为hello的新wifi
以上具体代码以及服务端代码已经上传到 gitee
补充:
1、每个工程下都有对应的BUILD.gn文件(业务),用来告诉编译器工程需要用到的头文件的路径;同样的,该工程外也有一个BUILD.gn文件(模块),用来告诉编译器你要编译哪个工程;该目录外还有一个BUILD.gn文件(模块),作用也是大同小异。
2、每个人工程下static_library的命名必须以app_的前缀命名。
- 出现的问题:
1、hb build -f 没有反应
需要安装对应的工具,执行 python3 -m pip install --user ohos-build (首先你得有python3的环境)
- 2、使用git时,出现以下错误
错误:服务器证书验证失败。CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none·
说明证书有问题。执行export GIT_SSL_NO_VERIFY=1 可以解决
- 3、烧写程序一直失败。可以尝试设置,勾上红色花圈部分。
4、当XR806开始连接连接wifi时,尝试断开wifi,(不是断电重启)其他wifi,此时开始重新设置SSID和PSK并初始化wifi,但一直不上,我的呼吸呼吸到底层SSID和PSK值是恒定的,无法被修改,我一股往深处追。为了实现这个功能,我利用了内部ROM空间,将SSID和PSK存入到ROM中掉电不丢失),再软件复位,通过串口输入SSID和PSK(成功连接到对应wifi)。
举报
更多回帖