C#中的TCP套接字-客户端-服务器体系结构 [英] TCP sockets in C# - Client - Server Architecture

查看:108
本文介绍了C#中的TCP套接字-客户端-服务器体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:如何在不在同一网络上的两台计算机之间建立套接字或任何其他连接.我试图找出解决方案,但是我尝试的所有事情都只能在同一台计算机上运行(服务器和客户端应用程序在同一台机器上运行//本地主机).我尝试关闭防火墙,但id并没有帮助.

具体来说,我想要的是能够将字符串发送到远程计算机或任何其他对象.


任何帮助将不胜感激:)

My question is: How to make socket or any other connection between two computers which are not on the same network. I tried to find out solution, but every thing i tried was working only on same computer (Server and client application running on same machine // localhost). I tried turning off firewalls but id did not helped.

Specifically what i want, is to be able to send string to remote computer, or any other object.


Any help would be appreciated :)

推荐答案

您需要将所选择的端口移植.
You need to portforward the port chosen.
//client part
TcpClient client = new TcpClient();
client.Connect(new IPAdress(new Byte[] { 127, 0, 0, 1 }), 12345); //Replace 127,0,0,1 with your external ip and 12345 with the choosen port (and forwarderd on the server)
//Now its connected or it times out

//Server part
TcpListen server = new TcpListener(IPAdress.Any, 12345); //Replace 12345 with the forwarded port
server.Start(); //Start Server
server.AcceptTcpClient(); //Holds the thread till a client connects


这篇关于C#中的TCP套接字-客户端-服务器体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆