P2P聊天系統 北大青鳥 大二寒假作業 主要涉及 線程池 不規則窗體 winsock編程 soket
源代碼如下
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;
using System.Collections;
namespace chating
...{
public partial class chatingto : Form
...{
private bool ismousedown = false;
private Point position;
private Thread beginlistening;
private frmlogin lg;
private NetworkStream netstream;
private Socket soc;
public delegate void DelgMsgListened();
private int count;
private String message;
private TcpListener server;
public ArrayList threadslist;
public Thread[] allthread;
public chatingto()
...{
//關閉線程安全檢查
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
//實例化監聽線程
beginlistening = new Thread(new ThreadStart(listening));
allthread = new Thread[5];
}
//監聽方法
private void listening()
...{
count = 0;
server = new TcpListener(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], 85);
try
...{
server.Start();
}
catch (Exception ex)
...{
Console.WriteLine(ex.Message);
}
//循環接受客戶端連接
while (count < 5)
...{
try
...{
soc = server.AcceptSocket();
}
catch (Exception ex)
...{
Console.WriteLine(ex.Message);
}
//啟動聊天線程
allthread[count] = new Thread(new ParameterizedThreadStart(beginchat));
allthread[count].Start(soc);
count++;
}
}
//聊天方法
private void beginchat(object socket)
...{
Socket soc = (Socket)socket;
lisMessage.Items.Add(lg.username + "已經進入了聊天");
while (true)
...{
byte[] stream = new byte[80];
int i = soc.Receive(stream);
//檢測客戶端是否退出
if (i < 1)
...{
lisMessage.Items.Add(lg.username + "已經退出了聊天");
break;
}
message = Encoding.UTF8.GetString(stream);
lisMessage.Items.Add(message);
}
}
//解決不規則窗體的移動
private void chatingto_MouseDown(object sender, MouseEventArgs e)
...{
if (e.Button == MouseButtons.Left)
...{
int x = -e.X;
int y = -e.Y;
position = new Point(x, y);
ismousedown = true;
}
}
private void chatingto_MouseMove(object sender, MouseEventArgs e)
...{
if (ismousedown)
...{
Point newposition = Control.MousePosition;
newposition.Offset(position);
this.Location = newposition;
}
}
private void chatingto_MouseUp(object sender, MouseEventArgs e)
...{
if (e.Button == MouseButtons.Left)
...{
ismousedown = false;
}
}
//退出是關閉資源
private void btnexit_Click(object sender, EventArgs e)
...{
server.Stop();
beginlistening.Abort();
Application.Exit();
}
//連接服務器並且設置按鈕的狀態
private void btnconnect_Click(object sender, EventArgs e)
...{
//實例化登陸框
lg = new frmlogin();
lg.ShowDialog();
//設置按鈕狀態
btnconnect.Enabled = false;
btndisconnect.Enabled = true;
btnsend.Enabled = true;
btnexit.Enabled = false;
}
private void chatingto_Load(object sender, EventArgs e)
...{
//監聽線程開始
beginlistening.Start();
}
private void btnsend_Click(object sender, EventArgs e)
...{
//向服務器發送消息
if (txtsendtxt.Text.Equals(""))
...{
MessageBox.Show("發送的內容不能為空!");
return;
}
lisMessage.Items.Add(lg.username + txtsendtxt.Text);
byte[] sendtext = Encoding.UTF8.GetBytes(lg.username + txtsendtxt.Text);
netstream = lg.clIEnt.GetStream();
netstream.Write(sendtext, 0, sendtext.Length);
txtsendtxt.Text = "";
}
//斷開連接
private void btndisconnect_Click(object sender, EventArgs e)
...{
netstream.Close();
lg.clIEnt.Close();
btndisconnect.Enabled = false;
btnsend.Enabled = false;
btnconnect.Enabled = true;
btnexit.Enabled = true;
}
}
}
登陸窗體:
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;
namespace chating
...{
public partial class frmlogin : Form
...{
public string username;
private bool ismousedown=false;
private Point position;
public TcpClient clIEnt;
public frmlogin()
...{
InitializeComponent();
}
//不規則窗體的移動
private void frmlogin_MouseDown(object sender, MouseEventArgs e)
...{
if(e.Button==MouseButtons.Left)
...{
int x = -e.X;
int y = -e.Y;
position = new Point(x, y);
ismousedown = true;
}
}
private void frmlogin_MouseMove(object sender, MouseEventArgs e)
...{
if(ismousedown)
...{
Point newposition = Control.MousePosition;
newposition.Offset(position);
this.Location = newposition;
}
}
private void frmlogin_MouseUp(object sender, MouseEventArgs e)
...{
if(e.Button==MouseButtons.Left)
...{
ismousedown = false;
}
}
//連接服務器
private void btnconnect_Click(object sender, EventArgs e)
...{
username = txtname.Text+":";
client = new TcpClIEnt();
try
...{
clIEnt.Connect(IPAddress.Parse(txtaddress.Text), 85);
}
catch(Exception ex)
...{
Console.WriteLine("連接錯誤:" + ex.Message);
return;
}
this.Close();
}
}
}