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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net.Sockets; using System; using System.Net; public class SocketClient { public delegate void ReceiveDataPocess(byte[] packetData); public delegate void ErrorDataPocess(); ReceiveDataPocess receiveProcess; ErrorDataPocess errorProcess; private Socket m_Socket; //리시브 private byte[] BeginReceiveData; public SocketClient(ReceiveDataPocess _revProcess, ErrorDataPocess _errorProcess) { receiveProcess = _revProcess; errorProcess = _errorProcess; } #region Conneting public void StartConnect() { try { m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string hostIp = "192.111.1.1"; int port = 11111; IPAddress curAdd = null; if (IPAddress.TryParse(hostIp, out curAdd)) { Debug.Log("유효한 IP 입니다"); m_Socket.BeginConnect(hostIp, port, Callback_ConnectEnd, null); } } catch(System.Exception ex) { ErrorException(ex); } } void Callback_ConnectEnd(IAsyncResult result) { try { m_Socket.EndConnect(result); //리시브 시작 Begin_Recieve(); Debug.Log("Socket Connect 성공 : " + m_Socket.Connected); } catch (System.Exception ex) { ErrorException(ex); } } #endregion #region Recieving int BeginReceiveDataLength = 1024; void Begin_Recieve() { try { if(m_Socket != null) { BeginReceiveData = new byte[BeginReceiveDataLength]; //서버로 부터 받을 데이터 크기 m_Socket.BeginReceive(BeginReceiveData, 0, BeginReceiveData.Length, SocketFlags.None, Callback_Received, BeginReceiveData); } } catch (System.Exception ex) { ErrorException(ex); } } void Callback_Received(IAsyncResult result) { if (m_Socket == null || !m_Socket.Connected) return; try { SocketError socketErr; int endReceiveLength = m_Socket.EndReceive(result,out socketErr); if(endReceiveLength <= 0) { //다시 beginReceive Begin_Recieve(); return; } if(socketErr == SocketError.Success) { byte[] receivedBuffer = (byte[])result.AsyncState; //리시브 데이터 처리 ProcessRecievedData(endReceiveLength, receivedBuffer); //다시 beginReceive Begin_Recieve(); } else { Debug.LogError("Recieve Error : " + socketErr.ToString()); return; } } catch (System.Exception ex) { ErrorException(ex); } } //리시브 데이터 처리 void ProcessRecievedData(int _endReceivedBufferLength, byte[] _receivedBuffer) { try { Debug.LogError("Recieved Success , endReceiveLength : " + _endReceivedBufferLength); //실질 데이터 처리 receiveProcess(_receivedBuffer); } catch (System.Exception ex) { ErrorException(ex); } } #endregion #region Sending public void Send_ProtocolData( byte[] sendBodyData) { try { if(m_Socket.Connected && m_Socket != null) { //비동기로 데이터 보내기 m_Socket.BeginSend(sendBodyData, 0, sendBodyData.Length, SocketFlags.None, Callback_SendProtocolData, null); } } catch (System.Exception ex) { ErrorException(ex); } } void Callback_SendProtocolData(IAsyncResult result) { try { m_Socket.EndSend(result); } catch (System.Exception ex) { ErrorException(ex); } } #endregion #region Error Exeption // ============= Error Exeption =============== void ErrorException(Exception ex) { Debug.LogError("Error_in Socket Processing : " + ex.ToString()); errorProcess(); DisConnect(); } #endregion #region Disconnect public void DisConnect() { if (m_Socket == null) return; if(m_Socket.Connected) m_Socket.Shutdown(SocketShutdown.Both); // 소켓을 비활성화 시킵니다. m_Socket.Close(); //소켓을 닫습니다. Debug.LogError("Close networking"); } #endregion } | cs |
'유니티 > 소켓 프로그래밍' 카테고리의 다른 글
[Socket] Lock문 (0) | 2018.12.18 |
---|---|
[Socket] 소켓 메서드관련 설명 (0) | 2018.12.14 |
[Socket] 소켓이란? (0) | 2018.12.14 |