• 大小: 112KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-12
  • 语言: 其他
  • 标签: c#  TCP  UDP  通信  

资源简介

1,Server支持多客户访问; 2,C与S之间使用TCP连接; 3,C与C之间直接通信(不是通过S传递)。 4,C与C之间直接通信既可以使用TCP,也可以使用UDP。 5,可以使用Socket,也可以使用TcpClient/UdpClient等;

资源截图

代码片段和文件信息

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

namespace SyncChatClient
{
    public partial class Client : Form
    {
        private bool isExit = false;
        private TcpClient client;
        private BinaryReader br;
        private BinaryWriter bw;

        private List userList = new List();
        private TcpListener myListener;
        IPAddress localAddress;
        private static int myport;

        public Client()
        {
            InitializeComponent();
            Random r = new Random((int)DateTime.Now.Ticks);
            textBoxUserName.Text = “User“ + r.Next(100 999);
            listBoxOnline.HorizontalScrollbar = true;

            //---------------------c-c----------------------------------
            IPAddress[] addrIP = Dns.GetHostAddresses(Dns.GetHostName());
            localAddress = addrIP[0];

            while (true)
            {
                myport = r.Next(2049 8888);
                try
                {
                    myListener = new TcpListener(localAddress myport);
                    break;
                }
                catch
                {
                    //如果端口不可用
                }
            }
            myListener.Start();
            Thread myThread = new Thread(ListenClientConnect);
            myThread.IsBackground=true;
            myThread.Start();
        }

        //---------------------c-c----------------------------------
        private void ListenClientConnect()   
        {
            TcpClient newClient = null;
            while (true)
            {
                try
                {
                    newClient = myListener.AcceptTcpClient();
                }
                catch
                {
                    break;
                }
                User user = new User(newClient);
                Thread threadReceive = new Thread(ServerReceive);
                threadReceive.Start(user);
                threadReceive.IsBackground = true;
            }
        }

        //---------------------c-c----------------------------------
        private void ServerReceive(object userState)    //客户端充当server的接收方法
        {
            User user = (User)userState ;//////////////////////////////////////////
            TcpClient client = user.client;
            while (isExit == false)
            {
                string receiveString = null;
                try
                {
                    receiveString = user.br.ReadString();
                    AddTalkMessage(receiveString);
                }
                catch
                {
                    if (isExit == false)
                  

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

     文件        189  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\App.config

     文件      17920  2018-06-21 12:22  实验2TCP+UDP通信实验\SyncChatClient\bin\Debug\SyncChatClient.exe

     文件        189  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\bin\Debug\SyncChatClient.exe.config

     文件      38400  2018-06-21 12:22  实验2TCP+UDP通信实验\SyncChatClient\bin\Debug\SyncChatClient.pdb

     文件      11592  2018-06-21 15:05  实验2TCP+UDP通信实验\SyncChatClient\bin\Debug\SyncChatClient.vshost.exe

     文件        189  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\bin\Debug\SyncChatClient.vshost.exe.config

     文件        490  2013-06-18 20:28  实验2TCP+UDP通信实验\SyncChatClient\bin\Debug\SyncChatClient.vshost.exe.manifest

     文件      11752  2018-04-30 19:47  实验2TCP+UDP通信实验\SyncChatClient\Client.cs

     文件       8875  2018-04-30 19:47  实验2TCP+UDP通信实验\SyncChatClient\Client.Designer.cs

     文件       5817  2018-04-30 19:47  实验2TCP+UDP通信实验\SyncChatClient\Client.resx

     文件       1463  2018-04-30 20:23  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6477  2018-06-21 14:54  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        180  2018-04-30 19:47  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.Client.resources

     文件       3664  2018-06-21 15:05  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.csproj.FileListAbsolute.txt

     文件        976  2018-04-30 19:47  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.csproj.GenerateResource.Cache

     文件       2383  2018-04-30 19:47  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.csprojResolveAssemblyReference.cache

     文件      17920  2018-06-21 12:22  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.exe

     文件      38400  2018-06-21 12:22  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.pdb

     文件        180  2018-04-30 19:44  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\SyncChatClient.Properties.Resources.resources

     文件          0  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        527  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\Program.cs

     文件       1336  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\Properties\AssemblyInfo.cs

     文件       2841  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\Properties\Resources.Designer.cs

     文件       5612  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\Properties\Resources.resx

     文件       1101  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\Properties\Settings.Designer.cs

     文件        249  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\Properties\Settings.settings

     文件       3966  2018-04-30 01:05  实验2TCP+UDP通信实验\SyncChatClient\SyncChatClient.csproj

     文件       2560  2018-04-30 01:12  实验2TCP+UDP通信实验\SyncChatClient\TCP_UDP通信实验.suo

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

评论

共有 条评论