달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

public class SocketInfo  {     public static String LOGIN_PRE_KEY  = "LOGININFO"// 메인키     public static String LOGIN_NAME_KEY = "USERNAME"// 서브키     public static String LOGIN_ID_KEY   = "USERID"// 서브키     public static String LOGIN_PW_KEY   = "USERPW"// 서브키     public static String LOGIN_AUTO_KEY = "AUTO_LOGIN"; // 서브키 // 서버 정보 public static String SERVER_IP = "IP"; public static int SERVER_PORT  = 포트번호; // 변수 크기 정의 // 로그인     public static int ID_SIZE             = 20;     public static int PW_SIZE             = 20;     public static int USER_NAME_SIZE      = 20;     public static int REQUEST_RESULT_SIZE = 2; // 서버 정보 요청     public static int SERVER_NAME_SIZE = 30;     public static int IP_SIZE          = 30;     public static int DB_NAME_SIZE     = 30;     public static int DB_DATE_SIZE     = 24;     public static int HEADER_SIZE      = 20; // 메세지 정의 // iMsgType     public static int MSGTYPE_PINGPONG                = 1;     public static int MSGTYPE_LOGIN                   = 2;     public static int MSGTYPE_SERVER_CONNECT_STATUS   = 3;     public static int MSGTYPE_SERVER_INFO             = 4;     public static int MSGTYPE_SERVER_STATUS_INFO      = 5;     public static int MSGTYPE_JOIN_STATUS_INFO        = 6;     public static int MSGTYPE_LOGOUT                  = 7;     public static int MSGTYPE_RECENT_JOIN_STATUS_INFO = 8;     public static int MSGTYPE_SYSTEM_STATUS_INFO      = 9;     public static int MSGTYPE_PICAON_JOIN_STATUS_INFO = 10; // iMsgDetailType     public static int MSGDETAILTYPE_REQUEST = 100// 요청     public static int MSGDETAILTYPE_RESULT  = 200// 결과 // 소켓 결과값 전송 플래그(what)     public static int REQUEST_RESULT = 1; // 응답 결과     public static String RESULT_DEF_SUCCESS = "00"// 성공     public static String RESULT_DEF_FAIL    = "01"// 실패 // OS Type     public static int OS_TYPE_WINDOWS = 1// 윈도우     public static int OS_TYPE_MOBLIE  = 2// 모바일 // 프로그램 내 정의     public static int NETWORK_CONNECT_FAIL = 0// 로그인 실패     public static int RECIEVE_BUFFER_SIZE = 86; } public class StatisticController : PicaCast.Web.Controllers.BaseController {     [RequireHttpsHttpPost]     public ActionResult Index(string id, string pwd)     {         List<Entity.StatisticItem> list;         int rowCount;         if (isCertification(id, pwd))         {             list = new BLL.StatisticComponent().GetList(1100out rowCount);         }         else         {             list = new List<Entity.StatisticItem>();         }         return View(list);     }     bool isCertification(string id, string pwd)     {         Socket client;         int port = SocketInfo.SERVER_PORT;         IPAddress hostIp = IPAddress.Parse(SocketInfo.SERVER_IP);         IPEndPoint endPoint = new IPEndPoint(hostIp, port);         byte[] sendBuffer,         receiveBuffer = new byte[SocketInfo.RECIEVE_BUFFER_SIZE];                      using (MemoryStream ms = new MemoryStream())         using (BinaryWriter bw = new BinaryWriter(ms))         {                  //header             bw.Write(SocketInfo.OS_TYPE_MOBLIE);             bw.Write(SocketInfo.MSGTYPE_LOGIN);             bw.Write(SocketInfo.MSGDETAILTYPE_REQUEST);             bw.Write((SocketInfo.OS_TYPE_MOBLIE + SocketInfo.MSGTYPE_LOGIN + SocketInfo.MSGDETAILTYPE_REQUEST + SocketInfo.ID_SIZE + SocketInfo.PW_SIZE + 4% 100);             bw.Write(SocketInfo.ID_SIZE + SocketInfo.PW_SIZE + 4); //body             byte[] idBytes = new byte[SocketInfo.ID_SIZE];             Encoding.GetEncoding("EUC-KR").GetBytes(id, 0, id.Length, idBytes, 0);             byte[] pwdBytes = new byte[SocketInfo.PW_SIZE];             Encoding.GetEncoding("EUC-KR").GetBytes(pwd, 0, pwd.Length, pwdBytes, 0);             bw.Write(idBytes);             bw.Write(pwdBytes);             bw.Write(SocketInfo.ID_SIZE + SocketInfo.PW_SIZE + 4);             sendBuffer = ms.ToArray();         }         try         {             client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                              client.Connect(endPoint);             int ret = client.Send(sendBuffer);             int ret2 = client.Receive(receiveBuffer);             string resultCode = Encoding.GetEncoding("EUC-KR").GetString(receiveBuffer.Where((o, i) => i < 2).ToArray());             string resultName = Encoding.GetEncoding("EUC-KR").GetString(receiveBuffer.Where((o, i) => i >= 22 && i < 42).ToArray());             string resultId   = Encoding.GetEncoding("EUC-KR").GetString(receiveBuffer.Where((o, i) => i >= 42 && i < 62).ToArray());             string resultPwd  = Encoding.GetEncoding("EUC-KR").GetString(receiveBuffer.Where((o, i) => i >= 62).ToArray());             resultCode = resultCode.Substring(0, resultCode.IndexOf('\0'));             resultName = resultName.Substring(0, resultName.IndexOf('\0'));             resultId   = resultId.Substring(0, resultId.IndexOf('\0'));             resultPwd  = resultPwd.Substring(0, resultPwd.IndexOf('\0'));             client.Close();             if (resultPwd.Equals(pwd))                 return true;             else                 return false;            }         catch (Exception)         {             return false;         }     } }

패킷 규약에 맞게 데이터 쌓아서 던지고 받아오면 됨 -_-ㅋ

Posted by 은하비류연
|