Skip to content

Instantly share code, notes, and snippets.

@CipherLab
Created July 11, 2014 13:13
Show Gist options
  • Save CipherLab/22c8a7113be1e0ec2398 to your computer and use it in GitHub Desktop.
Save CipherLab/22c8a7113be1e0ec2398 to your computer and use it in GitHub Desktop.
Socket connector class
using System;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using Timer = System.Windows.Forms.Timer;
using System.Diagnostics;
namespace CustomSocketConnector
{
public class mytcp : TcpClient
{
public bool is_active;
public mytcp(string address, int port)
: base(address, port)
{ }
public void UsingProtectedMethods()
{
if (this.Active)
{
is_active = true;
//Socket s = this.Server;
//LingerOption lingerOption = new LingerOption(true, 10);
//s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
}
else
{
is_active = false;
}
}
}
public static class locker
{
public static object locked = new object();
}
public class SocketConnector
{
public int ListenerID = 0;
public IPAddress IP;
public int port ;
public int PortReadDelay = 10;
private int ThreadInterval = 100;
public Socket socket;
public bool Stopped = false;
public bool Pause = false;
public bool IsPaused = false;
public bool port_error = false;
//public mytcp tcp;
private bool StopListener = false;
private System.Threading.Timer ThreadTimer;
private AutoResetEvent autoEvent = new AutoResetEvent(false);
private TimerCallback timerDelegate;
public SocketConnector()
{
timerDelegate = new TimerCallback(this.ThreadCommTimer_Tick);
ThreadTimer = new System.Threading.Timer(timerDelegate, autoEvent, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
public void Dispose()
{
ThreadTimer.Dispose();
}
public string Connect(string IP, int port_num)
{
//tcp = new mytcp(IP, port_num);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(IP), port_num);
socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
//IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(IP), port_num);
//socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
//socket.Connect(ipe);
//SocketTimer.Tick += new EventHandler(SocketTimer_Tick);
//SocketTimer.Interval = PortReadDelay;
//SocketTimer.Enabled = true;
ThreadTimer.Change(ThreadInterval, ThreadInterval);
try
{
//tcp.Start();
}
catch (Exception ex)
{
if (ex.Message.ToString() == "The requested address is not valid in its context")
{
return ("Invalid IP address. You may not be able to send or receive data through the ports.");
}
else
{
EventLogger.Log(ex.Message.ToString(),"GNPService");
}
}
finally
{
//tcp.UsingProtectedMethods();
}
return "";
}
public void StopListening()
{
StopListener = true;
Application.DoEvents();
}
public bool Connected = false;
private void ThreadCommTimer_Tick(Object stateInfo)
{
try
{
ThreadTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
catch
{
}
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
if (socket == null)
{
Connected = false;
}
else if (!socket.Connected)
{
Connected = false;
}
else
{
Connected = true;
}
//if (tcp.is_active)
//{
if (socket != null)
{
if (socket.Connected)
{
if (socket.Available > 0)
{
bool pulling_from_socket = true;
List<byte> buffer = new List<byte>();
byte[] bArray = new byte[0];
try
{
while (pulling_from_socket)
{
if (socket.Available > 0)
{
byte[] from_socket = new byte[socket.Available];
socket.Receive(from_socket);
buffer.AddRange(from_socket);
}
else
{
pulling_from_socket = false;
}
System.Threading.Thread.Sleep(100);
}
bArray = buffer.ToArray();
}
catch (Exception ex)
{
EventLogger.Log("ThreadCommTimer_Tick:1-" + ex.Message, "GNPService");
}
lock (locker.locked)
{
try
{
var endpoint = ((IPEndPoint)socket.LocalEndPoint);
DataAvailable(bArray, ListenerID, endpoint.Port);
StringAvailable(System.Text.Encoding.Unicode.GetString(bArray, 0, bArray.Length), ListenerID);
}
catch (Exception ex)
{
EventLogger.Log("ThreadCommTimer_Tick:2-" + ex.Message, "GNPService");
}
}
}
}
}
//}
//else
//{
// port_error = true;
//}
if (!StopListener)
{
autoEvent.Set();
ThreadTimer.Change(ThreadInterval, ThreadInterval);
}
else
{
//close
}
}
public delegate void dlgDataAvailable(byte[] DataReceived, int SocketID, int SocketNum);
public event dlgDataAvailable DataAvailable;
public delegate void dlgStringDataAvailable(string StringReceived, int SocketID);
public event dlgStringDataAvailable StringAvailable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment