继续C# SuperSocket服务端入门一善功能,实现字符串的收发代码如下
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 SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
namespace superSocketTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
AppServer appServer;//定义socket服务器对像
Dictionary<string, AppSession> ClientSession = new Dictionary<string, AppSession>();
private void Form1_Load(object sender, EventArgs e)
{
appServer = new AppServer();
ServerConfig serverconfig = new ServerConfig();
serverconfig.Port = int.Parse(txt_ServerPort.Text);
appServer.Setup(serverconfig);//注册9004端口服务
appServer.NewRequestReceived += appServer_NewRequestReceived; //数据接收事件
appServer.NewSessionConnected += appServer_NewSessionConnected;//监听客户端连接事件
appServer.SessionClosed += appServer_SessionClosed;//监听客户端断开事件
}
private void appServer_SessionClosed(AppSession session, SuperSocket.SocketBase.CloseReason value)
{
string ipstr = session.RemoteEndPoint.ToString();
lsbox_Clientlist.Invoke(new Action(()=> {
if (lsbox_Clientlist.FindString(ipstr) >= 0)
lsbox_Clientlist.Items.RemoveAt((lsbox_Clientlist.Items.IndexOf(ipstr)));
}));
if (ClientSession.ContainsKey(ipstr))
{
ClientSession.Remove(ipstr);
}
AddmsgInfromatioin("客户端:"+ipstr +"断开");
}
private void appServer_NewSessionConnected(AppSession session)
{
string ipstr = session.RemoteEndPoint.ToString();
lsbox_Clientlist.Invoke(new Action(()=>{ lsbox_Clientlist.Items.Add(ipstr); }));
ClientSession.Add(ipstr,session);
AddmsgInfromatioin("客户端:" + ipstr + "连接");
}
private void appServer_NewRequestReceived(AppSession session, StringRequestInfo requestInfo)
{
string ipstr = session.RemoteEndPoint.ToString();
AddmsgInfromatioin("客户端:" + ipstr + "接收:"+ requestInfo.Key);
}
//开启服务 按钮点击事件
private void btn_ServerStart_Click(object sender, EventArgs e)
{
if (appServer.Start())
{
AddmsgInfromatioin("服务启动成功");
}
else
AddmsgInfromatioin("服务启动失败,端口可能被占用!");
}
//停止服务 按钮点击事件
private void btn_serverStop_Click(object sender, EventArgs e)
{
appServer.Stop();
AddmsgInfromatioin("服务停止");
}
public string msgold = "";
public void AddmsgInfromatioin(string msg)
{
if (txtlist.IsDisposed)
return;
if (msg != msgold)
{
if (txtlist.InvokeRequired)
{
txtlist.Invoke(new EventHandler(delegate
{
txtlist.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + msg + "\r");
msgold = msg;
if (txtlist.Lines.Length > 1000)
{
txtlist.Select(1, txtlist.GetFirstCharIndexFromLine(1));
txtlist.SelectedText = "";
}
if (ck_autoBootom.Checked)
txtlist.ScrollToCaret();
}));
}
else
{
txtlist.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + msg + "\r");
msgold = msg;
if (txtlist.Lines.Length > 1000)
{
txtlist.Select(1, txtlist.GetFirstCharIndexFromLine(1));
txtlist.SelectedText = "";
}
if (ck_autoBootom.Checked)
txtlist.ScrollToCaret();
}
}
}
private void btn_send_Click(object sender, EventArgs e)
{
if (lsbox_Clientlist.SelectedItem != null)
{
msgold = "";
string ipstr = lsbox_Clientlist.SelectedItem.ToString();
string value = txt_SendText.Text;// + "\r\n";
ClientSession[ipstr].Send(value);
AddmsgInfromatioin("发送内容:"+ value);
}
}
}
}