C# TcpClient客户端连接服务端
已知服务端ip(本机ip)和端口ip:172.18.9.114portNum:51888客户端用:client=newTcpClient(ip,portNum);无法连接...
已知服务端ip(本机ip)和端口
ip:172.18.9.114
portNum:51888
客户端用: client = new TcpClient(ip, portNum);无法连接成功!
但是如果改为: client = new TcpClient(Dns.GetHostName(), portNum);却能连接成功!
ip为本机ip,客户端和服务端均在本机测试,为什么我用本机ip代替Dns.GetHostName()两者无法连接啊!求前辈指教,初学者谢谢大家! 展开
ip:172.18.9.114
portNum:51888
客户端用: client = new TcpClient(ip, portNum);无法连接成功!
但是如果改为: client = new TcpClient(Dns.GetHostName(), portNum);却能连接成功!
ip为本机ip,客户端和服务端均在本机测试,为什么我用本机ip代替Dns.GetHostName()两者无法连接啊!求前辈指教,初学者谢谢大家! 展开
3个回答
展开全部
服务端:
class mTcpServer
{
class server
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Thread newThread;
try
{
serverTcpListener = new TcpListener(localAddr, port);
serverTcpListener.Start();
while (true)
{
Console.Write("Waiting for a connection... ");
client = serverTcpListener.AcceptTcpClient();
Console.WriteLine("Connected!");
newThread = new Thread(new ThreadStart(newScream));
newThread.Start();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
Console.WriteLine("Stop listening for new clients");
serverTcpListener.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
static Int32 port = 9050;
static IPAddress localAddr = IPAddress.Parse("127.0.0.1");
static TcpListener serverTcpListener = null;
static Byte[] bytes = new Byte[256];
static String data = null;
static TcpClient client;
static void newScream()
{
data = null;
NetworkStream stream = client.GetStream();
stream.Write(Encoding.ASCII.GetBytes("Connected!"), 0, 10);
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
}
}
}
客户端:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace mTcpClint
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class client
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
byte[] data = new byte[1024];
//Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.Write("please input the server ip:");
string ipadd = Console.ReadLine();
Console.WriteLine();
Console.Write("please input the server port:");
int port = Convert.ToInt32(Console.ReadLine());
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);//服务器的IP和端口
TcpClient newclient;
newclient = new TcpClient();
newclient.Connect(ipadd, port);
int recv = newclient.Client.Receive(data);
string stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
while (true)
{
string input = Console.ReadLine();
if (input == "exit") break;
newclient.Client.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = newclient.Client.Receive(data);
stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
}
Console.WriteLine("disconnect from server");
newclient.Client.Shutdown(SocketShutdown.Both);
newclient.Close();
}
}
}
class mTcpServer
{
class server
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Thread newThread;
try
{
serverTcpListener = new TcpListener(localAddr, port);
serverTcpListener.Start();
while (true)
{
Console.Write("Waiting for a connection... ");
client = serverTcpListener.AcceptTcpClient();
Console.WriteLine("Connected!");
newThread = new Thread(new ThreadStart(newScream));
newThread.Start();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
Console.WriteLine("Stop listening for new clients");
serverTcpListener.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
static Int32 port = 9050;
static IPAddress localAddr = IPAddress.Parse("127.0.0.1");
static TcpListener serverTcpListener = null;
static Byte[] bytes = new Byte[256];
static String data = null;
static TcpClient client;
static void newScream()
{
data = null;
NetworkStream stream = client.GetStream();
stream.Write(Encoding.ASCII.GetBytes("Connected!"), 0, 10);
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
}
}
}
客户端:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace mTcpClint
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class client
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
byte[] data = new byte[1024];
//Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.Write("please input the server ip:");
string ipadd = Console.ReadLine();
Console.WriteLine();
Console.Write("please input the server port:");
int port = Convert.ToInt32(Console.ReadLine());
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);//服务器的IP和端口
TcpClient newclient;
newclient = new TcpClient();
newclient.Connect(ipadd, port);
int recv = newclient.Client.Receive(data);
string stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
while (true)
{
string input = Console.ReadLine();
if (input == "exit") break;
newclient.Client.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = newclient.Client.Receive(data);
stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
}
Console.WriteLine("disconnect from server");
newclient.Client.Shutdown(SocketShutdown.Both);
newclient.Close();
}
}
}
Jtti
2024-10-10 广告
2024-10-10 广告
Jtti是一家新加坡全球网络基础服务商,为数百万个网站提供支持,提供香港服务器、新加坡服务器等多种全球服务器,自营全球多个数据中心,为用户提供优质的网络资源和服务。
JTTI服务器整体性能是非常不错的,拥有CN2 GIA+BGP优化线路,多...
点击进入详情页
本回答由Jtti提供
展开全部
1)根据MSDN文档,TcpClient的构造函数
public TcpClient(string hostname,int port)
参数:
hostname 类型:System.String 要连接到的远程主机的 DNS 名。
port 类型:System.Int32 要连接到的远程主机的端口号。
注意:第一个参数hostname,只能是域名或主机名;不能直接使用字符串 "172.18.9.114"
2)使用以下方法,可以绕开 Dns.GetHostName( )
using System.Net;
……
EndPoint ep = new new IPEndPoint(IPAddress.Parse("172.18.9.114"), 518888);
//使用TcpClient另一个构造函数 public TcpCleent(EndPoint ep)
client = new TcpClient(ep);
……
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你看看返回的类型或者值是不是一样的,有可能值不一样啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询