using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ChatServer
{
class Program
{
static void Main(string[] args)
{
bool cancel = false;
byte[] buffer = new byte[1024];
string message;
byte[] messageBytes;
int count = 0;
TcpListener tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 13000));
tcpListener.Start();
Console.WriteLine("Waiting for a connection... ");
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connected.");
NetworkStream stream = tcpClient.GetStream();
Task.Factory.StartNew(() =>
{
while ((count = stream.Read(buffer, 0, buffer.Length)) != 0)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss fff} Reply from server {tcpClient.Client.LocalEndPoint.ToString()}:{Encoding.UTF8.GetString(buffer, 0, count)}");
}
});
Task t = Task.Factory.StartNew(() =>
{
while(!cancel)
{
message = Console.ReadLine();
if (message.ToUpper() == "Y")
{
cancel = true;
return;
}
messageBytes = Encoding.UTF8.GetBytes(message);
stream.Write(messageBytes, 0, messageBytes.Length);
}
});
if (cancel) tcpClient.Close();
while (true)
{
if (t != null && t.IsCompleted) break;
}
}
}
}
using System;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace ChatClient
{
class Program
{
static void Main(string[] args)
{
bool cancel = false;
byte[] buffer = new byte[1024];
string message;
byte[] messageBytes;
int count = 0;
try
{
TcpClient tcpClient = new TcpClient(new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(p => p.AddressFamily == AddressFamily.InterNetwork).First(), 14000));
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.94.26"), 13000));
NetworkStream stream = tcpClient.GetStream();
Task.Factory.StartNew(() =>
{
while ((count = stream.Read(buffer, 0, buffer.Length)) != 0)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss fff} Reply from client {tcpClient.Client.LocalEndPoint.ToString()}:{Encoding.UTF8.GetString(buffer, 0, count)}");
}
});
Task t = Task.Factory.StartNew(() =>
{
while (!cancel)
{
message = Console.ReadLine();
if (message.ToUpper() == "Y")
{
cancel = true;
return;
}
messageBytes = Encoding.UTF8.GetBytes(message);
stream.Write(messageBytes, 0, messageBytes.Length);
Thread.Sleep(10);
}
});
if (cancel) tcpClient.Close();
while (true)
{
if (t != null && t.IsCompleted) break;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有