132 lines
4.1 KiB
C#
132 lines
4.1 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
using CommonClassLib;
|
|||
|
namespace SocketsTCPIP
|
|||
|
{
|
|||
|
public static class CClientTCPIP
|
|||
|
{
|
|||
|
static int _initCount=0;
|
|||
|
static bool _ifInit = false;
|
|||
|
static string _remoteIP;
|
|||
|
static int _remotePort;
|
|||
|
static Socket clientSocket;
|
|||
|
static EndPoint epServer;
|
|||
|
static byte[] _byteData = new byte[16];
|
|||
|
static string _tcpError;
|
|||
|
|
|||
|
public static string TcpError
|
|||
|
{
|
|||
|
get { return _tcpError; }
|
|||
|
set { _tcpError = value; }
|
|||
|
}
|
|||
|
public static byte[] ByteData
|
|||
|
{
|
|||
|
get { return _byteData; }
|
|||
|
set { _byteData = value; }
|
|||
|
}
|
|||
|
public static bool InitClientTCPIP()
|
|||
|
{
|
|||
|
if (_initCount > 3)
|
|||
|
{
|
|||
|
_tcpError = "<22><><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!<21><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3>";
|
|||
|
return false;
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
//Using TCPIP sockets
|
|||
|
clientSocket = new Socket(AddressFamily.InterNetwork,
|
|||
|
SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
|||
|
//IP address of the server machine
|
|||
|
IPAddress ipAddress = IPAddress.Parse(_remoteIP);
|
|||
|
//Server is listening on port 1000
|
|||
|
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _remotePort);
|
|||
|
|
|||
|
epServer = (EndPoint)ipEndPoint;
|
|||
|
//Login to the server
|
|||
|
clientSocket.Connect(epServer);
|
|||
|
_ifInit = true;
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
if(_initCount>3)
|
|||
|
{
|
|||
|
_tcpError = "<22><><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!<21><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3>";
|
|||
|
return false;
|
|||
|
}
|
|||
|
_initCount++;
|
|||
|
_ifInit = false;
|
|||
|
_tcpError = ex.Message;
|
|||
|
return false;
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
public static byte[] ConnectTCPIP(byte[] byteData, string remoteip, int remoteport)
|
|||
|
{
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
_remoteIP = remoteip;
|
|||
|
_remotePort = remoteport;
|
|||
|
if (_ifInit == false)
|
|||
|
{
|
|||
|
if (InitClientTCPIP() == false)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
clientSocket.Send(byteData);
|
|||
|
CCarryConvert.WriteDarkCasket("CClientTCPIP", "Send", remoteip + ":" + remoteport, byteData);
|
|||
|
clientSocket.Receive(_byteData);
|
|||
|
CCarryConvert.WriteDarkCasket("CClientTCPIP", "Receive", remoteip + ":" + remoteport, _byteData);
|
|||
|
return _byteData;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_tcpError= ex.Message ;
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
public static byte[] ConnectTCPIP(string strData, string remoteip, int remoteport)
|
|||
|
{
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
_remoteIP = remoteip;
|
|||
|
_remotePort = remoteport;
|
|||
|
if (_ifInit == false)
|
|||
|
{
|
|||
|
if (InitClientTCPIP() == false)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
byte[] byteData;
|
|||
|
byteData = Encoding.UTF8.GetBytes(strData);
|
|||
|
clientSocket.Send(byteData);
|
|||
|
CCarryConvert.WriteDarkCasket("CClientTCPIP", "Send", remoteip + ":" + remoteport, byteData);
|
|||
|
clientSocket.Receive(_byteData);
|
|||
|
CCarryConvert.WriteDarkCasket("CClientTCPIP", "Receive", remoteip + ":" + remoteport, _byteData);
|
|||
|
return _byteData;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_tcpError= ex.Message ;
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
public static void CloseSockets()
|
|||
|
{
|
|||
|
clientSocket.Shutdown(SocketShutdown.Both);
|
|||
|
clientSocket.Close();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|