📄 program.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using JLL.SGIP;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Diagnostics;
namespace Test
{
public class StateObject
{
public Socket WorkSocket;
public byte[] Buffer = new byte[2048];
}
class Program
{
static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static void Test(object state)
{
Console.WriteLine("Test...Send之前!");
s.Send(Command.CreateCommand(new Unbind()).GetBytes());
Console.WriteLine("Test...send完毕!");
byte[] buffer = new byte[1024];
int nRead = s.Receive(buffer);
Console.WriteLine("test...Receive完毕");
Console.WriteLine("读到了{0}个字节", nRead);
}
private static void OnReceivData(IAsyncResult ar)
{
try
{
Console.WriteLine("OnReceiveData....");
StateObject obj = (StateObject)ar.AsyncState;
int n = obj.WorkSocket.EndReceive(ar);
if (n > 0)
{
try
{
Command cmd = Command.ToCommand(obj.Buffer, 0);
if (cmd.Body is Unbind)
{
Console.WriteLine("收到UnBind命令!");
}
else
{
Console.WriteLine("收到了命令ID:{0}", cmd.Head.CommandID);
}
Console.WriteLine(cmd.Head.CommandID);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(obj.Buffer));
}
catch
{
}
for (int i = 0; i < 2048; ++i)
obj.Buffer[i] = 0;
obj.WorkSocket.BeginReceive(obj.Buffer, 0, 2048, SocketFlags.None, new AsyncCallback(OnReceivData), obj);
}
else
{
Console.WriteLine("服务器断开了!");
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
private static void abcd()
{
try
{
Console.WriteLine(s.GetType().FullName);
throw new NotSupportedException("erroro!");
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}
private static void WriteBytes(byte[] buffer)
{
for (int i = 0; i < buffer.Length; ++i)
{
Console.Write(string.Format("{0:X2}", buffer[i]));
Console.Write(",");
}
}
class Data
{
private IPEndPoint _ep;
private DateTime _dt;
public Data(IPEndPoint ep)
{
_ep = ep;
_dt = DateTime.Now;
}
public IPEndPoint IPEndPoint
{
get { return _ep; }
}
public DateTime DateTime
{
get { return _dt; }
}
}
static void Main(string[] args)
{
#region 测试代码1
/*
string str = "测试MT消息!";//测试MT消息!
Console.WriteLine("\nascii:");
WriteBytes(Encoding.ASCII.GetBytes(str));
Console.WriteLine("\nutf-7:");
WriteBytes(Encoding.UTF7.GetBytes(str));
Console.WriteLine("\nutf-8:");
WriteBytes(Encoding.UTF8.GetBytes(str));
Console.WriteLine("\nutf-32:");
WriteBytes(Encoding.UTF32.GetBytes(str));
Console.WriteLine("\nbig-序列utf-16, unicode:");
WriteBytes(Encoding.BigEndianUnicode.GetBytes(str));
Console.WriteLine("\nlittle-序列utf-16, unicode:");
WriteBytes(Encoding.Unicode.GetBytes(str));
Console.WriteLine("\ngb180310:");
WriteBytes(Encoding.GetEncoding("GB18030").GetBytes(str));
byte[] buff = Encoding.BigEndianUnicode.GetBytes(str);
Console.WriteLine(Encoding.GetEncoding("GB18030").GetString(buff));
Console.Read();
return;
*/
/*
s.Connect(IPAddress.Parse("127.0.0.1"), 8801);
StateObject obj = new StateObject();
obj.WorkSocket = s;
s.BeginReceive(obj.Buffer, 0, 2048, SocketFlags.None, new AsyncCallback(OnReceivData), obj);
// new Timer(new TimerCallback(Test), null, 0, 500000);
Console.ReadLine();
s.Shutdown(SocketShutdown.Both);
s.Close();
abcd();
Console.Read();
return;
*/
#endregion
#region test code 2
/*
Dictionary<IPEndPoint, int> dict = new Dictionary<IPEndPoint, int>();
IPEndPoint e1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000);
IPEndPoint e2 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000);
IPEndPoint e3 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000);
dict[e1] = 10;
dict[e2] = 20;
dict[e3] = 30;
Console.WriteLine("{0}\t{1}\t{2}", dict[e1], dict[e2], dict[e3]);
Dictionary<IPAddress, int> t = new Dictionary<IPAddress, int>();
IPAddress s1 = IPAddress.Parse("127.0.0.1");
IPAddress s2 = IPAddress.Parse("127.0.0.1");
t[s1] = 10;
t[s2] = 20;
Console.WriteLine("{0}\t{1}", t[s1], t[s2]);
Dictionary<string, Data> _dict = new Dictionary<string, Data>(128);
_dict["1"] = new Data(new IPEndPoint(IPAddress.Parse("192.168.1.168"), 8801));
_dict["2"] = new Data(new IPEndPoint(IPAddress.Parse("192.168.1.168"), 8802));
_dict["3"] = new Data(new IPEndPoint(IPAddress.Parse("192.168.1.168"), 8803));
_dict["4"] = new Data(new IPEndPoint(IPAddress.Parse("192.168.1.168"), 8804));
foreach (KeyValuePair<string, Data> kvp in _dict)
{
Console.WriteLine("{0}==={1}, {2}", kvp.Key, kvp.Value.IPEndPoint, kvp.Value.DateTime);
}
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now + new TimeSpan(1, 1, 1);
TimeSpan t = dt2 - dt1;
Console.WriteLine(dt1 - dt2);
Console.Read();
return;
*/
#endregion
SubmitListenServer server = new SubmitListenServer();
DeliverListenServer dls = new DeliverListenServer();
try
{
server.Start();
dls.Start();
Console.WriteLine("server is staring, press any key to exit....");
Console.Read();
}
catch (InvalidOperationException e)
{
System.Diagnostics.Debug.WriteLine(e);
Console.WriteLine("shit!:" + e.Message);
Console.Read();
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
Console.WriteLine(e.Message);
Console.Read();
}
finally
{
server.Stop();
dls.Stop();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -