参考 vscode 免密登录服务器编辑
配置服务器信息,用vscode打开远程电脑的一个目录。
#include
#include
#include //系统函数
#include
#include //多线程库
#include "net_control_client.h"
ts_tcp_client s_tcp_client;
static int connect_to_server(void)
{
while(connect(s_tcp_client.socket_fd, (struct sockaddr *) &(s_tcp_client.server_socket_addr),
sizeof(struct sockaddr)) != 0){
//if connect error reconnect after 5 seconds
perror("connect");
printf("***reconnect after 5s***\n");
sleep(5);
printf("***reconnect...***\n");
}
printf("***connected***\n");
#if 1//test message send
sleep(1);
char test_msg[] = "This is from client";
send(s_tcp_client.socket_fd, test_msg ,sizeof(test_msg),0);
#endif
return 0;
}
/*
* 接收socket数据函数.
* client_fd - 客户端连接的socket。
*/
static int receive_packet(int client_fd)
{
unsigned char buf[TCP_BUFFER_SIZE];
int recvbytes;
while(1){
/*接收*/
bzero(buf,sizeof(buf));
recvbytes = recv(client_fd,buf,TCP_BUFFER_SIZE,0);
printf("Receive %d bytes\n",recvbytes);
if (recvbytes <= 0){
//receive error or disconnected
perror("recv");
/*reconnect to server*/
printf("close socket id = %d\n", s_tcp_client.socket_fd);
close(s_tcp_client.socket_fd); //关闭通道
s_tcp_client.socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (s_tcp_client.socket_fd == -1){
printf("###socket init error###\n");
perror("socket");
return -1;
}
printf("new socket id = %d\n", s_tcp_client.socket_fd);
if (connect_to_server() != 0){
printf("###connect_to_server error###\n");
return -1;
}
}else{
//receive success
#if 1//test
int i;
printf("***GET:\n");
for (i = 0; i < recvbytes; i++){
printf("0x%02X %c\n", *(buf+i), *(buf+i));
}
#endif
}
}
return 0;
}
/*tcp clinet send thread*/
int tcp_send_start(void)
{
char buf[100];
uint32_t i = 0;
while(1)
{
sleep(5);
memset(buf,0,sizeof(buf));
sprintf(buf,"***send data:hello world : %d\n",i);
i++;
printf("%s",buf);
send(s_tcp_client.socket_fd, buf ,strlen(buf),0);
}
}
/*tcp clinet running function*/
int tcp_client_start(void)
{
pthread_t tcp_send_thread_id;//返回的线程值
printf("***connect to %s:%d....***\n", s_tcp_client.server_ip,s_tcp_client.server_port);
/*connect to server*/
if (connect_to_server() != 0){
printf("###connect_to_server error###\n");
return -1;
}
//create thread for TCP send
pthread_create(&tcp_send_thread_id, NULL, (void *)tcp_send_start, NULL);
receive_packet(s_tcp_client.socket_fd);
return 0;
}
/*
* tcp_client initialize function
* port_num - TCP server port number
* server_ip - TCP server ip
* */
int tcp_client_init(unsigned short port_num, char *server_ip)
{
int res;
struct in_addr test_addr;
/*initialize variable*/
if (port_num > 0){
s_tcp_client.server_port = port_num;
}else{
printf("###invalid tcp server port:%d###\n", port_num);
return -1;
}
if (server_ip == NULL){
printf("###server_ip cannot be NULL###\n");
return -1;
}else{
strcpy(s_tcp_client.server_ip, server_ip);//record ip
}
printf("server ip is: %s\r\n",s_tcp_client.server_ip);
/*建立socket描述符*/
if ((s_tcp_client.socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket");
return -1;
}
printf("socket id = %d\n", s_tcp_client.socket_fd);
/*
* 填充服务器sockaddr结构
*/
bzero(&(s_tcp_client.server_socket_addr), sizeof(struct sockaddr_in)); //memset
s_tcp_client.server_socket_addr.sin_family = AF_INET;
inet_pton(AF_INET, s_tcp_client.server_ip, &(s_tcp_client.server_socket_addr.sin_addr.s_addr)); //把ip地址转化为用于网络传输的二进制数值
s_tcp_client.server_socket_addr.sin_port = htons(s_tcp_client.server_port); //将主机字节顺序转为网络字节顺序
bzero(&(s_tcp_client.server_socket_addr.sin_zero), 8);
return 0;
}
int main(int argc, char *argv[])
{
int i = 0;
int res;
pthread_t tcp_thread_id;//返回的线程值
char tcp_server_port[256];
char tcp_server_ip[256];
printf("enter main\r\n");
if (argc >= 2){
if (strcmp(argv[1],"-v") == 0){
printf("net_control_client v1.0\n");
return 0;
}else if (strcmp(argv[1],"-h") == 0){
printf("-v for version\n");
printf("-h for help\n");
printf("tcp_connect \n" );
return 0;
}else if (strcmp(argv[1], "tcp_connect") == 0){
/*tcp demo*/
strcpy(tcp_server_ip, argv[2]);
strcpy(tcp_server_port, argv[3]);
/*initialize functions*/
res = tcp_client_init((unsigned short)atoi(tcp_server_port), tcp_server_ip);//set tcp clinet setting
if (res == -1){
printf("###tcp_server_init error###\n");
return -1;
}
//create thread for TCP communication
pthread_create(&tcp_thread_id, NULL, (void *)tcp_client_start, NULL);
}else{
printf("Unknown argument %s\n",argv[1]);
return -1;
}
}
else{
printf("please input ip and port\r\n");
exit(0);
}
while(1){
if (i < 100){
i++;
}else{
i = 0;
}
sleep(1);
}
exit(0);
}
.h文件
#ifndef NET_CONTROL_CLINET_H_
#define NET_CONTROL_CLINET_H_
#include
#define TCP_BUFFER_SIZE 1024//max buff of receive buffer for tcp
typedef struct{
//tcp client class
unsigned short server_port;
char server_ip[64];
int socket_fd;//socket
struct sockaddr_in server_socket_addr;
}ts_tcp_client;
#endif /* NET_CONTROL_CLINET_H_ */
进入 'main.c’文件,然后点击菜单栏 终端->配置默认生成任务
,系统会自动建立一个task.json
文件,如下
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc 生成活动文件",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lpthread"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/gcc"
}
]
}
task.json
用来告诉vscode 怎么去编译源文件main.c
C/C++: gcc 生成活动文件
pthread
库,,那么在这里指定,这里和手动在命令行输入 gcc xxx 命令相同仍然保证 编辑区打开的是main.c
文件,然后点击菜单栏 运行->添加配置
,系统会自动建立一个launch.json
文件,如下
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["tcp_connect","192.168.1.201","8888"],//调试时传递给程序的命令行参数
"stopAtEntry": true,//是否停留在main函数
"cwd": "${fileDirname}",//调试程序时的工作目录
"environment": [],//环境变量
"externalConsole": false,//调试时是否显示控制台窗口
"MIMode": "gdb",//指定连接的调试器,可以为gdb或lldb
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc 生成活动文件",//调试开始前执行的任务,一般为编译程序 对应 tasks.json中的label
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
仍然保证 编辑区打开的是main.c
文件,
点击菜单栏终端->运行生成任务
或者 ctrl+shift+b
打开 vscode 侧边栏 选择调试标签
观察 绿色三角箭头 右侧的名称,与launch.json中
name`一致。
点击绿色箭头,或者按F5 进入调试。
在生成配置,或者调试时,一定要保证当前编辑器打开的是待调试的c文件,而不是新建的launch.json
或者tasks.json
https://code.visualstudio.com/docs/cpp/config-linux