using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
///
/// Socket接口
///
static class socket
{
//Socket
private static Socket clientSocket;
/// <summary>
/// socket连接
/// </summary>
/// <returns>
/// true 成功
/// false 失败
/// </returns>
public static bool SocketConnect()
{
//从配置文件获取IP
string SocketIP = DAL.Common.ReadConfigString("Recover", "IP");
//从配置文件获取端口
int SocketPort = Convert.ToInt32(DAL.Common.ReadConfigString("Recover", "Port"));
try
{
//创建socket实例
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//创建IP地址
IPAddress IP = IPAddress.Parse(SocketIP);
//创建网络端点
IPEndPoint ipEnd = new IPEndPoint(IP, SocketPort);
//clientSocket.Blocking = false;
//Socket连接
clientSocket.Connect(ipEnd);
if (clientSocket.Connected)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
string strError = "";
strError += "\r\n SocketIP = " + SocketIP.ToString();
strError += "\r\n SocketPort = " + SocketPort.ToString();
DAL.Common.WriteErrorLog(e, strError);
return false;
}
}
/// <summary>
/// Socket发送数据
/// </summary>
/// <param name="strSend">
/// 数据的内容
/// </param>
public static void SocketSend(string strSend)
{
try
{
Byte[] bytesSent = Encoding.UTF8.GetBytes(strSend);
//按byte发送
clientSocket.Send(bytesSent, bytesSent.Length, 0);
}
catch (Exception e)
{
string strError = "";
DAL.Common.WriteErrorLog(e, strError);
}
}
/// <summary>
/// Socket接收数据
/// </summary>
/// <returns>
/// 收到的数据
/// </returns>
public static string SocketReceive()
{
string result = "";
try
{
MemoryStream stream = new MemoryStream();
Byte[] bytesReceived = new Byte[256];
int bytes = 0;
do
{
bytes = clientSocket.Receive(bytesReceived, bytesReceived.Length, 0);
stream.Write(bytesReceived, 0, bytes);
}
while (bytes > 0);
result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
/*string strLogFile = System.AppDomain.CurrentDomain.BaseDirectory + "debug.txt";
StreamWriter sw = new StreamWriter(strLogFile, true);
sw.WriteLine(result);
sw.WriteLine("\n------------------------------------------------------------------------\n");
int len = stream.ToArray().Length;
char[] tempchar = new char[len];
byte[] tempbyte = new byte[len];
tempbyte = stream.ToArray();
for (int i = 0; i < len; i++)
{
tempchar[i] = (char)tempbyte[i];
}
sw.Write(tempchar);
sw.WriteLine("\n------------------------------------------------------------------------\n");
sw.Flush();
sw.Close();*/
//接收完毕后断开连接
clientSocket.Close();
}
catch (Exception e)
{
string strError = "";
DAL.Common.WriteErrorLog(e, strError);
}
return result;
}
/// <summary>
/// Socket通信
/// </summary>
/// <param name="strSend">
/// 发送的信息
/// </param>
/// <returns>
/// 包体的内容
/// </returns>
public static string GetSocketData(string strSend)
{
string strDatas = "";
string strResult = "";
string strExtLength = "";
try
{
//Socket连接
SocketConnect();
//发送信息
SocketSend(strSend);
//接收服务器的信息
strResult = SocketReceive();
//获取扩展信息的长度
strExtLength = strResult.Substring(16, 12);
int ExtLength = Convert.ToInt32(strExtLength);
//扩展信息,暂不使用
//string strExtInfo = strResult.Substring(32, ExtLength);
//获取包体的内容
strDatas = strResult.Substring(ExtLength + 32);
//strDatas = decodedString;
}
catch (Exception e)
{
string strError = "";
strError += "\r\n strResult = " + strResult;
strError += "\r\n strExtLength = " + strExtLength;
DAL.Common.WriteErrorLog(e, strError);
strDatas = "";
}
return strDatas;
}