¾È³çÇϼ¼¿ä. TCP ¼ÒÄÏÅë½ÅÀ»ÀÌ¿ëÇÏ¿© ClientºÎºÐÀ» ÀÛ¼ºÇÏ°íÀִµ¥ ºñµ¿±â Recive°¡ ¾ÈµÇ´Â Çö»óÀÌÀÖ¾î ¹®Àǵ帳´Ï´Ù.
¾Æ¿¹ ¾ÈµÇ´Â°ÍÀº ¾Æ´Ï°í, Çö»óÀº ¾Æ·¡¿Í °°½À´Ï´Ù.
1. Á¦°¡ ÀÛ¼ºÇÑ Äڵ带 ½ÇÇà ½ÃÄ×À»¶§´Â µÇÁö ¾Ê½À´Ï´Ù.
2. Á¦°¡ ÀÛ¼ºÇÑ Äڵ带 ½ÇÇà ½ÃŲ ÈÄ ´Ù¸¥ºÐÀÌ ÀÛ¼ºÇÑ Äڵ带 Áߺ¹ ½ÇÇà ´Ù¸¥ºÐÀÌ ÀÛ¼ºÇÑ
Äڵ带 µð½ºÄ¿³ØÆ®ÇÏ¸é ±×¿Í µ¿½Ã¿¡ Á¦ÇÁ·Î±×·¥¿¡¼ Á¤»óÀûÀ¸·Î Data¸¦ Read ÇÕ´Ï´Ù.
3. Á¦ÇÁ·Î±×·¥À» ´Ù½Ã Á¾·áÇÏ°í Àç ½ÇÇà Çϸé Read¸¦ ÇÏÁö ¸øÇÕ´Ï´Ù.
4. À§»óÅ¿¡¼ ´Ù¸¥ºÐÀÌ ÀÛ¼ºÇÑ Äڵ带 ½ÇÇà½ÃÄѵµ Data¸¦ ReadÇÏÁö ¸øÇÏ°í, ´Ù¸¥ºÐ ÇÁ·Î±×·¥À»
µð½ºÄ¿³ØÆ®ÇÑ»óÅ¿¡¼ ´Ù½Ã¿¬°áÇÏ¸é ´Ù¸¥ºÐ Äڵ嵵 Á¤»ó µ¿ÀÛ µË´Ï´Ù.
5. óÀ½ºÎÅÍ ´Ù¸¥ºÐÀÌ ÀÛ¼ºÇÑ Äڵ带 ½ÇÇà ½ÃÅ°¸é Á¤»ó µ¿ÀÛÇÕ´Ï´Ù.
½ÇÇà Ç÷οì´Â TCP ¿¬°á -> Client¿¡¼ ƯÁ¤ ÄÚµå Àü¼Û -> ±×¿¡¸Â´Â °á°ú¸¦ Server¿¡¼ º¸³»ÁÝ´Ï´Ù.
Ŭ¶óÀ̾ðÆ® ¼Ò½º °øÀ¯µå¸®°í Á¶¾ðÁ» ºÎŹµå¸³´Ï´Ù.
public class Client_TCP
{
Socket _sk_Client;
MsgFormat _MsgFormat;
PointF[] _VisionCoordinate;
bool _bThreadFlag;
public enum CameraNum { CAM1,CAM2 };
string _strVisionData;
public Client_TCP(int iPortnum, MsgFormat _MsgFormat)
{
_VisionCoordinate = new PointF[2];
_bThreadFlag = false;
this._MsgFormat = _MsgFormat;
TCPConnect(iPortnum);
}
public PointF[] VisionCoordinate
{
get { return _VisionCoordinate; }
set { _VisionCoordinate = value; }
}
public string strVisionData
{
get { return _strVisionData; }
set { _strVisionData = value; }
}
public Socket sk_Client
{
get { return _sk_Client; }
}
public void TCPConnect(int iPortnum)
{
_sk_Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), iPortnum);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = remoteIP;
_sk_Client.ConnectAsync(args);
System.Threading.Thread.Sleep(3000);
ServerRecvdData();
}
private void ServerRecvdData()
{
try
{
byte[] szData = new byte[100];
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += new EventHandler<SocketAsyncEventArgs>(Receive_Completed);
args.SetBuffer(szData, 0, szData.Length);
_sk_Client.ReceiveAsync(args);
}
catch (Exception ex)
{
}
}
private void Receive_Completed(object sender, SocketAsyncEventArgs e)
{
Socket ClientSocket = sender as Socket;
try
{
if (ClientSocket.Connected && e.BytesTransferred > 0)
{
byte[] szData = e.Buffer;
_strVisionData = Encoding.Unicode.GetString(szData);
_strVisionData = strVisionData.Replace("\0", "").Trim();
Array.Clear(szData, 0, szData.Length);
e.SetBuffer(szData, 0, 100);
ClientSocket.ReceiveAsync(e);
}
else
{
ClientSocket.Disconnect(false);
ClientSocket.Dispose();
}
}
catch (Exception ex)
{
}
}
public void ServerSendData(string _SendData)
{
byte[] sendbuffer = new byte[20];
sendbuffer = Encoding.UTF8.GetBytes(_SendData);
try
{
_sk_Client.Send(sendbuffer);
}
catch (Exception ex)
{
}
}
} |
SendDataÇϱâÀü¿¡ Á¤»óÀûÀ¸·Î Connection µÈ»óÅÂÀÎÁö üũ ¸ÕÀú ÇÏ°í ±× µÚ·Î ÇϳªÇϳª µð¹ö±ë ÇغÁ¾ßÇÒ°Í °°Àºµ¥¿ä