Server program responding to both TCP and UDP requests?

agolangf · · 437 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m currently working on an assignment for a Golang course where I&#39;m making a server program that is going to respond to both TCP and UDP connections. I&#39;ve seen examples of this being done separately, but never in the same program. Does anyone have an example on how to do this? Any help would be greatly appreciated.</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>The hardest part of understanding this is to understand that there is nothing hard to understand. (No sarcasm.) To have a server running two listening ports, you just start one goroutine to listen to one port, and start another to listen to the other. The only slight bit of drama is making sure the main goroutine doesn&#39;t fall off the bottom and exit, resulting in the program terminating. Sometimes in that situation I just randomly pick one of the things I&#39;m doing to put into the main function at the end. (In production code that&#39;s a bad idea, but if I just hacking around that works fine.)</p></pre>Stulenzor: <pre><p>Oh, well, that&#39;s great! It&#39;s not production code at all, so I guess I&#39;m in the clear. Thanks for the reply!</p></pre>neondirt: <pre><p>Don&#39;t ask the internet to do your homework.</p> <p>That being said, the go standard library supports both udp and tcp listeners. Read up on it.</p></pre>Swedophone: <pre><p>SIP and DNS servers are two examples which usually listen for both TCP and UDP connections on the same port.</p></pre>lukechampine: <pre><p>You should clarify whether you need to listen for both TCP and UDP on the same port, or if they are listening on different ports. The latter is much easier than the former.</p></pre>cklein: <pre><p>There‘s literally no difference. You are probably referring to the same socket which would require a raw socket.</p></pre>lukechampine: <pre><p>huh, TIL. Thanks!</p></pre>Stulenzor: <pre><p>It&#39;s supposed to follow RFC 865, so I&#39;m guessing they&#39;re going to listen on the same port. My code looks like this at the moment: <a href="https://pastebin.com/paT7qYFc" rel="nofollow">https://pastebin.com/paT7qYFc</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

437 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传