Unix网络编程 Ubuntu20.04.2 Visual Studio Code

Visual Studio Code
说明
1.本文中“→”表示下一步、下一级菜单和修改为,需根据上下文理解。
一、环境配置
1.安装gcc、g++和gdb

sudo apt-get update

sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install gdb

2.安装插件
Extensions→C/C++
Extensions→Code Runner

如果Extensions未显示,则
View→Extensions
Extensions→Mouse Right→Keep ‘Extensions’
3.Configuration
launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "compile",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

4.Settings Run In Terminal
File→Preferences→Settings→Extensions→Run Code Configure→Run In Terminal 【√】Whether to run code in Integrated Terminal
在这里插入图片描述

二、使用问题
1.调试参数
launch.json

"args": [“127.0.0.1”],

2.为输出文件指定后缀名
launch.json

"program": "${fileDirname}/${fileBasenameNoExtension}.out",

在这里插入图片描述

tasks.json

"args": [
	"-g",
	"${file}",
	"-o",
	"${fileDirname}/${fileBasenameNoExtension}.out"
],

3.Files Filter Exclude
File→Preferences→Setting→User→Text Editor→Files→Files:Exclude→Add Pattern→**/*.out
在这里插入图片描述

Unix网络编程daytimetcpcli调试
一、unpv13e编译
1.源码
http://www.unpbook.com
2.编译

./configure    # try to figure out all implementation differences

    cd lib         # build the basic library that all programs need
    make           # use "gmake" everywhere on BSD/OS systems

    cd ../libfree  # continue building the basic library
    make

    cd ../libroute # only if your system supports 4.4BSD style routing sockets
    make           # only if your system supports 4.4BSD style routing sockets

    cd ../libxti   # only if your system supports XTI
    make           # only if your system supports XTI

    cd ../intro    # build and test a basic client program
make daytimetcpcli

./daytimetcpcli 127.0.0.1

二、apue.h编译
1.源码
http://www.apuebook.com
2.编译

cd apue.3e
make

sudo cp ./include/apue.h /usr/include
sudo cp ./lib/libapue.a /usr/lib
sudo cp ./lib/error.c /usr/include

三、xinetd服务
1.安装xinetd

sudo apt-get install xinetd

cd /etc/xinetd.d/
sudo vim daytime
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: 

键入E

# This is the tcp version.
service daytime
{
        disable         = no
        type            = INTERNAL
        id              = daytime-stream
        socket_type     = stream
        protocol        = tcp
        user            = root
        wait            = no
}

# This is the udp version.
service daytime
{
        disable         = no
        type            = INTERNAL
        id              = daytime-dgram
        socket_type     = dgram
        protocol        = udp
        user            = root
        wait            = yes
}
"daytime" 26L, 500C                                           26,1          Bot

键入i开始编辑

service daytime{
	disable=yes->disable=no 		//共有两处,如上
}

修改结束后输入ESC,继续键入

:wq			//保存退出

:为手动输入,以退出daytime的编辑

开启xinetd服务

sudo servise xinetd start

四、在Visual Studio Code中调试
1.链接libunp.a库
在tasks.json中修改"args"

"args": [
	"-g",
	"${file}",
	"-o",
	"${fileDirname}/${fileBasenameNoExtension}.out",
	"-lunp"
],

对应的控制台命令为

gcc -o daytimetcpcli.out daytimetcpcli.c -lunp

在这里插入图片描述
2.本地主题测试参数设置
在launch.json中配置参数:

launch.json
ipv4

"args": [“127.0.0.1”],

ipv6

"args": [“::1”],
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值