using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
namespace DllScanForFiles
{
class ScanSocket
{
private Socket CurrSocket = null;
//private Machine CurrMac = null;
//public Hsms(Machine mac)
//{
//}
//与设备建立连接
//public bool InitSocket(Machine mac,ref string errorinfo)
//{
// try
// {
// CurrMac = mac;
// if (!ShutDownSocket(ref errorinfo))
// {
// errorinfo = "关闭socket错误:" + errorinfo;
// return false;
// }
// IPAddress ip = IPAddress.Parse(CurrMac.IPAddress);//服务端所在IP
// IPEndPoint ipEnd = new IPEndPoint(ip, CurrMac.FPort);//服务端所监听的接口
// CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
// CurrSocket.Connect(ipEnd);
// ////处理来自机台的包,设置接受超时时间为60秒
// //建立握手,产生握手二进制数组
// byte[] bytes = HsmsUnity.HexStrTobyte(HsmsUnity.SelectReq);
// int result = SendMsg(bytes, ref errorinfo);
// if (result <= 0)
// {
// errorinfo = "发送数据1错误:" + errorinfo;
// return false;
// }
// bytes = ReadData(ref errorinfo);
// if (bytes == null)
// {
// errorinfo = "接受数据1错误:" + errorinfo;
// return false;
// }
// string temphexstr = HsmsUnity.byteToHexStr(bytes, " ");
// if (temphexstr != HsmsUnity.SelectRsq)//说明没有接受到正确的响应包,则初始化不成功
// {
// errorinfo = "建立连接时,没有收到正确的响应包!";
// return false;
// }
// return true;
// }
// catch (Exception ex)
// {
// errorinfo = ex.Message.ToString();
// return false;
// }
//}
public bool InitSocket(string ipaddress, int fport, ref string errorinfo)
{
try
{
if (!ShutDownSocket(ref errorinfo))
{
errorinfo = "关闭socket错误:" + errorinfo;
return false;
}
IPAddress ip = IPAddress.Parse(ipaddress);//服务端所在IP
//IPEndPoint ipEnd = new IPEndPoint(ip, fport);//服务端所监听的接口
IPEndPoint ipEnd = new IPEndPoint(ip, 5999);//服务端所监听的接口
CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
CurrSocket.Connect(ipEnd);
uint dummy = 0;
byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);//启用Keep-Alive
BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//在这个时间间隔内没有数据交互,则发探测包 毫秒
BitConverter.GetBytes((uint)1000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);//发探测包时间间隔 毫秒
CurrSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
CurrSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
////处理来自机台的包,设置接受超时时间为60秒
//CurrSocket.ReceiveTimeout = 1000 * 60;
//CurrSocket.SendTimeout = 1000 * 60;
return true;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return false;
}
}
//关闭连接
public bool ShutDownSocket(ref string errorinfo)
{
try
{
if (CurrSocket == null)
return true;
//if (CurrSocket.Connected)
//{
//}
//CurrSocket.Shutdown(SocketShutdown.Both);
CurrSocket.Close();//关闭Socket
CurrSocket = null;
return true;
}
catch (Exception ex)
{
CurrSocket = null;
errorinfo = ex.Message.ToString();
return false;
}
}
//发送数据
private int SendMsg(byte[] datas, ref string errorinfo)
{
try
{
int result = CurrSocket.Send(datas);
return result;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
errorinfo = "Socket发送数据发生异常,错误信息为:" + errorinfo;
return -1;
}
}
//socket读取数据
private byte[] ReadData(ref string errorinfo)
{
try
{
//uint dummy = 0;
//byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
//BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);//启用Keep-Alive
//BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//在这个时间间隔内没有数据交互,则发探测包 毫秒
//BitConverter.GetBytes((uint)1000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);//发探测包时间间隔 毫秒
//CurrSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
//CurrSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
byte[] allbuffs = new byte[100];
int result = CurrSocket.Receive(allbuffs, 100, SocketFlags.None);
if (result <= 0)
{
errorinfo = "未能从机台上读取数据,可能机台已经断开了。";
return null;
}
byte[] tempbuff = new byte[result];
Array.Copy(allbuffs, 0, tempbuff, 0, result);
return tempbuff;
}
catch (SocketException ex)
{
errorinfo = "Socket接受数据发生异常,错误信息为:" + ex.SocketErrorCode.ToString();
return null;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
errorinfo = "Socket接受数据发生异常,错误信息为:" + errorinfo;
return null;
}
}
private byte[] GetKeepAliveData()
{
uint dummy = 0;
byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
BitConverter.GetBytes((uint)3000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//keep-alive间隔
BitConverter.GetBytes((uint)500).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);// 尝试间隔
return inOptionValues;
}
///
/// 接受设备指令
///
///
///
///
public byte[] ReceiveForBytes(ref string errorinfo)
{
try
{
byte[] receivedatas = ReadData(ref errorinfo);
return receivedatas;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return null;
}
}
public ScanBlock ReceiveForBlock(ref string errorinfo)
{
try
{
byte[] receivedatas = ReadData(ref errorinfo);
if (receivedatas == null)
return null;
string scanstr= System.Text.Encoding.ASCII.GetString(receivedatas);
ScanBlock receiveblock = new ScanBlock(scanstr,"",DateTime.Now);
return receiveblock;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return null;
}
}
public int SendForBytes(byte[] datas, ref string errorinfo)
{
try
{
int result = SendMsg(datas, ref errorinfo);
return result;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public int SetReceiveTimeout(int timeout)
{
CurrSocket.ReceiveTimeout = timeout;
return 1;
}
public int SetSendTimeout(int timeout)
{
CurrSocket.SendTimeout = timeout;
return 1;
}
}
}