static void Main(string[] args)
{
TcpClient client = null;
NetworkStream stream = null;
byte[] buffer = null;
string receiveString = null;
IPAddress localIP = IPAddress.Parse("127.0.0.1");
int localPort = 11000;
TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口实例化Listener
listener.Start();//开始监听
while (true)
{
client = listener.AcceptTcpClient();//接受一个Client
buffer = new byte[client.ReceiveBufferSize];
stream = client.GetStream();//获取网络流
stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据
stream.Close();//关闭流
client.Close();//关闭Client
receiveString = Encoding.Default.GetString(buffer).Trim('\0');//转换成字符串
Console.WriteLine(receiveString);
}
}
static void Main(string[] args)
{
string sendString = null;//要发送的字符串
byte[] sendData = null;//要发送的字节数组
TcpClient client = null;//TcpClient实例
NetworkStream stream = null;//网络流
IPAddress remoteIP = IPAddress.Parse("127.0.0.1");//远程主机IP
int remotePort = 11000;//远程主机端口
while (true)//死循环
{
sendString = Console.ReadLine();//获取要发送的字符串
sendData = Encoding.Default.GetBytes(sendString);//获取要发送的字节数组
client = new TcpClient();//实例化TcpClient
try
{
client.Connect(remoteIP, remotePort);//连接远程主机
}
catch (System.Exception ex)
{
Console.WriteLine("连接超时,服务器没有响应!");//连接失败
Console.ReadKey();
return;
}
stream = client.GetStream();//获取网络流
stream.Write(sendData, 0, sendData.Length);//将数据写入网络流
stream.Close();//关闭网络流
client.Close();//关闭客户端
}
}
static void Main(string[] args)
{
UdpClient client = null;
string receiveString = null;
byte[] receiveData = null;
//实例化一个远程端点,IP和端口可以随意指定,等调用client.Receive(ref remotePoint)时会将该端点改成真正发送端端点
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
client = new UdpClient(11000);
receiveData = client.Receive(ref remotePoint);//接收数据
receiveString = Encoding.Default.GetString(receiveData);
Console.WriteLine(receiveString);
client.Close();//关闭连接
}
}
static void Main(string[] args)
{
string sendString = null;//要发送的字符串
byte[] sendData = null;//要发送的字节数组
UdpClient client = null;
IPAddress remoteIP = IPAddress.Parse("127.0.0.1");
int remotePort = 11000;
IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);//实例化一个远程端点
while (true)
{
sendString = Console.ReadLine();
sendData = Encoding.Default.GetBytes(sendString);
client = new UdpClient();
client.Send(sendData, sendData.Length, remotePoint);//将数据发送到远程端点
client.Close();//关闭连接
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有