应用知识点
- Socket
- TCP协议
TCP协议全称: 传输控制协议, 顾名思义, 就是要对数据的传输进行一定的控制.
- Socket一般应用模式(服务器端和客户端)
服务器端的Socket(至少需要两个)
1.一个负责接收客户端连接请求(但不负责与客户端通信)
2.每成功接收到客户端的连接便在服务器端产生一个对应的复杂通信的Socket
客户端的Socket
必须指定要连接的服务器地址和端口
通过创建一个Socket对象来初始化一个到服务器端的TCP连接
- 一些函数
2. IPAddress:包含了一个IP地址
IPEndPoint:包含了一对IP地址和端口号
3. Socket():创建一个Socket
Bind():绑定一个本地的IP和端口号(IPEndPoint)
Listen():让Socket侦听传入的连接吃那个病,并指定侦听队列容量
Connect():初始化与另一个Socket的连接
Accept():接收连接并返回一个新的Socket
Send():输出数据到Socket
Receive():从Socket中读取数据
Close():关闭Socket,销毁连接
4. socket连接过程:
服务器监听:服务器端socket并不定位具体的客户端socket,而是处于等待监听状态,实时监控网络状态。
客户端请求:客户端clientSocket发送连接请求,目标是服务器的serverSocket。为此,clientSocket必须知道serverSocket的地址和端口号,进行扫描发出连接请求。
连接确认:当服务器socket监听到或者是受到客户端socket的连接请求时,服务器就响应客户端的请求,建议一个新的socket,把服务器socket发送给客户端,一旦客户端确认连接,则连接建立。
二.Threading
利用任务开启线程
new Thread(() =>
{
while(true) //死循环
{
byte[] receivedData = new byte[1024 * 1024];
g_client.Receive(receivedData);
textBox3.Text += "\r\n"+DateTime.Now + "接收:" + ASCIIEncoding.UTF8.GetString(receivedData);
}
}).Start();
程序
- 一. 界面
- 服务端
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets; namespace 聊天程序2Laplace { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } Socket g_server; //一个用来监听,一个负责收发 Socket g_accept; private void button1_Click(object sender, EventArgs e) { g_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//监听 IPAddress ip = IPAddress.Parse(textBox1.Text); IPEndPoint iPEndPoint = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text)); g_server.Bind(iPEndPoint); g_server.Listen(10); g_accept = g_server.Accept(); new Thread(() => { while (true) { byte[] recivedData = new byte[1024 * 1024]; g_accept.Receive(recivedData); textBox3.Text += "\r\n" + DateTime.Now + "接收:" + ASCIIEncoding.UTF8.GetString(recivedData) ; } }).Start(); } void SendMessage(string msg) { g_accept.Send(ASCIIEncoding.UTF8.GetBytes(msg)); } private void button2_Click(object sender, EventArgs e) { SendMessage(textBox4.Text); textBox3.Text +="\r\n"+DateTime.Now + "发送:" + textBox4.Text ; textBox4.Text = null; } private void button3_Click(object sender, EventArgs e) { textBox3.Text =null; } } }
- 客户端
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets; namespace 聊天程序Laplace { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } Socket g_client; private void button1_Click(object sender, EventArgs e) { g_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(textBox1.Text); IPEndPoint iPEndPoint = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text)); g_client.Connect(iPEndPoint); new Thread(() => { while(true) { byte[] receivedData = new byte[1024 * 1024]; g_client.Receive(receivedData); textBox3.Text += "\r\n"+DateTime.Now + "接收:" + ASCIIEncoding.UTF8.GetString(receivedData); } }).Start(); } //发送消息1.string->byte[]2.send void SendMessage(string msg) { g_client.Send(ASCIIEncoding.UTF8.GetBytes(msg)); } private void button2_Click(object sender, EventArgs e) { SendMessage(textBox4.Text); textBox3.Text += "\r\n"+DateTime.Now + "发送:" + textBox4.Text; textBox4.Text = null; } private void button3_Click(object sender, EventArgs e) { textBox3.Text = null; } } }