c#socket编程连接不上 高分悬赏
我做了最基础的一个,只是测试能不能接上这是服务器端的usingSystem;usingSystem.Collections.Generic;usingSystem.Lin...
我做了最基础的一个,只是测试能不能接上
这是服务器端的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ipa = IPAddress.Parse(host);
IPEndPoint ipep = new IPEndPoint(ipa, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipep);
s.Listen(0);
Console.WriteLine("正在监听");
Socket ls = s.Accept();
Console.WriteLine("已建立连接");
string recstr = "";
byte[] recbyte = new byte[1024];
int recleng;
recleng = ls.Receive(recbyte, recbyte.Length, 0);
recstr += Encoding.UTF8 .GetString(recbyte,0,recleng );
Console.WriteLine("{0}",recstr );
Console.ReadLine();
}
}
}
这是客户端的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int port = 2000;
string ipa = "202.20.81.90";
IPAddress ipas = IPAddress.Parse(ipa);
IPEndPoint ipenp = new IPEndPoint(ipas, port);
Socket sdd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sdd.Connect(ipenp);
Console.WriteLine("正在连接");
string sendstr = "你好";
byte[] sendbyte = new byte[1024];
sendbyte = Encoding.UTF8 .GetBytes(sendstr);
sdd.Send(sendbyte, sendbyte.Length, 0);
sendstr=Console.ReadLine();
sendbyte = Encoding.UTF8.GetBytes(sendstr);
sdd.Send(sendbyte, sendbyte.Length, 0);
Console.ReadLine();
}
}
}
我将客户端的ip改为“127.0.0.1”后俩个都在自己机子上可以用
但将客户端的放到别人机子上就是接不起来,不知道为什么?请各位帮忙解决下,谢了 展开
这是服务器端的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ipa = IPAddress.Parse(host);
IPEndPoint ipep = new IPEndPoint(ipa, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipep);
s.Listen(0);
Console.WriteLine("正在监听");
Socket ls = s.Accept();
Console.WriteLine("已建立连接");
string recstr = "";
byte[] recbyte = new byte[1024];
int recleng;
recleng = ls.Receive(recbyte, recbyte.Length, 0);
recstr += Encoding.UTF8 .GetString(recbyte,0,recleng );
Console.WriteLine("{0}",recstr );
Console.ReadLine();
}
}
}
这是客户端的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int port = 2000;
string ipa = "202.20.81.90";
IPAddress ipas = IPAddress.Parse(ipa);
IPEndPoint ipenp = new IPEndPoint(ipas, port);
Socket sdd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sdd.Connect(ipenp);
Console.WriteLine("正在连接");
string sendstr = "你好";
byte[] sendbyte = new byte[1024];
sendbyte = Encoding.UTF8 .GetBytes(sendstr);
sdd.Send(sendbyte, sendbyte.Length, 0);
sendstr=Console.ReadLine();
sendbyte = Encoding.UTF8.GetBytes(sendstr);
sdd.Send(sendbyte, sendbyte.Length, 0);
Console.ReadLine();
}
}
}
我将客户端的ip改为“127.0.0.1”后俩个都在自己机子上可以用
但将客户端的放到别人机子上就是接不起来,不知道为什么?请各位帮忙解决下,谢了 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询