本文主要是介绍简单的点对点聊天软件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/highplayer/article/details/5992311
在两台电脑上同时运行本程序,可以实现点对点的聊天。
输入对方IP,连接即可聊天。
源代码:
类ClassListener,监听通信信息。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Threading;
- using System.Net.Sockets;
- using System.Windows.Forms;
- namespace PToP
- {
- public class AddMessageEventArge : EventArgs
- {
- public string mess; //定义一个全局变量
- }
- class ClassListener
- {
- private Thread th;
- private TcpListener tcp1;
- public bool listenerRun = true;
- public event EventHandler<AddMessageEventArge> OnAddMessage;
- //启动线程开始监听
- public void StartListen()
- {
- th = new Thread(new ThreadStart(Listen));
- th.Start();
- }
- //停止监听
- public void stop()
- {
- tcp1.Stop();
- th.Abort();
- }
- public void Listen()
- {
- try
- {
- IPAddress addr = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
- IPEndPoint iplocalendpoint = new IPEndPoint(addr, 5656);
- tcp1 = new TcpListener(iplocalendpoint);
- tcp1.Start();
- while (listenerRun)
- {
- Socket s = tcp1.AcceptSocket();
- string remote = s.RemoteEndPoint.ToString();
- Byte[] stream = new Byte[512];
- int i = s.Receive(stream);
- string msg = "<" + remote + ">" + System.Text.Encoding.UTF8.GetString(stream);
- AddMessageEventArge arg = new AddMessageEventArge();
- arg.mess = msg;
- OnAddMessage(this, arg);
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- }
- }
类ClassSender,实现发送聊天信息
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- using System.Windows.Forms;
- namespace PToP
- {
- class ClassSender
- {
- private string obj;
- public ClassSender(string str)
- {
- obj=str;
- }
- public void Send(string str)
- {
- try
- {
- TcpClient tcpc = new TcpClient(obj, 5656);
- NetworkStream tcpstream = tcpc.GetStream();
- Byte[] data = System.Text.Encoding.ASCII.GetBytes(str);
- tcpstream.Write(data, 0, data.Length);
- tcpstream.Close();
- tcpc.Close();
- }
- catch(Exception)
- {
- MessageBox.Show("连接被目标主机拒绝!");
- }
- }
- }
- }
窗体代码中,添加连接目标计算机和发送聊天信息的程序
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.Net.Security;
- namespace PToP
- {
- public partial class Form1 : Form
- {
- //定义委托
- public delegate void InvokeDelegate();
- public bool appRun = true;
- private ClassListener Lie;
- private ClassSender sen;
- string Strs = "";
- public Form1()
- {
- InitializeComponent();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- //接收事件
- void Lie_OnAddMessage(object sender, AddMessageEventArge e)
- {
- Strs = e.mess.ToString();
- Invoke_Click(null, null);
- }
- //委托
- private void Invoke_Click(object sender, EventArgs e)
- {
- textBox1.BeginInvoke(new InvokeDelegate(InvokeMethod));
- }
- public void InvokeMethod()
- {
- if (textBox1.Text == "")
- {
- textBox1.Text = Strs;
- }
- else
- {
- textBox1.Text = textBox1.Text + "/r/n" + Strs;
- }
- }
- //连接目标计算机
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- //连接
- Lie.listenerRun = false;
- Lie.stop();
- }
- catch (NullReferenceException) { ;}
- finally
- {
- Lie = new ClassListener();
- Lie.OnAddMessage +=new EventHandler<AddMessageEventArge>(Lie_OnAddMessage);
- Lie.StartListen();
- }
- }
- //发送聊天记录信息
- private void button2_Click(object sender, EventArgs e)
- {
- if (textBox3.Text == "")
- {
- MessageBox.Show("请输入目标主机的IP地址");
- }
- else if (textBox2.Text == "")
- {
- MessageBox.Show("不能发送空信息!");
- }
- else
- {
- IPAddress ipaddress = new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
- if (textBox1.Text == "")
- {
- textBox1.Text = "<" + ipaddress.ToString() + "> " + textBox2.Text;
- }
- else
- {
- textBox1.Text =textBox1.Text +"/r/n"+ "<" + ipaddress.ToString() + "> " + textBox2.Text;
- }
- sen = new ClassSender(textBox3.Text);
- sen.Send(textBox2.Text);
- }
- }
- }
- }
这篇关于简单的点对点聊天软件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!