资源简介

C# Socket服务端向指定的客户端发送消息(包含服务器)

资源截图

代码片段和文件信息

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

namespace C0710_CharRoom_Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            TextBox.CheckForIllegalCrossThreadCalls = false;
        }

        Socket sokClient = null;//负责与服务端通信的套接字
        Thread threadClient = null;//负责 监听 服务端发送来的消息的线程
        bool isRec = true;//是否循环接收服务端数据

        private void btnConnect_Click(object sender EventArgs e)
        {
            //实例化 套接字
            sokClient = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            //创建 ip对象
            IPAddress address = IPAddress.Parse(txtIP.Text.Trim());
            //创建网络节点对象 包含 ip和port
            IPEndPoint endpoint = new IPEndPoint(address int.Parse(txtPort.Text.Trim()));
            //连接 服务端监听套接字
            sokClient.Connect(endpoint);
            
            //创建负责接收 服务端发送来数据的 线程
            threadClient = new Thread(ReceiveMsg);
            threadClient.IsBackground = true;
            //如果在win7下要通过 某个线程 来调用 文件选择框的代码,就需要设置如下
            threadClient.SetApartmentState(ApartmentState.STA);
            threadClient.Start();
        }

        /// 
        /// 接收服务端发送来的消息数据
        /// 

        void ReceiveMsg()
        {
            while (isRec)
            {
                byte[] msgArr = new byte[1024 * 1024 * 1];//接收到的消息的缓冲区
                int length=0;
                //接收服务端发送来的消息数据
                length =sokClient.Receive(msgArr);//Receive会阻断线程
                if (msgArr[0] == 0)//发送来的是文字
                {
                    string strMsg = System.Text.Encoding.UTF8.GetString(msgArr 1 length - 1);
                    txtShow.AppendText(strMsg + “\r\n“);
                }
                else if (msgArr[0] == 1) { //发送来的是文件
                    SaveFileDialog sfd = new SaveFileDialog();
                    //弹出文件保存选择框
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        //创建文件流
                        using (FileStream fs = new FileStream(sfd.FileName FileMode.OpenOrCreate))
                        {
                            fs.Write(msgArr 1 length - 1);
                            MessageBox.Show(“文件保存成功!“);
                        }
                    }
                }
                else if (msgArr[0] == 2) {
                    ShakeWindow();
                }
            }
        }

        /// 
        /// 闪屏
        /// 

        private void ShakeWindow()
        {
            Random ran = new Random();
            //保存 窗体原坐标
            System.Drawing.Point point = this.Location;
            for (

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

     文件      11776  2011-08-22 16:39  C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.exe

     文件      28160  2011-08-22 16:39  C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.pdb

     文件      11608  2011-10-17 18:38  C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.vshost.exe

     文件        490  2010-03-17 22:39  C0710_CharRoom_Client\bin\Debug\C0710_CharRoom_Client.vshost.exe.manifest

     文件       3594  2011-08-20 16:56  C0710_CharRoom_Client\C0710_CharRoom_Client.csproj

     文件       4074  2011-08-22 16:11  C0710_CharRoom_Client\Form1.cs

     文件       7608  2011-08-22 11:14  C0710_CharRoom_Client\Form1.Designer.cs

     文件       5817  2011-08-22 11:14  C0710_CharRoom_Client\Form1.resx

     文件       1531  2011-10-17 18:38  C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.csproj.FileListAbsolute.txt

     文件      11776  2011-08-22 16:39  C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.exe

     文件        180  2011-08-22 11:28  C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.Form1.resources

     文件      28160  2011-08-22 16:39  C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.pdb

     文件        180  2011-08-20 16:56  C0710_CharRoom_Client\obj\x86\Debug\C0710_CharRoom_Client.Properties.Resources.resources

     文件       6065  2011-10-17 18:27  C0710_CharRoom_Client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        574  2011-08-22 11:28  C0710_CharRoom_Client\obj\x86\Debug\GenerateResource-ResGen.read.1.tlog

     文件        986  2011-08-22 11:28  C0710_CharRoom_Client\obj\x86\Debug\GenerateResource-ResGen.write.1.tlog

     文件        502  2011-08-20 16:52  C0710_CharRoom_Client\Program.cs

     文件       1386  2011-08-20 16:52  C0710_CharRoom_Client\Properties\AssemblyInfo.cs

     文件       2892  2011-08-20 16:52  C0710_CharRoom_Client\Properties\Resources.Designer.cs

     文件       5612  2011-08-20 16:52  C0710_CharRoom_Client\Properties\Resources.resx

     文件       1106  2011-08-20 16:52  C0710_CharRoom_Client\Properties\Settings.Designer.cs

     文件        249  2011-08-20 16:52  C0710_CharRoom_Client\Properties\Settings.settings

     文件      15360  2011-08-22 16:39  C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.exe

     文件      36352  2011-08-22 16:39  C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.pdb

     文件      11608  2011-10-17 18:38  C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.vshost.exe

     文件        490  2010-03-17 22:39  C0710_ChatRoom_Server\bin\Debug\C0710_ChatRoom_Server.vshost.exe.manifest

     文件       3681  2011-08-22 11:28  C0710_ChatRoom_Server\C0710_ChatRoom_Server.csproj

     文件       4781  2011-08-22 16:10  C0710_ChatRoom_Server\ConnectionClient.cs

     文件        186  2011-08-22 11:29  C0710_ChatRoom_Server\DGShowMsg.cs

     文件       6988  2011-08-22 15:46  C0710_ChatRoom_Server\Form1.cs

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

评论

共有 条评论