两个端,一个服务端,一个客户端。
都是控制台程序。
显然地,服务端的要引用superSocket,但引用后编译时候会提示,所以最终引用的内容如图所示:
superSocket内置了log4net,所以会有图中所示。
然后上代码:
服务端:
using System;
using System.Linq;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
namespace superSocketServer
class Program
static AppServer appServer { get; set; }
static void Main(string[] args)
{
appServer = new AppServer();
//Setup the appServer
if (!appServer.Setup(2012)) //Setup with listening port
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
//Try to start the appServer
if (!appServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("The server started successfully, press key 'q' to stop it!");
//1.
appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConnected);
appServer.SessionClosed += appServer_NewSessionClosed;
//2.
appServer.NewRequestReceived += new RequestHandler<AppSession, StringRequestInfo>(appServer_NewRequestReceived);
while (Console.ReadKey().KeyChar != 'q')
{
Console.WriteLine();
continue;
}
//Stop the appServer
appServer.Stop();
Console.WriteLine("The server was stopped!");
Console.ReadKey();
}
//1.
static void appServer_NewSessionConnected(AppSession session)
{
Console.WriteLine($"服务端得到来自客户端的连接成功");
var count = appServer.GetAllSessions().Count();
Console.WriteLine("~~" + count);
session.Send("Welcome to SuperSocket Telnet Server");
}
static void appServer_NewSessionClosed(AppSession session, CloseReason aaa)
{
Console.WriteLine($"服务端 失去 来自客户端的连接" + session.SessionID + aaa.ToString());
var count = appServer.GetAllSessions().Count();
Console.WriteLine(count);
}
//2.
static void
客户端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace superSocketClient
{
class Program
{
static Socket socketClient { get; set; }
static void Main(string[] args)
{
//创建实例
socketClient = new Socket(SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("192.168.0.121");
IPEndPoint point = new IPEndPoint(ip, 2012);
//进行连接
socketClient.Connect(point);
//不停的接收服务器端发送的消息
Thread thread = new Thread(Recive);
thread.IsBackground = true;
thread.Start();
////不停的给服务器发送数据
Thread thread2 = new Thread(Send);
thread2.IsBackground = true;
thread2.Start();
Console.ReadKey();
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
static void Recive()
{
// 为什么用telnet客户端可以,但这个就不行。
while (true)
{
//获取发送过来的消息
byte[] buffer = new byte[1024 * 1024 * 2];
var effective = socketClient.Receive(buffer);
if (effective == 0)
{
break;
}
var str = Encoding.UTF8.GetString(buffer, 0, effective);
Console.WriteLine("来自服务器 --- " + str);
Thread.Sleep(2000);
}
}
static void Send()
{
int i = 0;
int sum = 0;
while (true)
{
i++;
sum += i;
var buffter = Encoding.UTF8.GetBytes($"ADD {sum} {sum + 1}" + "\r\n");
var temp = socketClient.Send(buffter);
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
}
}
以上代码可直接运行,客户端与服务器可以互发消息。
注意这只是一个基本应用,如果需要企业级的,高级一些的应用,则需要
自写 Session、Server,需要继承CommandBase和重载ExecuteCommand。
如:
using System.Linq;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
namespace superSocketServer
{
public class ADD : CommandBase<AppSession, StringRequestInfo>
{
public override void ExecuteCommand(AppSession session, StringRequestInfo requestInfo)
{
session.Send(requestInfo.Parameters.Select(p=>int.Parse(p)).Sum().ToString());
}
}
}
这样的话,客户端给服务器发消息,发诸如 “ ADD 3 4 \r\n” 这样的字符串给服务端,服务端就可以执行上面的这段代码,并且再返回给客户端。
再然后,如果命令是很多的,也不能这样写死,就要使用C#的反射,动态地根据客户端传来的字符串,然后调用服务端的响应函数。再返回给客户端内容。
这里只是说一个基本思路。
具体的格式,由客户端与服务端具体约定。
完毕。