我如何实现epoll超时?(How do I implement epoll timeout?)
我正在使用epoll进行网络编程。 这似乎工作正常。 我想添加一个超时函数,所以如果客户端长时间没有发送任何东西,那么它只会断开客户端。
我该怎么做呢? 我知道我可以在epoll_wait中设置超时时间,但这不适用于单个套接字...
提前致谢...
I am working on a network programming using epoll. It seems like working fine. I would like to add a timeout function so if a client doesn't sent anything for a long period time then it will just disconnect the client.
How do I do this? I know that I can set the timeout in epoll_wait but this is not for the individual socket...
Thanks in advance...
原文:https://stackoverflow.com/questions/6590531
最满意答案
您期待的解决方案如下所示。 但是您的输出与输入文件不匹配
awk 'FNR==NR{ hashKey[$2]=$1; next }$1 in hashKey{$1=hashKey[$1]}1' FS='\t' file1 file2
我们的想法是在第一个文件的第二列中散列值,这是以制表符分隔的。 然后,在第一列中存在于哈希表中的那些值的第二个值上,我们更新存储的哈希值。
The solution you are looking forward to is something like below. But your output does not match your input file
awk 'FNR==NR{ hashKey[$2]=$1; next }$1 in hashKey{$1=hashKey[$1]}1' FS='\t' file1 file2
The idea is we hash the values in the second column of the first file which is tab-separated. Then on the second values for those values in first column which are present in the hash table, we update the value from the stored hash.
相关问答
更多-
在awk中,如何使用包含printf的多个格式字符串的文件?(In awk, how can I use a file containing multiple format strings with printf?)[2024-01-02]
为什么这么冗长复杂的例子? 这表明了问题: $ echo "" | awk '{s="a\t%s"; printf s"\n","b"}' a b $ echo "a\t%s" | awk '{s=$0; printf s"\n","b"}' a\tb 在第一种情况下,字符串“a \ t%s”是一个字符串文字,因此被解释两次 - 一次当awk读取脚本,然后再执行时,再次在第一次传递时扩展\t然后在执行时,awk在格式化字符串中有一个字面tab字符。 在第二种情况下,awk在格式化字符串中仍然 ... -
一行awk解决方案可能是: awk -F'\t' 'FNR==NR{ind[$2]=$1;next} { if($1 in ind) { l=4*ind[$1]} else {l=l+1}; text[l]=text[l]"\t"$1 } END { for (i = 0; i < length(text); i++) {print substr(text[i],2)} }' index.txt source.txt 说明: -F'\t' 这是使用tab作为分隔符 FNR==NR 处理文件后的文件 ...
-
有任何awk: $ awk 'BEGIN{FS=OFS="\t"} {for (i=2;i<=NF;i++) if ($i~/:FAIL$/) sub(/:[^:]+/,":-1",$i)} 1' file GT:CN:CNL:CNP:CNQ:FT .:2:a:b:c:PASS .:2:c:b:a:PASS .:-1:d:c:a:FAIL With any awk: $ awk 'BEGIN{FS=OFS="\t"} {for (i=2;i<=NF;i++) if ($i~/:FAIL$/) s ...
-
由于您的字段分隔符是逗号,“空”字段可能包含空格,尤其是第二个字段。 因此它们可能不等于空字符串。 我会这样做: awk -F, -v OFS=, ' # ex NR>2 && !/^\([0-9]+ rows\)/ { for (i=2; i<=NF; i++) $i = ($i ~ /[^[:blank:]]/) ? "true" : "false" } { print } ' file Since your field sep ...
-
你的awk命令试图在同一行找到line1和line2 。 在不同行上搜索2个字符串的正确方法是: sed '/^[[:blank:]]*$/d' | awk -v RS= '/line1[^\n]*\n.*line2/ || /line2[^\n]*\n.*line1/{print "Ok"}' sed将从输入中删除所有空行 -v RS=将输入记录分隔符设置为NULL,从而忽略新行作为记录分隔符。 /line1[^\n]*\n.*line2/ || /line2[^\n]*\n.*line1/ /line ...
-
在AWK函数sub替换一个/第一个实例。 如果用gsub替换gsub应该替换所有实例。 因此,这应该工作正常: sig=2.6 awk -v sig="$sig" '{ gsub(/XSIGX/, sig); print; }' input.txt > output.txt In AWK function sub replaces one/first instance. If you replace sub with gsub should replace all i ...
-
使用Awk用一个来自其他文件的字符串替换一个文件中的字符串(Using Awk to replace strings in one file with strings from other file)[2022-12-19]
您期待的解决方案如下所示。 但是您的输出与输入文件不匹配 awk 'FNR==NR{ hashKey[$2]=$1; next }$1 in hashKey{$1=hashKey[$1]}1' FS='\t' file1 file2 我们的想法是在第一个文件的第二列中散列值,这是以制表符分隔的。 然后,在第一列中存在于哈希表中的那些值的第二个值上,我们更新存储的哈希值。 The solution you are looking forward to is something like below. But ... -
是的,有一种简单的方法可以在awk中执行此操作。 awk 'FNR==NR{a[$0];next}($0 in a)' file_1 file_2 其中file_1是您的字符串文件, file_2是您的搜索文件。 Yes, there is an easy way of doing this in awk. awk 'FNR==NR{a[$0];next}($0 in a)' file_1 file_2 where file_1 is your string file and file_2 is you ...
-
$ awk 'sub(/!\[Figure/,"& "i+1){i++} 1' file Foo Figure ![Figure 1. Foo](images/foo.png) Bar Figure ![Figure 2. "Bar"](images/bar.png) $ awk 'sub(/!\[Figure/,"& "i+1){i++} 1' file Foo Figure ![Figure 1. Foo](images/foo.png) Bar Figure ![Figure 2. "Bar"] ...
-
如何使用awk从文本文件中打印多个字符串的总数(How to print total count of multiples strings from a text file using awk)[2022-01-18]
你错过了'for'循环的大括号 - 你应该有: awk 'BEGIN {print "They" " " "He" " " "She"} #printing header {for (i=0;i<=NF;i++) { if ( $i =="They" ) numA++; if ( $i =="He" ) numB++; if ( $i =="She" ) numC++; } } END {p ...