客户端代码
namespace Client
{
class Program
{
static void Main(string[] args)
{
Console.Write("客户端开始.....");
//建立一个客户端的连接
TcpClient tcp = new TcpClient();
NetworkStream stream = null;
try
{
//指定端口号和地址 注意:如果多个客服端连接就必须实例化多个TcpClient
tcp.Connect(IPAddress.Parse("127.0.0.1"), 8070);
//用于接收和返回的数据
stream = tcp.GetStream();
//定义个信息变量
string msg = "";
do
{
Console.Write("请输入数据:");
msg = Console.ReadLine();
if (msg != "Q" && !string.IsNullOrEmpty(msg))
{
try
{
//向服务器发送数据 注:必须转换成字节序列,然后通过流发送到服务端
// Byte[] bytes = Encoding.Unicode.GetBytes(msg); //将string转换成字节
// stream.Write(bytes, 0, bytes.Length); //写入流中发送到服务端
//这也是一种方式
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(msg);
//接收服务器端返回的数据
Byte[] by = new byte[8192]; //定义一个数组
int m = stream.Read(by, 0, 8192); //把字节读取出来存入数组中
string msgS = Encoding.Unicode.GetString(by, 0, m); //将数组转换成string字符串
Console.Write(msgS);
}
catch (Exception x)
{
Console.Write(x.Message);
return;
}
}
} while (msg != "Q");
}
catch (Exception x)
{
Console.Write(x.Message);
}
stream.Close();
tcp.Close();
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.Write("客户端开始.....");
//建立一个客户端的连接
TcpClient tcp = new TcpClient();
NetworkStream stream = null;
try
{
//指定端口号和地址 注意:如果多个客服端连接就必须实例化多个TcpClient
tcp.Connect(IPAddress.Parse("127.0.0.1"), 8070);
//用于接收和返回的数据
stream = tcp.GetStream();
//定义个信息变量
string msg = "";
do
{
Console.Write("请输入数据:");
msg = Console.ReadLine();
if (msg != "Q" && !string.IsNullOrEmpty(msg))
{
try
{
//向服务器发送数据 注:必须转换成字节序列,然后通过流发送到服务端
// Byte[] bytes = Encoding.Unicode.GetBytes(msg); //将string转换成字节
// stream.Write(bytes, 0, bytes.Length); //写入流中发送到服务端
//这也是一种方式
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(msg);
//接收服务器端返回的数据
Byte[] by = new byte[8192]; //定义一个数组
int m = stream.Read(by, 0, 8192); //把字节读取出来存入数组中
string msgS = Encoding.Unicode.GetString(by, 0, m); //将数组转换成string字符串
Console.Write(msgS);
}
catch (Exception x)
{
Console.Write(x.Message);
return;
}
}
} while (msg != "Q");
}
catch (Exception x)
{
Console.Write(x.Message);
}
stream.Close();
tcp.Close();
}
}
}
服务端代码
namespace ClientServer
{
class Program
{
static void Main(string[] args)
{
Console.Write("服务器端开始监听:");
Tests t = new Tests();
t.Stars();
}
}
public class Tests
{
public TcpListener tp;
public bool isNormalExit = false;
public string LocalIp = "127.0.0.1";
public int Port = 8070;
/// <summary>
/// 接收客户端的连接
/// </summary>
public void Stars()
{
tp = new TcpListener(IPAddress.Parse(LocalIp), Port);
tp.Start();
TcpClient client;
while (true)
{
try
{
client = tp.AcceptTcpClient();
Console.Write("连接成功{0},{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
}
catch (Exception x)
{
Console.Write(x.Message);
break;
}
Thread threadReceive = new Thread(ReceiveData);
threadReceive.Start(client);
}
}
public void ReceiveData(object ob)
{
TcpClient tcp = (TcpClient)ob;
while (isNormalExit == false)
{
Console.Write(tcp.Client.LocalEndPoint + "--" + tcp.Client.RemoteEndPoint);
try
{
NetworkStream streas = tcp.GetStream();
Byte[] bytes = new byte[8092];
try
{
int m = streas.Read(bytes, 0, bytes.Length);
if (m == 0)
{
Console.WriteLine("空数据");
break;
}
string msg = Encoding.Unicode.GetString(bytes, 0, m);
Console.Write(msg);
}
catch (Exception)
{
break;
}
}
catch (Exception x)
{
}
}
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.Write("服务器端开始监听:");
Tests t = new Tests();
t.Stars();
}
}
public class Tests
{
public TcpListener tp;
public bool isNormalExit = false;
public string LocalIp = "127.0.0.1";
public int Port = 8070;
/// <summary>
/// 接收客户端的连接
/// </summary>
public void Stars()
{
tp = new TcpListener(IPAddress.Parse(LocalIp), Port);
tp.Start();
TcpClient client;
while (true)
{
try
{
client = tp.AcceptTcpClient();
Console.Write("连接成功{0},{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
}
catch (Exception x)
{
Console.Write(x.Message);
break;
}
Thread threadReceive = new Thread(ReceiveData);
threadReceive.Start(client);
}
}
public void ReceiveData(object ob)
{
TcpClient tcp = (TcpClient)ob;
while (isNormalExit == false)
{
Console.Write(tcp.Client.LocalEndPoint + "--" + tcp.Client.RemoteEndPoint);
try
{
NetworkStream streas = tcp.GetStream();
Byte[] bytes = new byte[8092];
try
{
int m = streas.Read(bytes, 0, bytes.Length);
if (m == 0)
{
Console.WriteLine("空数据");
break;
}
string msg = Encoding.Unicode.GetString(bytes, 0, m);
Console.Write(msg);
}
catch (Exception)
{
break;
}
}
catch (Exception x)
{
}
}
}
}
}