• c# 服务端


    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.Sockets;
    using System.Net;
    using System.Threading;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
    
            Dictionary<string, Socket> d = new Dictionary<string, Socket>();
            public Form1()
            {
                InitializeComponent();
            }
            Socket so;
    
            private void button1_Click(object sender, EventArgs e)
            {
                string str = this.textBox1.Text.Trim();
                Byte[] buf = System.Text.Encoding.UTF8.GetBytes(str);
                //Byte[] buffernew = new Byte[buf.Length + 1];
                //buffernew[0] = 0;
                //int i = 1;
                //foreach (byte a in buf) 
                //{
                //    buffernew[i] = a;
                //    i++;
                //}
                List<byte> list = new List<byte>();
                list.Add(0);
                list.AddRange(buf);
                byte[] buffernew = list.ToArray();
                d[comboBox1.SelectedItem.ToString()].Send(buffernew);
                //a.Send(buf);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string id = this.ip.Text;
                int port = Convert.ToInt32(this.port.Text);
                IPAddress ip = IPAddress.Parse(id);
                IPEndPoint ports = new IPEndPoint(ip,port);
                so = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                so.Bind(ports);
                so.Listen(10);
                Thread d = new Thread(client);
                d.IsBackground = true;
                d.Start(so);
                
    
                
                
    
            }
    
            public void listin(string msg) 
            {
    
                this.textBox2.AppendText(msg + "
    ");
                
            }
    
            //public void ceshi() 
            //{
            //    while (true) 
            //    {
            //        Socket ce = so.Accept();
            //        listin(ce.RemoteEndPoint.ToString()+"连接成功");
            //        Thread ax = new Thread(client);
            //        ax.Start(ce);
    
                
            //    }
            
            //}
    
            
            public void client(object o) 
            {
                Socket s = o as Socket;
    
                while (true) 
                {
                    Socket a = s.Accept();
                    d.Add(a.RemoteEndPoint.ToString(), a);
                    this.comboBox1.Items.Add(a.RemoteEndPoint.ToString());
                    listin(a.RemoteEndPoint.ToString() + "连接成功");
                    Thread th = new Thread(recevie);
                    th.IsBackground = false;
                    th.Start(a);
                 
                }
            
            }
    
            void recevie(object o) 
            {
                Socket a = o as Socket;
                while (true)
                {
                    Byte[] buffer = new Byte[1024 * 1024 * 3];
                    int t = a.Receive(buffer);
                    if (t == 0) 
                    {
                        break;
                    }
                    string str = Encoding.UTF8.GetString(buffer, 0, t);
                    listin(a.RemoteEndPoint.ToString() + ":" + str);
                }
            }
    
    
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Control.CheckForIllegalCrossThreadCalls = false;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                OpenFileDialog open = new OpenFileDialog();//创建对话框
                open.InitialDirectory = @"C:Documents and SettingsAll Users桌面"; //设置对话框路径
                open.Title = "对话框1"; //对话框标题
                open.Filter = "所有文件|*.*";
                open.Multiselect = true; //多选
                open.ShowDialog(); //打开对话框
                string paths = open.FileName;  //读取文件的全路径
                if (paths == "") return;
                this.textBox3.Text = paths;
    
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                string path = this.textBox3.Text;
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) 
                {
                    byte[] bytes = new byte[1024 * 1024 * 5];
                    int r = fs.Read(bytes, 0, bytes.Length);
                    List<byte> list = new List<byte>();
                    list.Add(1);
                    list.AddRange(bytes);
                    byte[] buffernew = list.ToArray();
                    d[comboBox1.SelectedItem.ToString()].Send(buffernew,0,r+1,SocketFlags.None);
                }
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                byte[] buf = new byte[1];
                buf[0] = 2;
                List<byte> list = new List<byte>();
                list.Add(2);
                list.AddRange(buf);
                byte[] buffernew = list.ToArray();
                d[comboBox1.SelectedItem.ToString()].Send(buffernew);
            }
        }
    }
    

      

  • 相关阅读:
    python学习日记——基本数据类型
    python学习日记——安装及初识
    STF平台探索
    fiddler基本操作梳理
    fly.js抛物线连续不断加入购物车
    判断是否存在某个字段hasOwnProperty
    vue中提示toFixed不是函数
    vue中父组件给子组件传值,子组件给父组件传值
    js判断用户的浏览器设备是移动端还是pc端
    css预处理器--sass学习($变量名)
  • 原文地址:https://www.cnblogs.com/mengluo/p/5648917.html
Copyright © 2020-2023  润新知