概念
1.Socket:网络上的两个程序通过一个双向的通信连接实现 数据的交换,这个连接的一端称为一个socket
2.端口:
3.传输协议: TCP/IP:网络通讯协议,有网络层ip和传输层tcp组成,ISO
UDP:用户数据协议,一种无连接传输协议
TCP/IP与UDP的区别:
不同点:
TCP/IP: 安全稳定 效率较低 面向连接的(三次握手)
UDP: 不稳定,数据易丢失 高效快速 不需要连接
相同点: 都是通过二进制传输 都位于OSI模型的传输层
Sokect通信基本流程
案例代码:
客户端
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
txtIP.Text = "192.168.2.126";
txtPort.Text = "8086";
}
Socket socketSend;
/// <summary>
/// 连接服务端
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnStart_Click(object sender, EventArgs e)
{
try
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(txtIP.Text);
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
socketSend.Connect(point);
showMessage("连接成功");
//开启一个新的线程不停接受服务端的信息
Thread th = new Thread(Recive);
th.IsBackground = true;
th.Start();
}
catch
{
}
}
/// <summary>
/// 发送信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
string str = txtMessage.Text.Trim();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
socketSend.Send(buffer);
}
/// <summary>
/// 接收信息
/// </summary>
void Recive()
{
while (true)
{
byte[] buffer = new byte[1024 * 1020 * 2];
int r = socketSend.Receive(buffer);
if (r == 0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, r);
showMessage(socketSend.RemoteEndPoint + ":" + str);
}
}
void showMessage(string str)
{
txtLog.AppendText(str + "
");
}
}
服务端
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>();
private void Form1_Load_1(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
txtIP.Text = "192.168.2.126";
txtPort.Text = "8086";
}
private void BtnStart_Click(object sender, EventArgs e)
{
try
{
Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(txtIP.Text);//IPAddress.Any;
//2.创建端口号对象
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
//3.监听
socketWatch.Bind(point);
ShowMsg("监听成功");
socketWatch.Listen(10);
//4.等待客户端连接
Thread th = new Thread(Listen);
th.IsBackground = true;
th.Start(socketWatch);
}
catch
{
}
}
/// <summary>
/// 监听:客户端的连接,并且创建与之通信用的socket
/// </summary>
Socket socketSend;
void Listen(object obj)
{
Socket socketWatch = obj as Socket;
while (true)
{
socketSend = socketWatch.Accept();
//将远程客户端的Ip和socket存入集合中
dicSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend);
cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString());
//负责和客户端通信的socker,需要存储
ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功");
//开启一个新线程不停接受客户端发来的信息
Thread th = new Thread(Recive);
th.IsBackground = true;
th.Start(socketSend);
}
}
void Recive(object obj)
{
Socket socketSend = obj as Socket;
while (true)
{
//客户端连接成功后服务器接受客户端发来的信息
byte[] buffer = new byte[1024 * 1020 * 2];
int r = socketSend.Receive(buffer);
if (r == 0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, r);
ShowMsg(socketSend.RemoteEndPoint + ":" + str);
}
}
void ShowMsg(string str)
{
txtLog.AppendText(str + "
");
}
private void txtSend_Click(object sender, EventArgs e)
{
string str = txtMessage.Text.Trim();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
// socketSend.Send(buffer);
//获得服务端在下拉框中选中的ip地址
string ip = cboUsers.SelectedItem.ToString();
dicSocket[ip].Send(buffer);
}
}