服務器端代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace ChatToolServer
{
public partial class Form1 : Form
{
//server-用於處理客戶端連接請求的socket
Socket clientSocket = null;
delegate void del();
public Form1()
{
InitializeComponent();
}
//server-偵聽方法
private void listen()
{
//獲取服務器IP
string hostName = Dns.GetHostName();
IPAddress[] ip = Dns.GetHostAddresses(hostName);
IPAddress HostIp = ip[0];
//創建一個網絡端點
IPEndPoint iep = new IPEndPoint(HostIp, 82);
//創建服務端服務端套接字
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//將套接字與網絡端點綁定
serverSocket.Bind(iep);
//將套接字置爲偵聽狀態,並設置最大隊列數爲10
serverSocket.Listen(10);
//以同步方式從偵聽套接字的連接請求隊列中提取第一個掛起的連接請求,然後創建並返回新的 Socket
//新的套接字:包含對方計算機的IP和端口號,可使用這個套接字與本機進行通信
clientSocket = serverSocket.Accept();
if (clientSocket != null)
{
MessageBox.Show("連接成功!");
}
}
private void send_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "")//不能發送空消息
{
try
{
//發送數據
string message = textBox1.Text;
byte[] sendbytes = System.Text.Encoding.UTF8.GetBytes(message);
int successSendBtyes = clientSocket.Send(sendbytes, sendbytes.Length, SocketFlags.None);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
//將發送的數據顯示到對話窗口並使對話窗口的滾動條一直停留在最下方
this.textBox2.Text +="服務器:"+"/r/n" +textBox1.Text + "/r/n";//發完一條消息就換行顯示
this.textBox2.SelectionStart = this.textBox2.Text.Length;
this.textBox2.ScrollToCaret();
this.textBox1.Text = "";//將發送窗口清空
}
else
{
MessageBox.Show("發送內容不能爲空");
}
}
private void Form1_Load(object sender, EventArgs e)
{
//server-創建並運行偵聽線程
Thread threadListen = new Thread(new ThreadStart(listen));
threadListen.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] receiveBytes = new byte[1024];
//如果偵聽後取得客戶端連接,並且客戶端的緩衝區中有內容可讀,開始接收數據
if (clientSocket != null)
{
if (clientSocket.Poll(100, SelectMode.SelectRead))
{
int successReceiveBytes = clientSocket.Receive(receiveBytes);
this.textBox2.Text += "客戶端:" +"("+ clientSocket.RemoteEndPoint.ToString()+")"+"/r/n" +
System.Text.Encoding.UTF8.GetString(receiveBytes, 0, successReceiveBytes) + "/r/n";
this.textBox2.SelectionStart = this.textBox2.Text.Length;//使對話窗口的滾動條一直停留在最下方
this.textBox2.ScrollToCaret();
}
}
}
}
}
客戶端代碼;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatToolClient
{
public partial class Form1 : Form
{
Socket clientSocket = null;//客戶端套接字
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//建立與服務器連接的套接字
IPAddress ip = IPAddress.Parse("172.16.94.134");
IPEndPoint iep = new IPEndPoint(ip, 82);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(iep);
textBox2.Text = "連接成功" + "/r/n";
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
private void send_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
try
{
//發送數據
string message = textBox1.Text;
byte[] sendbytes = System.Text.Encoding.UTF8.GetBytes(message);
int successSendBtyes = clientSocket.Send(sendbytes, sendbytes.Length, SocketFlags.None);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
//將發送的數據顯示到對話窗口並使對話窗口的滾動條一直停留在最下方
this.textBox2.Text += "我自己:"+"/r/n"+textBox1.Text + "/r/n";//發完一條消自己息就換行顯示
this.textBox2.SelectionStart = this.textBox2.Text.Length;
this.textBox2.ScrollToCaret();
this.textBox1.Text = "";//將發送窗口清空
}
else
{
MessageBox.Show("發送內容不能爲空");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] receiveBytes = new byte[1024];
if (clientSocket.Poll(100, SelectMode.SelectRead))
{
int successReceiveBytes = clientSocket.Receive(receiveBytes);
this.textBox2.Text +="服務器:"+"/r/n"+
System.Text.Encoding.UTF8.GetString(receiveBytes, 0, successReceiveBytes) + "/r/n";
this.textBox2.SelectionStart = this.textBox2.Text.Length;//使對話窗口的滾動條一直停留在最下方
this.textBox2.ScrollToCaret();
}
}
}
}