163 lines
4.9 KiB
C#
163 lines
4.9 KiB
C#
using System.Net;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using System.Threading;
|
||
//using CommonClassLib;
|
||
namespace SocketsTCPIPServer
|
||
{
|
||
|
||
public class CServerTCPIP
|
||
{
|
||
Thread mythread;
|
||
Socket socket;
|
||
Socket AcceptSocket;
|
||
public Socket ClientSocket;
|
||
string _returnData;
|
||
string _tcpServerError;
|
||
byte[] _revMessage;
|
||
|
||
public byte[] RevMessage
|
||
{
|
||
get { return _revMessage; }
|
||
set { _revMessage = value; }
|
||
}
|
||
public string TcpServerError
|
||
{
|
||
get { return _tcpServerError; }
|
||
set { _tcpServerError = value; }
|
||
}
|
||
public string ReturnData
|
||
{
|
||
get { return _returnData; }
|
||
set { _returnData = value; }
|
||
}
|
||
public CServerTCPIP()
|
||
{
|
||
try
|
||
{
|
||
IPAddress ServerIp = GetServerIP();
|
||
IPEndPoint iep = new IPEndPoint(ServerIp, 1000);
|
||
socket = new
|
||
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
|
||
if (socket.Connected == false)
|
||
socket.Bind(iep);
|
||
|
||
}
|
||
catch (System.Exception er)
|
||
{
|
||
_tcpServerError= er.Message;
|
||
}
|
||
}
|
||
|
||
public static IPAddress GetServerIP()
|
||
{
|
||
|
||
return IPAddress.Parse("172.18.136.252");
|
||
}
|
||
public void StartListen()
|
||
{
|
||
mythread = new Thread(new ThreadStart(BeginListen));
|
||
mythread.Start();
|
||
}
|
||
public void EndListen()
|
||
{
|
||
|
||
mythread.Abort();
|
||
}
|
||
private void BeginListen()
|
||
{
|
||
byte[] byteMessage = new byte[12];
|
||
|
||
while (true)
|
||
{
|
||
try
|
||
{
|
||
socket.Listen(50);
|
||
if (AcceptSocket==null )
|
||
{
|
||
AcceptSocket = socket.Accept();
|
||
ClientSocket = AcceptSocket;
|
||
}
|
||
|
||
|
||
AcceptSocket.Receive(byteMessage);
|
||
//_revMessage = byteMessage;
|
||
////CCarryConvert.WriteDarkCasket(this.ToString(), "Receive", newSocket.RemoteEndPoint.ToString(), byteMessage);
|
||
///////////////////////////////////
|
||
////<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
|
||
DealWithReceive(byteMessage, AcceptSocket);
|
||
|
||
}
|
||
catch (SocketException ex)
|
||
{
|
||
AcceptSocket.Close();
|
||
AcceptSocket = null;
|
||
_tcpServerError= ex.Message ;
|
||
}
|
||
}
|
||
}
|
||
void DealWithReceive(byte[] byteMessage, Socket newSocket )
|
||
{
|
||
try
|
||
{
|
||
//<2F><><EFBFBD>ֿͻ<D6BF><CDBB><EFBFBD>byteMessage<67><65>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>
|
||
byte[] rdata = new byte[6] ;
|
||
int task = byteMessage[3]* 256 + byteMessage[2];
|
||
if (byteMessage[0] == 72)
|
||
{
|
||
rdata[0] = 82;
|
||
rdata[1] = 0;
|
||
rdata[2] = byteMessage[2];
|
||
rdata[3] = byteMessage[3];
|
||
rdata[4] = 0;
|
||
rdata[5] =0;
|
||
newSocket.Send(rdata);
|
||
Thread.Sleep(10);
|
||
rdata[0] = 71;
|
||
rdata[1] = 0;
|
||
rdata[2] = byteMessage[2];
|
||
rdata[3] = byteMessage[3];
|
||
if (byteMessage[8]==255)
|
||
{
|
||
|
||
if(task==7586)
|
||
{
|
||
rdata[4] = 9;
|
||
}
|
||
if (task == 7574)
|
||
{
|
||
rdata[4] = 8;
|
||
}
|
||
if (task == 7580)
|
||
{
|
||
rdata[4] = 7;
|
||
}
|
||
if (task == 7592)
|
||
{
|
||
rdata[4] = 6;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
rdata[4] = byteMessage[8];
|
||
}
|
||
rdata[5] = 0;
|
||
newSocket.Send(rdata);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
|
||
//CCarryConvert.WriteDarkCasket(this.ToString(), "Send", newSocket.RemoteEndPoint.ToString(), rdata);
|
||
//newSocket.Shutdown(SocketShutdown.Both);
|
||
//newSocket.Close();
|
||
_returnData = Encoding.ASCII.GetString(byteMessage);
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
_tcpServerError = ex.Message;
|
||
}
|
||
}
|
||
}
|
||
}
|