2015. 3. 14. 17:30
C# with TCP/IP
프로그램 설명
1:1 통신에 이어서 1:N 통신을 다루는 프로그램입니다.
각 클라이언트 소켓마다 쓰레드를 만들어서 통신하는 방법입니다.
실행 후
메신지 전송 후
프로그램 작성 순서
1. 서버 사이드 소켓 핸들 (handleClient.cs)
class handleClient { TcpClient clientSocket; int clientNo; public void startClient(TcpClient ClientSocket, int clientNo) { this.clientSocket = ClientSocket; this.clientNo = clientNo; Thread t_hanlder = new Thread(doChat); t_hanlder.IsBackground = true; t_hanlder.Start(); } public delegate void MessageDisplayHandler(string text); public event MessageDisplayHandler OnReceived; public delegate void CalculateClientCounter(); public event CalculateClientCounter OnCalculated; private void doChat() { NetworkStream stream = null; try { byte[] buffer = new byte[1024]; string msg = string.Empty; int bytes = 0; int MessageCount = 0; while(true) { MessageCount++; stream = clientSocket.GetStream(); bytes = stream.Read(buffer, 0, buffer.Length); msg = Encoding.Unicode.GetString(buffer, 0, bytes); msg = msg.Substring(0, msg.IndexOf("$")); msg = "Data Received : " + msg; if (OnReceived != null) OnReceived(msg); msg = "Server to client(" + clientNo.ToString() + ") " + MessageCount.ToString(); if (OnReceived != null) OnReceived(msg); byte[] sbuffer = Encoding.Unicode.GetBytes(msg); stream.Write(sbuffer, 0, sbuffer.Length); stream.Flush(); msg = " >> " + msg; if (OnReceived != null) { OnReceived(msg); OnReceived(""); } } } catch(SocketException se) { Trace.WriteLine(string.Format("doChat - SocketException : {0}", se.Message)); if (clientSocket != null) { clientSocket.Close(); stream.Close(); } if (OnCalculated != null) OnCalculated(); } catch (Exception ex) { Trace.WriteLine(string.Format("doChat - Exception : {0}", ex.Message)); if (clientSocket != null) { clientSocket.Close(); stream.Close(); } if (OnCalculated != null) OnCalculated(); } } }
2. 서버 프로그램
public partial class MainForm : Form { TcpListener server = null; TcpClient client = null; static int counter = 0; public MainForm() { InitializeComponent(); // socket start Thread t = new Thread(InitSocket); t.IsBackground = true; t.Start(); } private void InitSocket() { server = new TcpListener(IPAddress.Any, 9999); client = default(TcpClient); server.Start(); DisplayText(">> Server Started"); while(true) { try { counter++; client = server.AcceptTcpClient(); DisplayText(">> Accept connection from client"); handleClient h_client = new handleClient(); h_client.OnReceived += new handleClient.MessageDisplayHandler(DisplayText); h_client.OnCalculated += new handleClient.CalculateClientCounter(CalculateCounter); h_client.startClient(client, counter); } catch (SocketException se) { Trace.WriteLine(string.Format("InitSocket - SocketException : {0}", se.Message)); } catch (Exception ex) { Trace.WriteLine(string.Format("InitSocket - Exception : {0}", ex.Message)); } } } private void CalculateCounter() { counter--; } private void DisplayText(string text) { if (richTextBox1.InvokeRequired) { richTextBox1.BeginInvoke(new MethodInvoker(delegate { richTextBox1.AppendText(text + Environment.NewLine); })); } else richTextBox1.AppendText(text + Environment.NewLine); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (client != null) { client.Close(); client = null; } if (server != null) { server.Stop(); server = null; } } }
3. 클라이언트 프로그램
public partial class MainForm : Form { TcpClient clientSocket = new TcpClient(); public MainForm() { InitializeComponent(); new Thread(delegate() { InitSocket(); }).Start(); } private void InitSocket() { try { clientSocket.Connect("192.168.0.12", 9999); DisplayText("Client Started"); labelStatus.Text = "Client Socket Program - Server Connected ..."; } catch (SocketException se) { MessageBox.Show(se.Message, "Error"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } } private void btnSend_Click(object sender, EventArgs e) { NetworkStream stream = clientSocket.GetStream(); byte[] sbuffer = Encoding.Unicode.GetBytes(richTextBox2.Text + "$"); stream.Write(sbuffer, 0, sbuffer.Length); stream.Flush(); byte[] rbuffer = new byte[1024]; stream.Read(rbuffer, 0, rbuffer.Length); string msg = Encoding.Unicode.GetString(rbuffer); msg = "Data from Server : " + msg; DisplayText(msg); richTextBox2.Text = ""; richTextBox2.Focus(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (clientSocket != null) clientSocket.Close(); } private void DisplayText(string text) { if (richTextBox1.InvokeRequired) { richTextBox1.BeginInvoke(new MethodInvoker(delegate { richTextBox1.AppendText(Environment.NewLine + " >> " + text); })); } else richTextBox1.AppendText(Environment.NewLine + " >> " + text); } }
'C# with TCP/IP' 카테고리의 다른 글
[Program C#] SslStream 을 이용한 통신 방법 (1) | 2015.03.20 |
---|---|
[Program C#] 서버-클라이언트 채팅 통신 (0) | 2015.03.17 |
[Program C#] Socket multiple port로 작업하기 (0) | 2015.03.12 |
[Program C#] 서버-클라이언트 1:1 통신 (0) | 2015.03.12 |
[Program C#] 비동기 접속 - 윈도우폼 (0) | 2015.03.06 |