2015. 3. 6. 16:46
C# with TCP/IP
프로그램 설명
비동기로 접속을 하고 데이타를 주고 받는 방법중에 method 뒤에 SocketAsyncEventArgs class 를 이용하는 방법입니다.
실행 후
데이타 전송 후
프로그램 작성 순서
1. 서버 프로그램
#region MainForm public MainForm() { InitializeComponent(); } #endregion #region MainForm_Load private Socket m_ServerSocket; private Listm_ClientSocket; private byte[] szData; private void MainForm_Load(object sender, EventArgs e) { m_ClientSocket = new List (); m_ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10000); m_ServerSocket.Bind(ipep); m_ServerSocket.Listen(20); SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.Completed += new EventHandler (Accept_Completed); m_ServerSocket.AcceptAsync(args); } #endregion #region Accept_Completed private void Accept_Completed(object sender, SocketAsyncEventArgs e) { Socket ClientSocket = e.AcceptSocket; m_ClientSocket.Add(ClientSocket); //접속한 클라이언수 보이기 label1.Text = m_ClientSocket.Count.ToString(); try { if (m_ClientSocket != null) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); szData = new byte[1024]; args.SetBuffer(szData, 0, szData.Length); args.UserToken = m_ClientSocket; args.Completed += new EventHandler (Receive_Completed); ClientSocket.ReceiveAsync(args); } e.AcceptSocket = null; m_ServerSocket.AcceptAsync(e); } catch(SocketException se) { Trace.WriteLine(string.Format("SocketException : {0}", se.Message)); } catch (Exception ex) { Trace.WriteLine(string.Format("Exception : {0}", ex.Message)); } } #endregion #region Receive_Completed private void Receive_Completed(object sender, SocketAsyncEventArgs e) { Socket ClientSocket = sender as Socket; if (ClientSocket.Connected && e.BytesTransferred > 0) { byte[] szData = e.Buffer; string sData = Encoding.Unicode.GetString(szData); sData = sData.Replace("\0", "").Trim(); SetText(sData); e.SetBuffer(szData, 0, 1024); ClientSocket.ReceiveAsync(e); } else { ClientSocket.Disconnect(false); ClientSocket.Dispose(); m_ClientSocket.Remove(ClientSocket); } //접속한 클라이언수 보이기 label1.Text = m_ClientSocket.Count.ToString(); } #endregion #region SetText private delegate void SetTextCallback(string text); private void SetText(string text) { if (richTextBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { if (richTextBox1.TextLength > 0) { richTextBox1.AppendText("\n"); } richTextBox1.AppendText(text); richTextBox1.ScrollToCaret(); } } #endregion #region Dispose protected override void Dispose(bool disposing) { foreach(Socket pBuffer in m_ClientSocket) { if (pBuffer.Connected) pBuffer.Disconnect(false); pBuffer.Dispose(); } m_ServerSocket.Dispose(); if (disposing && components != null) components.Dispose(); base.Dispose(disposing); } #endregion
2. 클라이언트 프로그램
#region MainForm public MainForm() { InitializeComponent(); } #endregion private Socket m_ClientSocket = null; #region MainForm_Load private void MainForm_Load(object sender, EventArgs e) { m_ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("192.168.0.12"), 10000); SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.RemoteEndPoint = remoteIP; m_ClientSocket.ConnectAsync(args); } #endregion #region button1_Click method private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length > 0) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); byte[] szData = Encoding.Unicode.GetBytes(textBox1.Text); args.SetBuffer(szData, 0, szData.Length); m_ClientSocket.SendAsync(args); textBox1.Text = ""; textBox1.Focus(); } } #endregion #region Dispose protected override void Dispose(bool disposing) { if (m_ClientSocket.Connected) m_ClientSocket.Disconnect(false); m_ClientSocket.Dispose(); if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #endregion
'C# with TCP/IP' 카테고리의 다른 글
[Program C#] Socket multiple port로 작업하기 (0) | 2015.03.12 |
---|---|
[Program C#] 서버-클라이언트 1:1 통신 (0) | 2015.03.12 |
[Program C#] 라이브러리를 이용한 클라이언트-서버 소켓 (0) | 2015.02.25 |
[Program C#]Client-Server Broadcast 예제 - 콘솔버전 (0) | 2015.02.09 |
[Program C#]Binary Serialization 을 이용한 소켓통신 - 윈도우버전 (0) | 2015.01.31 |