using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using Recognize.RecognizeCard;
using Recognize.Util;
namespace Recognize.Net
{
class CardsCenter
{
private readonly Socket _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private readonly Dictionary<Socket, List<Card>> _cards = new Dictionary<Socket, List<Card>>();
public bool Start(ushort port, ushort maxClients = 3)
{
try
{
_listener.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port));
_listener.Listen(maxClients);
PostAsyncAccept();
}
catch (Exception)
{
return false;
}
return true;
}
public List<Card> GetAllCards()
{
var cards = new List<Card>();
foreach (var cardInfo in _cards)
{
cards.AddRange(cardInfo.Value);
}
return cards;
}
protected void PostAsyncAccept()
{
var acceptSae = new SocketAsyncEventArgs();
acceptSae.Completed += AcceptCompelete;
_listener.AcceptAsync(acceptSae);
}
protected void AcceptCompelete(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError != SocketError.Success)
{
return;
}
PostAsyncReceive(e.AcceptSocket);
PostAsyncAccept();
}
protected void PostAsyncReceive(Socket sReceive)
{
var recieveSae = new SocketAsyncEventArgs();
var buffer = new byte[2048];
recieveSae.SetBuffer(buffer, 0, buffer.Length);
recieveSae.Completed += RecieveComplete;
sReceive.ReceiveAsync(recieveSae);
}
protected void RecieveComplete(object sender, SocketAsyncEventArgs e)
{
var sReceive = sender as Socket ?? _listener;
var data = e.Buffer;
var card = UtilCard.DeserializeCard(data);
if (_cards.ContainsKey(sReceive))
{
_cards[sReceive].Add(card);
}
else
{
_cards.Add(sReceive, new List<Card> {card});
}
PostAsyncReceive(sReceive);
}
}
}
Client主要代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Recognize.RecognizeCard;
using Recognize.Util;
namespace Recognize.Net
{
class Client
{
private readonly Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public bool Connect(string serverIP, ushort serverPort)
{
try
{
_socket.Connect(IPAddress.Parse(serverIP), serverPort);
}
catch (Exception)
{
return false;
}
return true;
}
public bool Disconnect()
{
try
{
_socket.Close();
}
catch (Exception)
{
return false;
}
return true;
}
public bool SendCard(List<Card> cards)
{
try
{
cards.ForEach(card =>
{
_socket.Send(UtilCard.SerializeCard(card));
Thread.Sleep(10);
});
}
catch (Exception)
{
return false;
}
return true;
}
}
}
序列化主要代码:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Recognize.RecognizeCard;
namespace Recognize.Util
{
class UtilCard
{
public static byte[] SerializeCard(Card card)
{
var bf = new BinaryFormatter();
var ms = new MemoryStream();
bf.Serialize(ms, card);
return ms.ToArray();
}
public static Card DeserializeCard(byte[] cardData)
{
var bf = new BinaryFormatter();
var ms = new MemoryStream();
ms.Write(cardData, 0, cardData.Length);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
try
{
return bf.Deserialize(ms) as Card;
}
catch (Exception)
{
return null;
}
}
}
}