반응형
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 == nullreturn;
 
        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
반응형

비동기 Socket 사용시 (BeginConnect, EndConnect, BegineSend, EndSend, BeginReceive, EndReceive 매서드 사용시)


socket은 thread를 사용하기에 Receive로 받은 데이터(byte[]) 을 접근할때 유의해야한다.


간혹 동시에 mainThread (응용계층)에서 Receive데이터를 접근시 비동기 socket thread 에서 도 Receive데이터를 접근할수 있다.


위문제가 발생하면 Receive데이터 값을 처리하는데에 문제가 생길수 있다.


아래 lock문 예시로 동기화 처리를 하도록 하자.




lock정의 
- lock은 thread-unsafe(안전하지 않은 스레드)코드를 안전하게 사용하고자 할때 사용한다. 즉, 동기화 처리를 할때 사용하는 구문이다.


lock 사용법은 아래와 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Program
    {
        static object x = new object();
        static void AnotherMethod()
        {
            lock (x) { Console.WriteLine("Another method"); }
        }
        static void Main()
        {
            lock (x)
            {
                AnotherMethod();
               
            }
            Console.Read();
        }
    }
}
cs







필자는 비동기 socket 사용시 lock사용할떄 아래와같이 receive데이터를 접근하엿다.


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
public class SocketAsyncPool 
{
    private Queue<ByteData> m_DataPool;
 
 
    public SocketAsyncPool()
    {
        m_DataPool = new Queue<ByteData>();
    }
 

//들어온 receive데이터를 queue에 담는다.
    public void AddData(ByteData _addData)
    {
//이떄 lock 잠궈서 Enqueue한다.
        lock (m_DataPool)
            m_DataPool.Enqueue(_addData);
    }
 

// mainThread에서 저장된 receive데이터를 사용할떄
    public  ByteData GetData()
    {
//이떄 lock 잠궈서 Dequeue한다.
        lock (m_DataPool)
            return m_DataPool.Dequeue();
    }
 
    public bool ChkDataPool()
    {
        lock (m_DataPool)
            return m_DataPool.Count > 0 ? true : false;
    }
    
}
 

cs



주석 친 설명과같이  

lock (m_DataPool)


Lock 문 안에 처리를 하게되면 socket thread가 m_DataPool를 건들고 있을떄 mainThread는 m_DataPool 건들지 못하고 기다리게 된다.








반응형

'유니티 > 소켓 프로그래밍' 카테고리의 다른 글

[Socket] 소켓클라이언트 구현  (0) 2018.12.18
[Socket] 소켓 메서드관련 설명  (0) 2018.12.14
[Socket] 소켓이란?  (0) 2018.12.14
반응형


    * Socket.Connect()

 - Connect는 동기식이므로 연결이 이루어질 때까지 현재 스레드를 차단합니다.


    * Socket.BeginConnect()

 - BeginConnect는 비동기식입니다. 즉, 다른 스레드에서 연결을 수행하고 연결이 완료되면 이를 알립니다. 현재 호출 스레드를 차단하지 않습니다

   Connect 완료를 받으면 Socket.EndConnect을 호출해야합니다.


*Socket.EndConnect(result);

 - BeginConnect가 호출되고 성공적으로 콜백받게 되면 endConnect호출해야한다. 호출해주지 않으면 연결실패로 인한 재접속으로 불필요한     Connection을 발생하게 됩니다.


* Socket.BeginSend()

 비동기로 데이터 전송


*Socket.endSend()

 비동기 데이터 전송 완료시 호출해야한다.


* Socket.BeginReceive()

 비동기로 데이터 받는다. 성공적으로 콜백이 호출되면 endReceive를 바로 호출해야하고 이후 데이터처리 하면된다.


*socket.endReceive()

비동기 데이터 받기 완료시 호출해야한다.




반응형

'유니티 > 소켓 프로그래밍' 카테고리의 다른 글

[Socket] 소켓클라이언트 구현  (0) 2018.12.18
[Socket] Lock문  (0) 2018.12.18
[Socket] 소켓이란?  (0) 2018.12.14
반응형

소켓이란 ?


- 대상이 멀리 떨어져 있기 때문에 소프트웨어 차원에서 호스트들간에 연결해주는 장치가 필요하고 이러한 기능을 해주는 장치를 소켓(socket)이라고 합니다. 

- TCP , UDP 를 간단하게 다루기 위한 통신 API 이며, 통신의 endPoint (극점)이다.

  간단히 말해 상대와 나를 연결해주는 통로 같은 존재, 전화기? 같은 존재이다.





* TCP 와 UDP 차이


  • TCP(Transmission Control Protocol) 는 두 프로그램 간에 처음 시작될 때부터 끝날 때까지 계속 연결을 유지하는 연결지향(Connection oriented) 방식으로 전화와 비유할 수 있습니다.

 

  • UDP(User Datagram Protocol) 는 명시적으로 연결을 설정하지 않고 데이터를 보내는 방식으로 우편물에 비유할 수 있습니다. 보낸 데이터가 제대로 갔는지, 순서대로 갔는지 동의 여부에 대해 전혀 신경쓰지 않는 신뢰성이 결여된 프로토콜 입니다.  TCP처럼 연결을 하고 끊는 번거로운 작업이 필요 없고, TCP보다 빠르며 데이터의 순서가 별로 중요하지 않고 패킷의 일부가 손상되어도 큰 영양을 미치지 않는 음성이나 영상 데이터를 전송할때 많이 이용되어 집니다.UDP은 메시지의 크기에 약간의 제한이 있으며 메시지의 확실한 전달 역시 보장하지 않으며 통신 중 데이터를 잃어버려도 오류가 발생하지 않습니다.

반응형

'유니티 > 소켓 프로그래밍' 카테고리의 다른 글

[Socket] 소켓클라이언트 구현  (0) 2018.12.18
[Socket] Lock문  (0) 2018.12.18
[Socket] 소켓 메서드관련 설명  (0) 2018.12.14

+ Recent posts