Socket是什么?
套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息:连接使用的协议,本地主机的IP地址,本地进程的协议端口,远地主机的IP地址,远地进程的协议端口。应用层通过传输层进行数据通信时,TCP会遇到同时为多个应用程序进程提供并发服务的问题。多个TCP连接或多个应用程序进程可能需要通过同一个 TCP协议端口传输数据。为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了套接字(Socket)接口。应用层可以和传输层通过Socket接口,区分来自不同应用程序进程或网络连接的通信,实现数据传输的并发服务。
Socket的作用
我们可以通过在服务器创建一个Socket,用来监听客户端的请求,当客户端请求调用服务端应用时,服务端监听Socket就会创建一个链接Socket与客户端进行连接。
Socket实例
服务端(代码如下)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Socket服务端
{
public partial class Form1 : Form
{
Socket socketWatch;
Socket socketTake;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
Connect_Socket();
Thread th = new Thread(TakeMessage);
th.IsBackground = true;
th.Start(socketWatch);
}
private void button1_Click(object sender, EventArgs e)
{
SendMessage();
}
private void Connect_Socket()
{
try
{
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint ipendpoint = new IPEndPoint(ip, 50000);
socketWatch.Bind(ipendpoint);
socketWatch.Listen(10);
}
catch(Exception e)
{
MessageBox.Show("连接失败");
}
}
private void SendMessage()
{
string str = SendBox.Text;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
socketTake.Send(buffer);
}
private void TakeMessage(Object o)
{
Socket socketWatch = o as Socket;
while(true)
{
try
{
socketTake = socketWatch.Accept();
Thread th = new Thread(Recive);
th.IsBackground = true;
th.Start(socketTake);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
private void Recive(Object o)
{
Socket socketSend = o as Socket;
while(true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
int r = socketSend.Receive(buffer);
if(r==0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, r);
TakeBox.Text = str;
}
}
}
}
客户端(代码如下)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Socket客户端
{
public partial class Form1 : Form
{
Socket socketClint;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SendMessage();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
Connet_Socket();
}
private void Connet_Socket()
{
try
{
socketClint = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint point = new IPEndPoint(ip, 50000);
socketClint.Connect(point);
Thread th = new Thread(Recive);
th.IsBackground = true;
th.Start();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private void Recive()
{
while(true)
{
try
{
byte[] buffer = new byte[1024 * 1024 * 5];
int r = socketClint.Receive(buffer);
if(r==0)
{
break;
}
textBox1.Text = Encoding.UTF8.GetString(buffer, 0, r);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
private void SendMessage()
{
string str = textBox2.Text.Trim();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
socketClint.Send(buffer);
}
}
}
实现效果如下