(동영상)C#, 윈도우 채팅 클라이언트, Winform 채팅, Chatting Client 만들기, 시샵채팅소스, 채팅예제, 채팅프로그램, 닷넷학원, 자바학원, C#학원, WPF학원, SQL학원
http://ojc.asia/bbs/board.php?bo_table=LecCsharp&wr_id=417
ojc.asia

https://www.youtube.com/watch?v=xxK1uIjfcqU&list=PLxU-iZCqT52CA9Y474h7UbqmWqXwIZ-hl&index=4

https://www.youtube.com/watch?v=T_4rw5Fsm3k&list=PLxU-iZCqT52CA9Y474h7UbqmWqXwIZ-hl&index=3

https://www.youtube.com/watch?v=9Hkqgs4o-sc&list=PLxU-iZCqT52CA9Y474h7UbqmWqXwIZ-hl&index=2

https://www.youtube.com/watch?v=RLiaZaEdyqs&list=PLxU-iZCqT52CA9Y474h7UbqmWqXwIZ-hl&index=1

윈폼 기반의 채팅 프로그램(클라이언트)



1. 프로젝트명 : ChatClient
[채팅 클라이언트]
[Form1.Designer.cs]
namespace ChatClient
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.btnConnect = new System.Windows.Forms.Button();
this.txtChatMsg = new System.Windows.Forms.TextBox();
this.txtMsg = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(43, 15);
this.label1.TabIndex = 0;
this.label1.Text = "대화명";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(71, 21);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(117, 23);
this.txtName.TabIndex = 1;
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(262, 21);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(122, 27);
this.btnConnect.TabIndex = 2;
this.btnConnect.Text = "입장";
this.btnConnect.UseVisualStyleBackColor = true;
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// txtChatMsg
//
this.txtChatMsg.Location = new System.Drawing.Point(25, 67);
this.txtChatMsg.Multiline = true;
this.txtChatMsg.Name = "txtChatMsg";
this.txtChatMsg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtChatMsg.Size = new System.Drawing.Size(359, 183);
this.txtChatMsg.TabIndex = 3;
//
// txtMsg
//
this.txtMsg.Location = new System.Drawing.Point(27, 267);
this.txtMsg.Name = "txtMsg";
this.txtMsg.Size = new System.Drawing.Size(357, 23);
this.txtMsg.TabIndex = 4;
this.txtMsg.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtMsg_KeyPress);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(407, 305);
this.Controls.Add(this.txtMsg);
this.Controls.Add(this.txtChatMsg);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Label label1;
private TextBox txtName;
private Button btnConnect;
private TextBox txtChatMsg;
private TextBox txtMsg;
}
}
[Form1.cs]
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ChatClient
{
//클라이언트의 텍스트박스에 글을 쓰기위한 델리게이트
//실제 글을 쓰는것은 Form1클래스의 쓰레드가 아닌 다른 스레드인 ChatHandler의 스레드 이기에
//(만약 컨트롤을 만든 쓰레드가 아닌 다른 스레드에서 텍스트박스에 글을 쓴다면 에러발생)
//ChatHandler의 스레드에서 이 델리게이트를 호출하여 서버에서 넘어오는 메시지를 쓴다.
delegate void SetTextDelegate(string s);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpClient tcpClient = null;
NetworkStream ntwStream = null;
//서버와 채팅을 실행
ChatHandler chatHandler = new ChatHandler();
//입장 버튼 클릭
private void btnConnect_Click(object sender, EventArgs e)
{
if (btnConnect.Text == "입장")
{
try
{
tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 2022);
ntwStream = tcpClient.GetStream();
chatHandler.Setup(this, ntwStream, this.txtChatMsg);
Thread chatThread = new Thread(new ThreadStart(chatHandler.ChatProcess));
chatThread.Start();
Message_Snd("<" + txtName.Text + "> 님께서 접속 하셨습니다.", true);
btnConnect.Text = "나가기";
}
catch (System.Exception Ex)
{
MessageBox.Show("Server 오류발생 또는 Start 되지 않았거나\n\n" + Ex.Message, "Client");
}
}
else
{
Message_Snd("<" + txtName.Text + "> 님께서 접속해제 하셨습니다.", false);
btnConnect.Text = "입장";
chatHandler.ChatClose();
ntwStream.Close();
tcpClient.Close();
}
}
private void Message_Snd(string lstMessage, Boolean Msg)
{
try
{
//보낼 데이터를 읽어 Default 형식의 바이트 스트림으로 변환 해서 전송
string dataToSend = lstMessage + "\r\n";
byte[] data = Encoding.Default.GetBytes(dataToSend);
ntwStream.Write(data, 0, data.Length);
}
catch (Exception Ex)
{
if (Msg == true)
{
MessageBox.Show("서버가 Start 되지 않았거나\n\n" + Ex.Message, "Client");
btnConnect.Text = "입장";
chatHandler.ChatClose();
ntwStream.Close();
tcpClient.Close();
}
}
}
//다른 스레드인 ChatHandler의 쓰레드에서 호출하는 함수로
//델리게이트를 통해 채팅 문자열을 텍스트박스에 씀
public void SetText(string text)
{
if (this.txtChatMsg.InvokeRequired)
{
SetTextDelegate d = new SetTextDelegate(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtChatMsg.AppendText(text);
}
}
private void txtMsg_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
//서버에 접속이 된 경우에만 메시지를 서버로 보냄
if (btnConnect.Text == "나가기")
{
Message_Snd("<" + txtName.Text + "> " + txtMsg.Text, true);
}
txtMsg.Text = "";
e.Handled = true; //이벤트처리중지, KeyUp or Click등
}
}
}
public class ChatHandler
{
private TextBox txtChatMsg;
private NetworkStream netStream;
private StreamReader strReader;
private Form1 form1;
public void Setup(Form1 form1, NetworkStream netStream, TextBox txtChatMsg)
{
this.txtChatMsg = txtChatMsg;
this.netStream = netStream;
this.form1 = form1;
this.netStream = netStream;
this.strReader = new StreamReader(netStream);
}
public void ChatClose()
{
netStream.Close();
strReader.Close();
}
public void ChatProcess()
{
while (true)
{
try
{
//문자열을 받음
string lstMessage = strReader.ReadLine();
if (lstMessage != null && lstMessage != "")
{
//SetText 메서드에서 델리게이트를 이용하여 서버에서 넘어오는 메시지를 쓴다.
form1.SetText(lstMessage + "\r\n");
}
}
catch (System.Exception)
{
break;
}
}
}
}
}
#채팅서버, #채팅클라이언트, #채팅소스, #시샵채팅, #닷넷채팅, #채팅프로그램, #닷넷교육, #시샵교육, #닷넷학원, #시샵학원, #닷넷동영상, 채팅서버, 채팅클라이언트, 채팅소스, 시샵채팅, 닷넷채팅, 채팅프로그램, 닷넷교육, 시샵교육, 닷넷학원, 시샵학원, 닷넷동영상,