C#另类重写
这个我不知道起个什么名字好。只是在写TcpClient的时候想重写了TcpClient Client属性的Send方法,实际上就是Socket的Send方法了。因为我要以字符串交互数据,这个byte[]转换太麻烦了。
比如发送接收,在没有有重写的时候,发送和接受台麻烦了。
TcpClient client = listener.AcceptTcpClient();
string buff = "成功建立连接\r\n";
byte[] b = System.Text.Encoding.Default.GetBytes(buff);
client.Client.Send(b);
NetworkStream myNetworkStream = client.GetStream();
byte[] bytes = new byte[1024];
int bytesRead = myNetworkStream.Read(bytes, 0, bytes.Length);
string getstr = Encoding.Default.GetString(bytes, 0, bytesRead);
string buff = "成功建立连接\r\n";
byte[] b = System.Text.Encoding.Default.GetBytes(buff);
client.Client.Send(b);
NetworkStream myNetworkStream = client.GetStream();
byte[] bytes = new byte[1024];
int bytesRead = myNetworkStream.Read(bytes, 0, bytes.Length);
string getstr = Encoding.Default.GetString(bytes, 0, bytesRead);
如果定义一个类 MyTcpClient 继承自 TcpClient ,但是listener.AcceptTcpClient();的类型是TcpClient ,无法转换为MyTcpClient 。
MyTcpClient client = (MyTcpClient )listener.AcceptTcpClient();是不可以的。
那么,如果我直接定义MyTcpClient 有Send方法
/// <summary>
/// 下发服务端信息
/// </summary>
/// <param name="msg"></param>
public void Send(string msg)
{
byte[] b = System.Text.Encoding.Default.GetBytes(msg);
Tcp.Client.Send(b);
}
在这里是无法使用了。
另类一点的方法是聚合一下。
具体代码就是
/// <summary>
/// 该类型为TcpClient
/// 值为new TcpListener().AcceptTcpClient()
/// </summary>
public class MyTcpClient
{
TcpClient Tcp { get; set; }
NetworkStream myNetworkStream;
public bool Connected { get { return Tcp.Connected; } }
/// <summary>
/// 下发服务端信息
/// </summary>
/// <param name="msg"></param>
public void Send(string msg)
{
byte[] b = System.Text.Encoding.Default.GetBytes(msg);
Tcp.Client.Send(b);
}
public void Close()
{
myNetworkStream.Close();
Tcp.Close();
}
/// <summary>
/// 接收客户端数据
/// </summary>
/// <returns></returns>
public string Received()
{
byte[] bytes = new byte[1024];
int bytesRead = myNetworkStream.Read(bytes, 0, bytes.Length);
string getstr = Encoding.Default.GetString(bytes, 0, bytesRead);
return getstr;
}
MyTcpClient(TcpClient tcp)
{
Tcp = tcp;
myNetworkStream = Tcp.GetStream();
}
/// <summary>
/// 重载等于
/// </summary>
/// <param name="tcp"></param>
/// <returns></returns>
public static implicit operator MyTcpClient(TcpClient tcp)
{
return new MyTcpClient(tcp);
}
}
/// 该类型为TcpClient
/// 值为new TcpListener().AcceptTcpClient()
/// </summary>
public class MyTcpClient
{
TcpClient Tcp { get; set; }
NetworkStream myNetworkStream;
public bool Connected { get { return Tcp.Connected; } }
/// <summary>
/// 下发服务端信息
/// </summary>
/// <param name="msg"></param>
public void Send(string msg)
{
byte[] b = System.Text.Encoding.Default.GetBytes(msg);
Tcp.Client.Send(b);
}
public void Close()
{
myNetworkStream.Close();
Tcp.Close();
}
/// <summary>
/// 接收客户端数据
/// </summary>
/// <returns></returns>
public string Received()
{
byte[] bytes = new byte[1024];
int bytesRead = myNetworkStream.Read(bytes, 0, bytes.Length);
string getstr = Encoding.Default.GetString(bytes, 0, bytesRead);
return getstr;
}
MyTcpClient(TcpClient tcp)
{
Tcp = tcp;
myNetworkStream = Tcp.GetStream();
}
/// <summary>
/// 重载等于
/// </summary>
/// <param name="tcp"></param>
/// <returns></returns>
public static implicit operator MyTcpClient(TcpClient tcp)
{
return new MyTcpClient(tcp);
}
}
这样用MyTcpClient 替换掉TcpClient 是不是很另类,呵呵
使用的时候
TcpListener listener = null;
listener = new TcpListener(8009);
listener.Start();
while (true)
{
MyTcpClient client = listener.AcceptTcpClient();
client.Send(DateTime.Now.ToString() + "成功建立连接\r\n");
Console.Write(client.Received());
client.Close();
}
listener.Stop();
这就简单太多了 : p
listener = new TcpListener(8009);
listener.Start();
while (true)
{
MyTcpClient client = listener.AcceptTcpClient();
client.Send(DateTime.Now.ToString() + "成功建立连接\r\n");
Console.Write(client.Received());
client.Close();
}
listener.Stop();