博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using BASH for network socket operation
阅读量:4029 次
发布时间:2019-05-24

本文共 1199 字,大约阅读时间需要 3 分钟。

原文链接:

Using BASH for network socket operation

In order to solve a programming assignment (send a “TEST” to an UDP service and readout the result [beeing "success"]) i looked into socket programming with the BASH command shell.

Here is the code:

# configurationHOST="127.0.0.1"PORT="1337"# define functionssocksend (){	SENDME="$1"	echo "sending: $SENDME"	echo -ne "$SENDME" >&5 &}sockread (){	LENGTH="$1"	RETURN=`dd bs=$1 count=1 <&5 2> /dev/null`}echo "trying to open socket"# try to connectif ! exec 5<> /dev/udp/$HOST/$PORT; then  echo "`basename $0`: unable to connect to $HOST:$PORT"  exit 1fiecho "socket is open"# send requestsocksend "TEST"# read 7 bytes for "success"sockread 7echo "RETURN: $RETURN"

The procedure is fairly easy:

- create a filedescriptor (using number 5 because 0,1,2 are for system stdin/stdout/stderr, so i am on the safe side) and link it to the special device /dev/udp (/dev/tcp for tcp connections).
- read/write to the fd

Attention: your bash must be compiled with this feature, otherwise there wont be any /dev/udp or /dev/tcp devices.

My post should answer the question posted here:

Also, i stumbled across those pages which where quite helpful for getting started:

转载地址:http://ilmbi.baihongyu.com/

你可能感兴趣的文章
谷歌走了
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
CLOSE_WAIT和TIME_WAIT
查看>>
在C++中使用Lua
查看>>
在Dll中调用自身的位图资源
查看>>
IP校验和详解
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
C++获取文件大小常用技巧分享
查看>>
未来5年大机遇:做贩卖多巴胺的超级玩家
查看>>