using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Sqlite2._0
{
class TCP
{
Socket socketwatch;
bool Thread_Remove = false;
public bool Listening = false;
public Socket socketsend;
public string Received_Text
{
private set;
get;
}
public event EventHandler OnMessageRecievedEvent;
public int TCP_Link(int num)
{
socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Any;
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(num));
socketwatch.Bind(point);
socketwatch.Listen(10);
Thread thread1 = new Thread(Listen);
thread1.IsBackground = true;
if (Thread_Remove == false)
{
thread1.Start(socketwatch);
return 1;
}
else
{
thread1.Abort();
return 0;
}
}
public void Listen(object o)
{
socketwatch = o as Socket;
while (true)
{
socketsend = socketwatch.Accept();
Thread thread2 = new Thread(Received);
thread2.IsBackground = true;
if (Thread_Remove == false)
{
thread2.Start(socketsend);
Listening = true;
}
else
{
thread2.Abort();
}
}
}
public void Received(object o)
{
socketsend = o as Socket;
try
{
while (true)
{
byte[] date = new byte[1024 * 1024 * 3];
int count = socketsend.Receive(date);
Received_Text = Encoding.UTF8.GetString(date, 0, count);
if (OnMessageRecievedEvent != null)
{
OnMessageRecievedEvent(this, EventArgs.Empty);
}
}
}
catch
{
}
}
}
}