首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当发生客户端连接时,接受TCP连接的Bash脚本将一天中的时间作为对客户端的响应发送。

当发生客户端连接时,接受TCP连接的Bash脚本将一天中的时间作为对客户端的响应发送。
EN

Stack Overflow用户
提问于 2017-10-30 08:15:17
回答 1查看 1K关注 0票数 1

我不想撒谎,这是一项家庭作业,但我一直在谷歌,试图了解如何处理这个特定的问题。

“创建一个名为lab6s6的脚本,该脚本接受TCP连接。当发生客户端连接时,发送一天中的时间作为对客户端的响应。您可以选择任何端口号作为侦听端口,并且不要忘记关闭连接。”

我正在我的virtualbox上运行最新的fedora操作系统

到目前为止,在做了一些研究之后,我遇到了一段特定的代码

$ exec {file-descriptor}<>/dev/{protocol}/{host}/{port}

我做了一些研究后想出来的是

代码语言:javascript
复制
exec 3<>/dev/TCP/127.0.0.1/8000

因此,根据我的一般理解,文件描述符总是被设置为3(这是因为stdin,stdout,stderr,这是为了什么目的?)还有表示读写的"<>“,目录是实际使用这些协议的一种方式。最后,对于我的ip,我读到的某个地方,我不应该使用这个环回,这是行不通的,但老实说,我在读这篇文章的时候有点不知所措,而对于端口,我从来没有真正理解这一点,是不是就像你的信号越多?

另外一个问题是,我是否需要安装任何其他类型的软件才能完成这样的任务?如果有人能澄清我是否基本上像我的电脑上的电话线一样开放,以便能够在我的局域网上与其他计算机通话,这可能吗?

我不是在要求直接的回答,但是如果有人能把我推向正确的方向,我会非常感激的!

再次感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-31 05:55:26

我已经为您准备了两个脚本:客户机和服务器,在授予它们执行权限之后: chmod u+x script_name,您可以按任何顺序运行它们(客户机、->服务器或服务器->客户机)。

bash_server.sh

代码语言:javascript
复制
#!/usr/bin/env bash

#define port on which the server will listen
#and the output file that will be used to store the client port to send an answer
readonly PORT_LISTEN=22222;
readonly SERVER_FILE=server_file_tmp.out;

echo "Removing the server temporary file: ${SERVER_FILE}";
rm -f "${SERVER_FILE}";
#will open/bind/listen on PORT_LISTEN and whenever some information is received 
#it will write it in the SERVER FILE
echo "Starting the server on port: ${PORT_LISTEN} with configuration file: ${SERVER_FILE}";
nc -k -l "${PORT_LISTEN}" | tee "${SERVER_FILE}" &

echo "Waiting for connection..."
#active listening to entry connection
while true; 
do
    #get always information about the external connection trying to connect to our open port
    tmpNetworkString=$(lsof -i:"${PORT_LISTEN}" | grep "localhost:${PORT_LISTEN} (ESTABLISHED)" | awk '{print $9}');
    echo -n "${tmpNetworkString}";
    if [ -s "${SERVER_FILE}" ] && [ ! -z "${tmpNetworkString}" ]; then
        answerPORT=$(cat "${SERVER_FILE}");
        echo "Connection received on port ${PORT_LISTEN}...";
        incomingIP=$(echo $tmpNetworkString | cut -d':' -f1);
        incomingPort=$(echo $tmpNetworkString | cut -d'-' -f1 | cut -d':' -f2);
        echo ">>Incoming traffic IP: ${incomingIP}";
        echo ">>Incoming traffic Port: ${incomingPort}";
        echo "Answering on IP: ${incomingIP}, port: ${answerPORT}...";
        #wait client port to be ready
        nc -z "${incomingIP}" "${answerPORT}";
        isOpen=$?;
        while [ ! "${isOpen}" -eq 0 ];
        do
            nc -z "${incomingIP}" "${answerPORT}";
            isOpen=$?;
        done
        echo $(date) | nc -q 2 "${incomingIP}" "${answerPORT}";
        echo "Closing the server, port: ${PORT_LISTEN}";
        fuser -k -n tcp "${PORT_LISTEN}";
        echo "Removing the server temporary file: ${SERVER_FILE}";
        rm -f "${SERVER_FILE}";
        exit 0;
    fi
done

bash_client.sh

代码语言:javascript
复制
#!/usr/bin/env bash

#define port on which the client will listen
#and the output file that will be used to store the answer from the server
readonly PORT_LISTEN=33333;
readonly CLIENT_FILE=client_file_tmp.out;
readonly SERVER_PORT=22222;
readonly SERVER_IP=localhost

echo "Removing the client temporary file: ${CLIENT_FILE}";
rm -f "${CLIENT_FILE}";
#will open/bind/listen on PORT_LISTEN and whenever some information is received 
#it will write it in the CLIENT FILE
echo "Starting the server on port: ${PORT_LISTEN} with configuration file: ${CLIENT_FILE}";
nc -k -l "${PORT_LISTEN}" > "${CLIENT_FILE}" &

echo "Connecting to the server: ${SERVER_IP}, on port: ${SERVER_PORT} and waiting for answer";
#sending port information for answer:

#wait client port to be ready
nc -z "${SERVER_IP}" "${SERVER_PORT}";
isOpen=$?;
while [ ! "${isOpen}" -eq 0 ];
do
    nc -z "${SERVER_IP}" "${SERVER_PORT}";
    isOpen=$?;
done
echo "${PORT_LISTEN}" | nc -q 2 "${SERVER_IP}" "${SERVER_PORT}";

while true; 
do
    if [ -s "${CLIENT_FILE}" ]; then
        echo "Answer received from server...";
        echo "##############################";
        echo "##############################";
        cat "${CLIENT_FILE}";
        echo "##############################";
        echo "##############################";
        #sleep 10;
        echo "Closing the open port of the client, port: ${PORT_LISTEN}";
        fuser -k -n tcp "${PORT_LISTEN}";
        echo "Removing the answer file: ${CLIENT_FILE}";
        rm -f "${CLIENT_FILE}";
        exit 0;
    fi
done
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47010630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档