-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCP.cs
145 lines (137 loc) · 5.27 KB
/
TCP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#define debug
using System;
using System.Net.Sockets;
using System.Text;
//fsutil file createnew null.txt 5278350000
//CMD创建一个指定大小的文件
namespace UChat
{
/// <summary>
/// 进行局域网 TCP 通信。信息发送在 50012 端口进行,文件传输在 50024 端口进行。
/// </summary>
/// <returns></returns>
public class TCP
{
/// <summary>
/// 发送聊天消息。
/// </summary>
/// <param name="ip">目标主机 IP</param>
/// <param name="message">要发送的消息。应该以本机 UID 打头方便对面识别。</param>
/// <param name="port">50012 端口。</param>
public void TCPMessageSender(string ip, string message, int port)
{
try
{
TcpClient client = new TcpClient(ip, port);//初始化一个对象,并连接到目标主机(因此不需要 connect 方法)
///NetworkStream 是面向连接,基于 TCP/IP 的网络流,在 UDP 使用中虽然不会报错但是会异常
NetworkStream sendStream = client.GetStream();
byte[] sendBytes = Encoding.UTF8.GetBytes(message);
using (client)
{
using (sendStream)
{
sendStream.Write(sendBytes, 0, sendBytes.Length);
sendStream.Flush();
sendStream.Close();//关闭网络流
}
}
}
catch
{
}
}
/// <summary>
/// 监听在 50012 端口的 TCP 信息发送。这是一个可以在后台线程上循环复用的方法。
/// </summary>
public void TCPMessageListener()
{
HostInfo hostInfo = new HostInfo();
TcpListener tcpListener = new TcpListener(hostInfo.IPv4Address, 50012);//本机ip
tcpListener.Start();
while (true)
{
try
{
TcpClient newClient = tcpListener.AcceptTcpClient();
string message = "";
using (newClient)
{
byte[] buffer = new byte[newClient.ReceiveBufferSize];//缓冲字节数组
NetworkStream clientStream = newClient.GetStream();
using (clientStream)
{
clientStream.Read(buffer, 0, buffer.Length);
message = Encoding.UTF8.GetString(buffer).Trim('\0');//收到的信息
clientStream.Close();
}
newClient.Close();
}
MessageProcessor(message);//将信息载体交给下一级方法分割处理
}
catch
{
}
}
}
/// <summary>
/// 监听端口的 TCP 信息发送。一次性使用。
/// </summary>
/// <param name="port">监听的端口号</param>
/// <returns></returns>
public string TCPMessageListener(int port)
{
HostInfo hostInfo = new HostInfo();
TcpListener tcpListener = new TcpListener(hostInfo.IPv4Address, port);//本机ip
tcpListener.Start();
string message = "";
try
{
TcpClient newClient = tcpListener.AcceptTcpClient();
using (newClient)
{
byte[] buffer = new byte[newClient.ReceiveBufferSize];//缓冲字节数组
NetworkStream clientStream = newClient.GetStream();
using (clientStream)
{
clientStream.Read(buffer, 0, buffer.Length);
message = Encoding.UTF8.GetString(buffer).Trim('\0');//收到的信息
clientStream.Close();
}
newClient.Close();
}
}
catch (Exception e)
{
return "Error!\r\n" + e.ToString();
}
finally
{
tcpListener.Stop();
}
return message;
}
/// <summary>
/// 对收到的 TCP 信息作进一步归档处理。
/// </summary>
/// <param name="carrier"></param>
private static void MessageProcessor(string carrier)
{
if (carrier.Substring(0, 2) == "FR")//首部有文件传输请求
{
}
else//首部没有 FR(File transfer Request) , 这是一个正常的聊天消息
{
string remoteUID = carrier.Substring(0, 17);
string message = carrier.Substring(17);
if (remoteUID == CommonFoundations.RemoteUID)//收到的消息是正在聊天的对面发的,直接搞到聊天框
{
FormMain.formMain.HandleYouMessage(message);
}
else//不是正在聊天的人发的,归档为未读消息
{
FormMain.formMain.WriteUnread(remoteUID, message);
}
}
}
}
}