UnityMol  0.9.6-875
UnityMol viewer / In developement
UnitySocket.cs
Go to the documentation of this file.
1 
67 {
68 
69 
70  using UnityEngine;
71  using System;
72  using System.Net.Sockets;
73  using System.Net;
74  using System.Collections;
75  using System.Text;
76  using Convert;
77  using Cmd;
78  using UI;
79 
80  public class UnitySocket
81  {
82 
83 
84  public static Socket mSocket = null;
85 
86  public UnitySocket()
87  {
88 
89  }
90 
91 
92  public static void SocketConnection(string LocalIP, int LocalPort)
93  {
94  mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
95  try
96  {
97 
98  IPAddress ip = IPAddress.Parse(LocalIP);
99  IPEndPoint ipe = new IPEndPoint(ip, LocalPort);
100 
101  mSocket.Connect(ipe);
102 
103 // Send("<policy-file-request/>");//the first connect.
104  string s="";
105  System.Random radom= new System.Random();
106  int radomKey=radom.Next(100000,999999);
107  Debug.Log(radomKey);
108  string strKey=radomKey.ToString();
109 
110 // while(s!="NC")
111 // {
112 // Send("<!policy-file-request>");//the second connect.if it's not equals policy head,it will come in the main connect!
113 // s=ReceiveString(2);
114 // Debug.Log(s);
115 // }
116 // s="";
117 // while(s!="NC")
118 // {
119  Send(CommandID.LOGIN);
120 // s=ReceiveString(2);
121 // Debug.Log(s);
122 // }
123 //
124 // Debug.Log("ibpc"+strKey);
125 // while(s!="NC")
126 // {
127  Send("ibpc"+strKey);
128  s=ReceiveString(2);
129 // }
130  Debug.Log(s);
131 
132 
133 // string s=ReceiveString(2);
134  if(s.Equals("OK"))
135  {
136  UIData.loginSucess=true;
137  }
138 
139  else
140  {
141  UIData.loginSucess=false;
142  }
143  //Debug.Log(s.Length);
144  Debug.Log(s);
145  }
146  catch (Exception e)
147  {
148  // Something went wrong, so lets get information about it.
149  Debug.Log(e.ToString());
150  UIData.loginSucess=false;
151  }
152  }
153 
154  public static void Send(short data)
155  {
156  byte[] longth=TypeConvert.getBytes(data,false);
157  mSocket.Send(longth);
158 
159  }
160 
161  public static void Send(long data)
162  {
163  byte[] longth=TypeConvert.getBytes(data,false);
164  mSocket.Send(longth);
165 
166  }
167 
168  public static void Send(int data)
169  {
170  byte[] longth=TypeConvert.getBytes(data,false);
171  mSocket.Send(longth);
172 
173  }
174 
175  public static void Send(string data)
176  {
177  byte[] longth=Encoding.UTF8.GetBytes(data);
178  mSocket.Send(longth);
179 
180  }
181 
182  public static short ReceiveShort()
183  {
184  byte[] recvBytes = new byte[2];
185  mSocket.Receive(recvBytes,2,0);
186  short data=TypeConvert.getShort(recvBytes,true);
187  return data;
188  }
189 
190  public static int ReceiveInt()
191  {
192  byte[] recvBytes = new byte[4];
193  mSocket.Receive(recvBytes,4,0);
194  int data=TypeConvert.getInt(recvBytes,true);
195  return data;
196  }
197 
198  public static long ReceiveLong()
199  {
200  byte[] recvBytes = new byte[8];
201  mSocket.Receive(recvBytes,8,0);
202  long data=TypeConvert.getLong(recvBytes,true);
203  return data;
204  }
205 
206  public static string ReceiveString(int length)
207  {
208  byte[] recvBytes = new byte[length];
209  mSocket.Receive(recvBytes,length,0);
210  string data=Encoding.UTF8.GetString(recvBytes);
211  return data;
212  }
213 
214 
215  }
216 }
static short getShort(byte[] buf, bool asc)
Definition: TypeConvert.cs:135
Definition: CommandID.cs:66
static void Send(short data)
Definition: UnitySocket.cs:154
static int getInt(byte[] buf, bool asc)
Definition: TypeConvert.cs:155
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
static long getLong(byte[] buf, bool asc)
Definition: TypeConvert.cs:175
static void Send(string data)
Definition: UnitySocket.cs:175
static int LOGIN
Definition: CommandID.cs:72
static string ReceiveString(int length)
Definition: UnitySocket.cs:206
static byte[] getBytes(short s, bool asc)
Definition: TypeConvert.cs:78
static void SocketConnection(string LocalIP, int LocalPort)
Definition: UnitySocket.cs:92
static bool loginSucess
Definition: UIData.cs:137
Definition: GUIDisplay.cs:66