using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System;
|
using System.Collections;
|
using System.Net;
|
using System.Text;
|
using System.Threading;
|
using System.Net.Sockets;
|
|
namespace SA_LTT.Base
|
{
|
public class SocketClient
|
{
|
#region Property
|
/// <summary>
|
/// Client socket
|
/// </summary>
|
private Socket sock;
|
|
/// <summary>
|
/// Receive event handler
|
/// </summary>
|
private ReceiveEventHandler RecevedHandler;
|
|
/// <summary>
|
/// Server ip to be connected
|
/// </summary>
|
public IPAddress Ip { get; private set; }
|
|
/// <summary>
|
/// Server port to be connected
|
/// </summary>
|
public int Port { get; private set; }
|
|
/// <summary>
|
/// Client name
|
/// </summary>
|
public string Name { get; private set; }
|
|
public bool isConnected { get { return sock == null ? false : sock.Connected; } }
|
#endregion
|
|
#region Construct
|
/// <summary>
|
/// Socket client construct
|
/// </summary>
|
/// <param name="ip"> Server ip to be connected</param>
|
/// <param name="port"> Server port to be connected</param>
|
public SocketClient()
|
{
|
this.Ip = IPAddress.Parse("127.0.0.1");
|
this.Port = 8000;
|
this.Name = string.Empty;
|
|
RecevedHandler = new ReceiveEventHandler();
|
}
|
#endregion
|
|
#region Fuction
|
public void ChangeAddress(IPAddress ip, int port)
|
{
|
if (isConnected)
|
{
|
Disconnect();
|
}
|
|
this.Ip = ip;
|
this.Port = port;
|
}
|
|
/// <summary>
|
/// Connect to server
|
/// </summary>
|
/// <returns></returns>
|
public bool Connect()
|
{
|
try
|
{
|
if (sock == null)
|
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
else if (sock.Connected)
|
throw new Exception("socket is already connected.");
|
else
|
{
|
sock.Close();
|
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
}
|
|
IPEndPoint ipep = new IPEndPoint(Ip, Port);
|
|
IAsyncResult async_result = sock.BeginConnect(ipep, new AsyncCallback(ConnectedCallback), sock);
|
|
if (async_result.AsyncWaitHandle.WaitOne(5000, false))
|
{
|
return true;
|
}
|
else
|
{
|
sock.Close();
|
sock = null;
|
return false;
|
}
|
}
|
catch (Exception ex)
|
{
|
return false;
|
}
|
|
}
|
|
/// <summary>
|
/// Add received Event
|
/// </summary>
|
/// <param name="Received_event">Received event</param>
|
/// <returns></returns>
|
public bool Add_received_event(ReceivedEvent Received_event)
|
{
|
try
|
{
|
RecevedHandler.ReceiveEvent += Received_event;
|
return true;
|
}
|
catch (Exception ex)
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// Disconnect to server
|
/// </summary>
|
public void Disconnect()
|
{
|
try
|
{
|
if (sock == null)
|
throw new Exception("socket is already disconnected");
|
|
this.sock.Close();
|
|
sock.Dispose();
|
sock = null;
|
}
|
catch (Exception ex)
|
{
|
}
|
}
|
|
/// <summary>
|
/// Async connected call back
|
/// </summary>
|
/// <param name="ar"></param>
|
private void ConnectedCallback(IAsyncResult ar)
|
{
|
try
|
{
|
Socket client = (Socket)ar.AsyncState;
|
|
if (!client.Connected) return;
|
|
client.EndConnect(ar);
|
sock = client;
|
|
StateObject so = new StateObject(sock);
|
|
so.workSocket.BeginReceive(so.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(RecieveCallback), so);
|
}
|
catch (Exception ex)
|
{
|
}
|
}
|
|
/// <summary>
|
/// Async receieve call back
|
/// </summary>
|
/// <param name="ar"></param>
|
private void RecieveCallback(IAsyncResult ar)
|
{
|
try
|
{
|
StateObject so = (StateObject)ar.AsyncState;
|
|
if (!so.workSocket.Connected) return;
|
|
int byteread = so.workSocket.EndReceive(ar);
|
|
if (byteread > 0)
|
{
|
so.Received_data.Append(Encoding.ASCII.GetString(so.buffer, 0, byteread));
|
RecevedHandler.OnReceived(so);
|
StateObject so2 = new StateObject(so.workSocket);
|
so.workSocket.BeginReceive(so2.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(RecieveCallback), so2);
|
}
|
}
|
catch (Exception ex)
|
{
|
}
|
}
|
|
/// <summary>
|
/// Send data
|
/// </summary>
|
/// <param name="data"></param>
|
/// <param name="clientid"></param>
|
/// <param name="sock"></param>
|
public bool SendData(string data)
|
{
|
try
|
{
|
byte[] buffer = Encoding.Default.GetBytes(data);
|
sock.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), sock);
|
return true;
|
}
|
catch (Exception ex)
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// Send data
|
/// </summary>
|
/// <param name="data"></param>
|
/// <returns></returns>
|
public bool SendData(byte[] data)
|
{
|
try
|
{
|
sock.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), sock);
|
return true;
|
}
|
catch (Exception ex)
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// Async Send call back
|
/// </summary>
|
/// <param name="ar"></param>
|
public void SendCallback(IAsyncResult ar)
|
{
|
try
|
{
|
Socket sock = (Socket)ar.AsyncState;
|
|
int byteSent = sock.EndSend(ar);
|
}
|
catch (Exception ex)
|
{
|
}
|
}
|
#endregion
|
}
|
|
public delegate void ReceivedEvent(object obj, ReceiveEventArgs e);
|
|
public class ReceiveEventHandler
|
{
|
/// <summary>
|
/// Recevied event
|
/// </summary>
|
public event ReceivedEvent ReceiveEvent;
|
|
/// <summary>
|
/// Invoke an ReceiveEvent
|
/// </summary>
|
/// <param name="so">StateObject after received</param>
|
public virtual void OnReceived(StateObject so)
|
{
|
if (ReceiveEvent != null)
|
{
|
ReceiveEvent(this, new ReceiveEventArgs(so));
|
}
|
}
|
}
|
|
public class ReceiveEventArgs : EventArgs
|
{
|
public StateObject stateobject { get; private set; }
|
|
/// <summary>
|
/// Socket이 Received 되었을때 사용
|
/// </summary>
|
/// <param name="so">Received 된 후의 StateObject 값</param>
|
public ReceiveEventArgs(StateObject stateobject)
|
{
|
this.stateobject = stateobject;
|
}
|
}
|
|
public class StateObject
|
{
|
/// <summary>
|
/// Received Sockect
|
/// </summary>
|
public Socket workSocket = null;
|
|
/// <summary>
|
/// Maximum size that can be received
|
/// </summary>
|
public const int BufferSize = 256;
|
|
/// <summary>
|
/// Received buffer
|
/// </summary>
|
public byte[] buffer = new byte[BufferSize];
|
|
/// <summary>
|
/// Received data string
|
/// </summary>
|
public StringBuilder Received_data = new StringBuilder();
|
|
public StateObject(Socket sock)
|
{
|
workSocket = sock;
|
}
|
}
|
}
|