234 lines
9.8 KiB
C#
234 lines
9.8 KiB
C#
|
//using System;
|
|||
|
//using System.Net;
|
|||
|
//using System.Net.Sockets;
|
|||
|
//using System.Text;
|
|||
|
//using System.Threading;
|
|||
|
|
|||
|
//using Cosmos.BaseFrame;
|
|||
|
//using Cosmos.UserFrame;
|
|||
|
//using System.IO;
|
|||
|
|
|||
|
//namespace Cosmos.Common
|
|||
|
//{
|
|||
|
// /// <summary>
|
|||
|
// /// TCP 클라이언트 동기화 소켓
|
|||
|
// /// </summary>
|
|||
|
// public class TcpSocketClient
|
|||
|
// {
|
|||
|
// public Socket m_sckClient;
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 서버 IP
|
|||
|
// /// </summary>
|
|||
|
// private string m_IpAddr = "";
|
|||
|
// /// <summary>
|
|||
|
// /// 서버 PORT
|
|||
|
// /// </summary>
|
|||
|
// private int m_PortNo;
|
|||
|
|
|||
|
// public TcpSocketClient()
|
|||
|
// {
|
|||
|
// }
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 소켓 접속 처리
|
|||
|
// /// </summary>
|
|||
|
// /// <param name="sIpAddr"></param>
|
|||
|
// /// <param name="nPortNo"></param>
|
|||
|
// /// <returns></returns>
|
|||
|
// public int Connect(string sIpAddr, int nPortNo)
|
|||
|
// {
|
|||
|
// m_IpAddr = sIpAddr;
|
|||
|
// m_PortNo = nPortNo;
|
|||
|
|
|||
|
// try
|
|||
|
// {
|
|||
|
// IPAddress _IPAddr;
|
|||
|
// if (CmUtil.IsNumber(m_IpAddr.Replace(".", "")) == false)
|
|||
|
// {
|
|||
|
// IPHostEntry hostInfo = Dns.GetHostEntry(m_IpAddr); // Get DNS host information.
|
|||
|
// IPAddress[] IPaddresses = hostInfo.AddressList; // Get the DNS IP addresses associated with the host.
|
|||
|
|
|||
|
// _IPAddr = IPaddresses[0];
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// _IPAddr = IPAddress.Parse(m_IpAddr);
|
|||
|
// }
|
|||
|
// IPEndPoint _EP = new IPEndPoint(_IPAddr, m_PortNo);
|
|||
|
|
|||
|
// Socket tempSocket = new Socket(_EP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
|||
|
// tempSocket.Connect(_EP);
|
|||
|
// if (tempSocket.Connected == false)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
|
|||
|
// string.Format("Connect Fail = {0} : {1}", sIpAddr, nPortNo));
|
|||
|
// return BaseCom.NG;
|
|||
|
// }
|
|||
|
|
|||
|
// m_sckClient = tempSocket;
|
|||
|
|
|||
|
// return BaseCom.OK;
|
|||
|
// }
|
|||
|
// catch (Exception ex)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
|
|||
|
// return BaseCom.NG1;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 소켓 종료 처리
|
|||
|
// /// </summary>
|
|||
|
// public void Close()
|
|||
|
// {
|
|||
|
// try
|
|||
|
// {
|
|||
|
// if (m_sckClient.Connected == true)
|
|||
|
// {
|
|||
|
// m_sckClient.Blocking = false;
|
|||
|
// m_sckClient.Shutdown(SocketShutdown.Both);
|
|||
|
// m_sckClient.Close();
|
|||
|
// }
|
|||
|
// }
|
|||
|
// catch (Exception ex)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
|
|||
|
// }
|
|||
|
|
|||
|
// m_sckClient = null;
|
|||
|
// }
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 데이터 송신 처리
|
|||
|
// /// </summary>
|
|||
|
// /// <param name="byteSenddata"></param>
|
|||
|
// /// <returns></returns>
|
|||
|
// public int SendData(byte[] byteSenddata)
|
|||
|
// {
|
|||
|
// int nByte = 0;
|
|||
|
|
|||
|
// try
|
|||
|
// {
|
|||
|
// if (this.m_sckClient != null)
|
|||
|
// {
|
|||
|
// nByte = this.m_sckClient.Send(byteSenddata, byteSenddata.Length, SocketFlags.None);
|
|||
|
// }
|
|||
|
|
|||
|
// if (nByte != byteSenddata.Length) // 송신실패
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "SendData Fail");
|
|||
|
// return BaseCom.NG;
|
|||
|
// }
|
|||
|
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
|
|||
|
// CmUtil.RPadH("[SEND" + ":" + m_IpAddr + ":" + m_PortNo, 27) + "] " + Encoding.Default.GetString(byteSenddata));
|
|||
|
// return BaseCom.OK;
|
|||
|
// }
|
|||
|
// catch (Exception ex)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
|
|||
|
// return BaseCom.NG1;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 지정길이 데이터 수신 처리
|
|||
|
// /// </summary>
|
|||
|
// /// <param name="byteRecvData"></param>
|
|||
|
// /// <param name="nRecvDataLen"></param>
|
|||
|
// /// <param name="nTimeOut"></param>
|
|||
|
// /// <returns></returns>
|
|||
|
// public int RecvProc(ref byte[] byteRecvData, int nRecvDataLen, int nTimeOut)
|
|||
|
// {
|
|||
|
// int intRecvSize = 0;
|
|||
|
|
|||
|
// try
|
|||
|
// {
|
|||
|
// if (this.m_sckClient != null)
|
|||
|
// {
|
|||
|
// m_sckClient.ReceiveTimeout = nTimeOut;
|
|||
|
|
|||
|
// intRecvSize = m_sckClient.Receive(byteRecvData, nRecvDataLen, SocketFlags.None);
|
|||
|
// }
|
|||
|
|
|||
|
// if (intRecvSize > 0)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
|
|||
|
// CmUtil.RPadH("[RECV" + ":" + m_IpAddr + ":" + m_PortNo, 27) + "] " + Encoding.Default.GetString(byteRecvData, 0, intRecvSize));
|
|||
|
// }
|
|||
|
// }
|
|||
|
// catch (Exception ex)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
|
|||
|
|
|||
|
// }
|
|||
|
// return intRecvSize;
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 데이터 수신 처리( ETX 나올때 까지 수신)
|
|||
|
// /// </summary>
|
|||
|
// /// <param name="byteRecvData"></param>
|
|||
|
// /// <param name="nTimeOut"></param>
|
|||
|
// /// <returns></returns>
|
|||
|
// public int RecvProcEndToETX(ref byte[] byteRecvData, int nTimeOut)
|
|||
|
// {
|
|||
|
// int intRecvSize = 0;
|
|||
|
|
|||
|
// try
|
|||
|
// {
|
|||
|
// if (this.m_sckClient != null)
|
|||
|
// {
|
|||
|
// m_sckClient.ReceiveTimeout = nTimeOut;
|
|||
|
|
|||
|
// while (true)
|
|||
|
// {
|
|||
|
// byte[] byteRecvTemp = new byte[1];
|
|||
|
// int nSize = m_sckClient.Receive(byteRecvTemp, 1, SocketFlags.None);
|
|||
|
// if ( nSize <= 0 )
|
|||
|
// {
|
|||
|
// if (intRecvSize > 0)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
|
|||
|
// CmUtil.RPadH("[RECV FAIL" + ":" + m_IpAddr + ":" + m_PortNo, 27) + "] " + Encoding.Default.GetString(byteRecvData, 0, intRecvSize));
|
|||
|
// }
|
|||
|
// return 0;
|
|||
|
// }
|
|||
|
// byteRecvData[intRecvSize] = byteRecvTemp[0];
|
|||
|
// intRecvSize++;
|
|||
|
|
|||
|
// // ETX 수신시 까지 계속 수신
|
|||
|
// if (byteRecvTemp[0] == 0x03) break;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// if (intRecvSize > 0)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
|
|||
|
// CmUtil.RPadH("[RECV" + ":" + m_IpAddr + ":" + m_PortNo, 27) + "] " + Encoding.Default.GetString(byteRecvData, 0, intRecvSize));
|
|||
|
// }
|
|||
|
// }
|
|||
|
// catch (Exception ex)
|
|||
|
// {
|
|||
|
// UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
|
|||
|
// System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
|
|||
|
|
|||
|
// }
|
|||
|
// return intRecvSize;
|
|||
|
// }
|
|||
|
// }
|
|||
|
//}
|