服务端 这个是放在外网远程服务器上
class Program
{
static void Main(string[] args)
{
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endpoints = ipGlobalProperties.GetActiveTcpListeners(); TcpListener listener;
listener = new TcpListener(new IPEndPoint(IPAddress.Any, 0));
listener.Start(); Console.WriteLine( (listener.LocalEndpoint as IPEndPoint).Port );
Console.ReadLine();
listener.Stop();
}
}
客户端
private void EstablishConnection()
{
toolStripStatusLabel1.Text = "正在连接服务器";
try
{
//clientsocket = new TcpClient(serveraddress, serverport);
int ps =int.Parse(label1.Text.Remove(0, 2));
label2.Text ="["+ ps +"]";
clientsocket = new TcpClient("116.255.188.240", ps);
ns = clientsocket.GetStream();
sr = new StreamReader(ns);
connected = true;
if (connected)
toolStripStatusLabel1.Text = "已经连接上服务器"; }
catch (Exception e)
{
MessageBox.Show("不能连接捯服务器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
toolStripStatusLabel1.Text = "已断开连接";
}
} private void Linkbutton_Click(object sender, EventArgs e)
{
EstablishConnection();
}
116.255.188.240 这是远程 ip ps 是远程服务器 空闲开始监听的端口 通过返回客户端得到的 是正确的 在本地测试没问题 服务端能正常开启监听 现在就是客户端连不上!!!这是服务器设置问题么??请教高人 谢谢 分不多了 就 20 分 请指点 在线等 。
class Program
{
static void Main(string[] args)
{
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endpoints = ipGlobalProperties.GetActiveTcpListeners(); TcpListener listener;
listener = new TcpListener(new IPEndPoint(IPAddress.Any, 0));
listener.Start(); Console.WriteLine( (listener.LocalEndpoint as IPEndPoint).Port );
Console.ReadLine();
listener.Stop();
}
}
客户端
private void EstablishConnection()
{
toolStripStatusLabel1.Text = "正在连接服务器";
try
{
//clientsocket = new TcpClient(serveraddress, serverport);
int ps =int.Parse(label1.Text.Remove(0, 2));
label2.Text ="["+ ps +"]";
clientsocket = new TcpClient("116.255.188.240", ps);
ns = clientsocket.GetStream();
sr = new StreamReader(ns);
connected = true;
if (connected)
toolStripStatusLabel1.Text = "已经连接上服务器"; }
catch (Exception e)
{
MessageBox.Show("不能连接捯服务器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
toolStripStatusLabel1.Text = "已断开连接";
}
} private void Linkbutton_Click(object sender, EventArgs e)
{
EstablishConnection();
}
116.255.188.240 这是远程 ip ps 是远程服务器 空闲开始监听的端口 通过返回客户端得到的 是正确的 在本地测试没问题 服务端能正常开启监听 现在就是客户端连不上!!!这是服务器设置问题么??请教高人 谢谢 分不多了 就 20 分 请指点 在线等 。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货