一:有图有真相,很简单
二:客户端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace client
{
public partial class Client : Form
{
public Client()
{
InitializeComponent();
}
//创建 1个客户端套接字 和1个负责监听服务端请求的线程
Socket socketClient = null;
Thread threadClient = null;
private delegate void SetText(string text);
/// <summary>
/// 链接服务端
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_login_Click(object sender, EventArgs e)
{
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//这里的ip地址,端口号都是服务端绑定的相关数据。
IPAddress ip = IPAddress.Parse(tb_ip.Text.Trim());
IPEndPoint endpoint = new IPEndPoint(ip, Convert.ToInt32(tb_socket.Text.Trim()));
socketClient.Connect(endpoint);//链接有端口号与IP地址确定服务端.
ThreadStart thS = new ThreadStart(this.ReceMsg);
//客户端在接受服务端发送过来的数据是通过Socket 中的Receive方法,
//该方法会阻断线程,所以我们自己为该方法创建了一个线程
threadClient = new Thread(thS);
threadClient.IsBackground = true;//设置后台线程
threadClient.Start();
}
//接收服务端数据
public void ReceMsg()
{
while (true)//持续监听服务端发来的消息
{
//定义一个2M的内存缓冲区 用于临时性存储接收到的信息
byte[] buffer = new byte[1024 * 1024 * 2];
//将客户端套接字接收到的数据存入内存缓冲区, 并获取其长度
int length = socketClient.Receive(buffer);
//clientSocket.Receive(buffer);//接收服务端发送过来的数据
string ReceiveMsg = System.Text.Encoding.UTF8.GetString(buffer,0,length);//把接收到的字节数组转成字符串显示在文本框中,如果没有长度,就会以零填充满,后面的日期就会不显示了。
ShowMsg("接收到数据: " + ReceiveMsg);
}
}
//消息框里面数据
void ShowMsg(string str)
{
if (this.tb_infor.InvokeRequired)
{
SetText st = new SetText(ShowMsg);
this.Invoke(st, new object[] { str });
}
else
{
//string Ystr = "";
//if (tb_infor.Text != "")
//{
// Ystr = tb_infor.Text + "\r\n";
//}
//tb_infor.Text = Ystr + str + GetCurrentTime();
//代替上面的方法
tb_infor.AppendText(str + GetCurrentTime() + "\r\n");
}
}/// <summary>
/// 发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_sendmessage_Click(object sender, EventArgs e)
{
string txtMsg = tb_message.Text;
//将输入的内容字符串转换为机器可以识别的字节数组
byte[] buffer = Encoding.UTF8.GetBytes(txtMsg);
socketClient.Send(buffer);
ShowMsg("发送的数据: " + txtMsg);
}
#endregion
/// <summary>
/// 获取当前系统时间的方法
/// </summary>
/// <returns>当前时间</returns>
private DateTime GetCurrentTime()
{
DateTime currentTime = new DateTime();
currentTime = DateTime.Now;
return currentTime;
}
}
}
三:服务器代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace socket
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
//关闭对文本框的非法线程操作检查,不建议用
//TextBox.CheckForIllegalCrossThreadCalls = false;
}
Socket socketWatch = null;//负责监听客户端的套接字
Thread threadWatch = null;//负责监听客户端的线程
//声明一个带参数委托
private delegate void SetText(string text);
/// 开始服务端监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_StartServer_Click(object sender, EventArgs e)
{
#region
//创建一个Socket实例
//第一个参数表示使用ipv4
//第二个参数表示发送的是数据流
//第三个参数表示使用的协议是Tcp协议
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//获取ip地址
IPAddress ip = IPAddress.Parse(tb_ip.Text.Trim());
//创建一个网络通信节点,这个通信节点包含了ip地址,跟端口号。
//这里的端口我们设置为1029,这里设置大于1024,为什么自己查一下端口号范围使用说明。
IPEndPoint endPoint = new IPEndPoint(ip, int.Parse(tb_socket.Text.Trim()));
//设置SOCKET允许多个SOCKET访问同一个本地IP地址和端口号
socketWatch.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//Socket绑定网路通信节点
socketWatch.Bind(endPoint);
//将套接字的监听队列长度限制为10
socketWatch.Listen(10);
ShowMsg("开启监听!");
//实力化一个线程上的委托
ThreadStart threadDelegate = new ThreadStart(this.accpet);
//实力化一个处理线程委托的的新线程
threadWatch = new Thread(threadDelegate);
//等同于上面两句newthread = new Thread(new ThreadStart(this.accpet));
threadWatch.IsBackground = true;
threadWatch.Start();
#endregion
}
//消息框里面数据
public void ShowMsg(string str)
{
if (this.tb_infor.InvokeRequired)
{
//实例化一个委托
SetText d = new SetText(ShowMsg);
this.Invoke(d, new object[] { str });
}
else
{
//string Ystr = "";
//if (tb_infor.Text != "")
//{
// Ystr = tb_infor.Text + "\r\n";
//}
//this.tb_infor.Text = Ystr + str + GetCurrentTime();
tb_infor.AppendText(str + GetCurrentTime() + "\r\n");
}
}
Socket SocketConnection = null;//创建一个负责和客户端通信的套接字
public void accpet()
{
while (true)//注意该循环,服务端要持续监听,要不然一个客户端链接过后就无法链接第二个客户端了。
{
//创建一个接收客户端通信的Socket
SocketConnection = socketWatch.Accept();
//如果监听到客户端有链接,则运行到下一部,提示,链接成功!
ShowMsg("链接成功!");
//创建一个通信线程
ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
Thread thr = new Thread(pts);
thr.IsBackground = true;
//启动线程
thr.Start(SocketConnection);
}
}
/// <summary>
/// 向客户端发送信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_sendany_Click(object sender, EventArgs e)
{
string SendMsg = tb_message.Text;
if (SendMsg != "")
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(SendMsg); //将要发送的数据,生成字节数组。
SocketConnection.Send(buffer);
ShowMsg("向客户端发送了:" + SendMsg);
}
}
/// <summary>
/// 获取当前系统时间的方法
/// </summary>
/// <returns>当前时间</returns>
private DateTime GetCurrentTime()
{
DateTime currentTime = new DateTime();
currentTime = DateTime.Now;
return currentTime;
}
/// <summary>
/// 接收客户端发来的信息
/// </summary>
/// <param name="socketClientPara">客户端套接字对象</param>
private void ServerRecMsg(object socketClientPara)
{
Socket socketServer = socketClientPara as Socket;
while (true)
{
//创建一个内存缓冲区 其大小为1024*1024字节 即1M
byte[] arrServerRecMsg = new byte[1024 * 1024 * 2];
//将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
int length = socketServer.Receive(arrServerRecMsg);
//将机器接受到的字节数组转换为人可以读懂的字符串
string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
//将发送的字符串信息附加到文本框txtMsg上
ShowMsg("接受客户端数据:" + strSRecMsg);
}
}
}
}