p2p聊天系统(北大青鸟大二寒假作业)

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();
        }

    }

}

你可能感兴趣的:(java~爪哇)