C# 打开和使用 TCP 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8896286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:02:14  来源:igfitidea点击:

Open and Use a TCP Port

c#.netvb.nettcpports

提问by Elmo

I need to transfer data between several computers in a network using my app. I thought of using TCP ports. Are there any tutorials on how to open and use them? I only want to make a file transfer and chat app that works offline.

我需要使用我的应用程序在网络中的多台计算机之间传输数据。我想到了使用 TCP 端口。有没有关于如何打开和使用它们的教程?我只想制作一个离线工作的文件传输和聊天应用程序。

采纳答案by Almo

Here are several examples. Between all of these, you should be able to get what you need.

这里有几个例子。在所有这些之间,您应该能够获得所需的东西。

http://www.codeproject.com/KB/IP/TCPIPChat.aspx

http://www.codeproject.com/KB/IP/TCPIPChat.aspx

http://www.codeproject.com/KB/IP/realtimeapp.aspx

http://www.codeproject.com/KB/IP/r​​ealtimeapp.aspx

回答by Jon Skeet

I suggest you look at the documentation for TcpClientand TcpListener, both of which include examples.

我建议你看一下文档TcpClientTcpListener,两者都含有例子。

It's hard to give a more specific recommendation without a more specific question - are you trying to create your own protocol on top of TCP/IP, or implement an existing one? If it's an existing one, there may well be a higher-level API already available.

如果没有更具体的问题,很难给出更具体的建议——您是在尝试在 TCP/IP 之上创建自己的协议,还是实施现有的协议?如果它是现有的,那么很可能已经有一个更高级别的 API 可用。

回答by Shai

You can use the TcpChannelclass to open TCP ports and exposemethods/functions in your Application to OTHER applications.

您可以使用TcpChannel类打开 TCP 端口并将应用程序中的方法/函数公开给其他应用程序。

Advantage: Allows your application to behave like a "webservice"

优点:允许您的应用程序表现得像“网络服务”

Disadvantage: Client applications must know the function/method prototype

缺点:客户端应用程序必须知道函数/方法原型

good luck!

祝你好运!