• 大小: 200KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: C#
  • 标签: C/S  聊天  TCP  

资源简介

C/S模式的TCP聊天室程序 本程序用C#实现,代码附带注释 客户端同时也是服务器 用于局域网内各台机器间通信 提醒:本程序为点对点通信,不是聊天室,还望加以区分 (俺不能为了分而模糊俺的东东。。)

资源截图

代码片段和文件信息

using System;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocketsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = “MSN.ssk“;  
            IPHostEntry ipHostEntry = Dns.GetHostByName(Dns.GetHostName());  //获得本机IP
            IPAddress addr = new IPAddress(ipHostEntry.AddressList[0].Address);  //转化为*.*.*.*格式
            this.toolStripStatusLabel3.Text = addr.ToString();
            
        }
        private Thread listenThread;  //监听线程
        private bool listen = false;     //监听状态
        private TcpListener tcpListener;     //网络监听对象

        private void StartListen()
        {
            try
            {
                tcpListener = new TcpListener(1314);  // 自定义端口
                tcpListener.Start();         //开始监听
                toolStripStatusLabel1.Text = “开始监听.......“;
                this.listen = true;       

                while (listen)
                {
                    Socket s = tcpListener.AcceptSocket();  //接收挂起的链接请求
                    Byte[] stream = new Byte[100];             //设置缓冲区
                    int i = s.Receive(stream);                       //int
                    string message = System.Text.Encoding.UTF8.GetString(stream);//将获得的流转化为字符串
                    this.richTextBox2.AppendText(message);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                toolStripStatusLabel1.Text = “已停止监听!“;
            }
        }
        //================================================================
        private void button1_Click(object sender EventArgs e)                  //发送
        {
            try
            {
                string msg = “<“ + this.textBox1.Text.Trim() + “>:“ + this.richTextBox1.Text + “\n“;
                TcpClient tcpClient = new TcpClient(textBox2.Text 1314);         //根据目标计算机地址建立连接   IP:PORT
                NetworkStream tcpStream = tcpClient.GetStream();                  //获得用于网络访问的数据流
                StreamWriter streamW = new StreamWriter(tcpStream);
                streamW.Write(msg);                                                                //将数据写入数据流
                streamW.Flush();                                                                      //将缓冲数据写入网络流,同时清空缓冲区
                tcpStream.Close();
                tcpClient.Close();
                this.richTextBox2.AppendText(msg);
                this.richTextBox1.Clear();
                toolStripStatusLabel1.Text = “开始监听.......“;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.M

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5612  2009-12-21 19:04  SocketsTest\SocketsTest\Properties\Resources.resx

     文件        249  2009-12-21 19:04  SocketsTest\SocketsTest\Properties\Settings.settings

     文件       1354  2009-12-21 19:04  SocketsTest\SocketsTest\Properties\AssemblyInfo.cs

     文件       2872  2009-12-21 19:04  SocketsTest\SocketsTest\Properties\Resources.Designer.cs

     文件       1096  2009-12-21 19:04  SocketsTest\SocketsTest\Properties\Settings.Designer.cs

     文件       3917  2009-12-26 13:20  SocketsTest\SocketsTest\SocketsTest.csproj

     文件        490  2009-06-11 05:14  SocketsTest\SocketsTest\bin\Debug\SocketsTest.vshost.exe.manifest

     文件      14328  2010-01-09 17:03  SocketsTest\SocketsTest\bin\Debug\SocketsTest.vshost.exe

     文件      12800  2009-12-26 13:29  SocketsTest\SocketsTest\bin\Debug\SocketsTest.exe

     文件      26112  2009-12-26 13:29  SocketsTest\SocketsTest\bin\Debug\SocketsTest.pdb

     文件     516096  2006-08-18 11:50  SocketsTest\SocketsTest\bin\Debug\IrisSkin2.dll

     文件      16292  2006-03-22 05:55  SocketsTest\SocketsTest\bin\Debug\MSN.ssk

     文件      15156  2006-03-22 05:55  SocketsTest\SocketsTest\bin\Debug\MacOS.ssk

     文件      15928  2006-07-18 23:12  SocketsTest\SocketsTest\bin\Debug\Vista2_color7.ssk

     文件        180  2009-12-26 13:29  SocketsTest\SocketsTest\obj\Debug\SocketsTest.Form1.resources

     文件        180  2009-12-26 13:20  SocketsTest\SocketsTest\obj\Debug\SocketsTest.Properties.Resources.resources

     文件      12800  2009-12-26 13:29  SocketsTest\SocketsTest\obj\Debug\SocketsTest.exe

     文件        536  2010-01-09 17:03  SocketsTest\SocketsTest\obj\Debug\SocketsTest.csproj.FileListAbsolute.txt

     文件      26112  2009-12-26 13:29  SocketsTest\SocketsTest\obj\Debug\SocketsTest.pdb

     文件       5661  2009-12-26 13:20  SocketsTest\SocketsTest\obj\Debug\ResolveAssemblyReference.cache

     文件        847  2009-12-26 13:29  SocketsTest\SocketsTest\obj\Debug\SocketsTest.csproj.GenerateResource.Cache

     文件        492  2009-12-21 19:04  SocketsTest\SocketsTest\Program.cs

     文件       6214  2009-12-26 13:29  SocketsTest\SocketsTest\Form1.resx

     文件       9144  2009-12-26 13:29  SocketsTest\SocketsTest\Form1.Designer.cs

     文件       4914  2009-12-26 13:29  SocketsTest\SocketsTest\Form1.cs

     文件        923  2009-12-21 19:04  SocketsTest\SocketsTest.sln

    ..A..H.     15360  2010-01-09 17:03  SocketsTest\SocketsTest.suo

     目录          0  2009-12-21 19:04  SocketsTest\SocketsTest\obj\Debug\TempPE

     目录          0  2009-12-21 19:04  SocketsTest\SocketsTest\bin\Debug

     目录          0  2009-12-21 19:04  SocketsTest\SocketsTest\obj\Debug

............此处省略8个文件信息

评论

共有 条评论