using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace SocketApp.Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IPEndPoint ServerInfo;
private Socket ClientSocket;
//信息接收缓存
private Byte[] MsgBuffer;
//信息发送存储
private Byte[] MsgSend;
private void Form1_Load(object sender, EventArgs e)
{
this.CmdSend.Enabled = false;
this.CmdExit.Enabled = false;
//定义一个IPV4,TCP模式的Socket
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
MsgBuffer = new Byte[65535];
MsgSend = new Byte[65535];
//允许子线程刷新数据
CheckForIllegalCrossThreadCalls = false;
this.UserName.Text = Environment.MachineName;
}
private void button1_Click(object sender, EventArgs e)
{
//服务端IP和端口信息设定,这里的IP可以是127.0.0.1,可以是本机局域网IP,也可以是本机网络IP
ServerInfo = new IPEndPoint(IPAddress.Parse(this.ServerIP.Text), Convert.ToInt32(this.ServerPort.Text));
try
{
//客户端连接服务端指定IP端口,Sockket
ClientSocket.Connect(ServerInfo);
//将用户登录信息发送至服务器,由此可以让其他客户端获知
ClientSocket.Send(Encoding.Unicode.GetBytes("用户: " + this.UserName.Text + " 进入系统!\n"));
//开始从连接的Socket异步读取数据。接收来自服务器,其他客户端转发来的信息
//AsyncCallback引用在异步操作完成时调用的回调方法
ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);
this.SysMsg.Text += "登录服务器成功!\n";
this.CmdSend.Enabled = true;
this.CmdEnter.Enabled = false;
this.CmdExit.Enabled = true;
}
catch
{
MessageBox.Show("登录服务器失败,请确认服务器是否正常工作!");
}
}
private void ReceiveCallBack(IAsyncResult AR)
{
try
{
//结束挂起的异步读取,返回接收到的字节数。 AR,它存储此异步操作的状态信息以及所有用户定义数据
int REnd = ClientSocket.EndReceive(AR);
lock (this.RecieveMsg)
{
this.RecieveMsg.AppendText(Encoding.Unicode.GetString(MsgBuffer, 0, REnd));
}
ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
}
catch
{
MessageBox.Show("已经与服务器断开连接!");
this.Close();
}
}
private void CmdSend_Click(object sender, EventArgs e)
{
MsgSend = Encoding.Unicode.GetBytes(this.UserName.Text + "说:\n" + this.SendMsg.Text + "\n");
if (ClientSocket.Connected)
{
//将数据发送到连接的 System.Net.Sockets.Socket。
ClientSocket.Send(MsgSend);
this.SendMsg.Text = "";
}
else
{
MessageBox.Show("当前与服务器断开连接,无法发送信息!");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (ClientSocket.Connected)
{
ClientSocket.Send(Encoding.Unicode.GetBytes(this.UserName.Text + "离开了房间!\n"));
//禁用发送和接受
ClientSocket.Shutdown(SocketShutdown.Both);
//关闭套接字,不允许重用
ClientSocket.Disconnect(false);
}
ClientSocket.Close();
this.CmdSend.Enabled = false;
this.CmdEnter.Enabled = true;
this.CmdExit.Enabled = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyValue == 13)
{
e.Handled = true;
this.CmdSend_Click(this, null);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketApp.Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IPEndPoint ServerInfo;//存放服务器的IP和端口信息
private Socket ServerSocket;//服务端运行的SOCKET
private Thread ServerThread;//服务端运行的线程
private Socket[] ClientSocket;//为客户端建立的SOCKET连接
private int ClientNumb;//存放客户端数量
private byte[] MsgBuffer;//存放消息数据
private void Form1_Load(object sender, EventArgs e)
{
}
//接受客户端连接的方法
private void RecieveAccept()
{
while (true)
{
//Accept 以同步方式从侦听套接字的连接请求队列中提取第一个挂起的连接请求,然后创建并返回新的 Socket。
//在阻止模式中,Accept 将一直处于阻止状态,直到传入的连接尝试排入队列。连接被接受后,原来的 Socket 继续将传入的连接请求排入队列,直到您关闭它。
ClientSocket[ClientNumb] = ServerSocket.Accept();
ClientSocket[ClientNumb].BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None,
new AsyncCallback(RecieveCallBack), ClientSocket[ClientNumb]);
lock (this.ClientList)
{
this.ClientList.Items.Add(ClientSocket[ClientNumb].RemoteEndPoint.ToString() + " 成功连接服务器.");
}
ClientNumb++;
}
}
//回发数据给客户端
private void RecieveCallBack(IAsyncResult AR)
{
try
{
Socket RSocket = (Socket)AR.AsyncState;
int REnd = RSocket.EndReceive(AR);
//对每一个侦听的客户端端口信息进行接收和回发
for (int i = 0; i < ClientNumb; i++)
{
if (ClientSocket[i].Connected)
{
//回发数据到客户端
ClientSocket[i].Send(MsgBuffer, 0, REnd, SocketFlags.None);
}
//同时接收客户端回发的数据,用于回发
RSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket);
}
}
catch { }
}
private int GetPort()
{
return 1234;
}
private void button1_Click(object sender, EventArgs e)
{
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//提供一个 IP 地址,指示服务器应侦听所有网络接口上的客户端活动
IPAddress ip = IPAddress.Any;
ServerInfo = new IPEndPoint(ip, this.GetPort());
ServerSocket.Bind(ServerInfo);//将SOCKET接口和IP端口绑定
ServerSocket.Listen(10);//开始监听,并且挂起数为10
ClientSocket = new Socket[65535];//为客户端提供连接个数
MsgBuffer = new byte[65535];//消息数据大小
ClientNumb = 0;//数量从0开始统计
ServerThread = new Thread(new ThreadStart(RecieveAccept));//将接受客户端连接的方法委托给线程
ServerThread.Start();//线程开始运行
CheckForIllegalCrossThreadCalls = false;//不捕获对错误线程的调用
this.CmdStar.Enabled = false;
this.CmdStop.Enabled = true;
this.StateMsg.Text = "服务正在运行..." + " 运行端口:" + this.GetPort().ToString();
this.ClientList.Items.Add("服务于 " + DateTime.Now.ToString() + " 开始运行.");
}
private void button2_Click(object sender, EventArgs e)
{
ServerThread.Abort();//线程终止
ServerSocket.Close();//关闭socket
this.CmdStar.Enabled = true;
this.CmdStop.Enabled = false;
this.StateMsg.Text = "等待运行...";
this.ClientList.Items.Add("服务于 " + DateTime.Now.ToString() + " 停止运行.");
}
}
}