zoukankan      html  css  js  c++  java
  • 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.Threading;
    using System.Net.Sockets;
    using System.Net;
    using System.IO;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Socket client;
            private void button2_Click(object sender, EventArgs e)
            {
                 client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(this.ip.Text);
                IPEndPoint prot = new IPEndPoint(ip, Convert.ToInt32(this.port.Text));
                client.Connect(prot); //服务器端口
                listin("连接成功");
                Thread th = new Thread(sends);
                th.Start();
                
                
                
                
            }
    
            public void listin(string msg)
            {
    
                this.textBox2.AppendText(msg + "
    ");
    
            }
    
            void sends() 
            {
                try{
                    while (true)
                    {
                        Byte[] buf = new Byte[1024 * 1024 * 3];
                        int result = client.Receive(buf);
                        if (result == 0)
                        {
                            break;
                        }
                        if (buf[0] == 0) 
                        {
                            //接收消息
                            string str = Encoding.UTF8.GetString(buf, 1, result-1);
                            listin(client.RemoteEndPoint.ToString() + ":" + str);
    
    
                        }
                        else if (buf[0] == 1)
                        {
                            SaveFileDialog sd = new SaveFileDialog(); //创建
                            // 保存文件对话框
                            sd.InitialDirectory = @"C:Documents and SettingsAll Users桌面"; //设置对话框路径
                            sd.Title = "对话框1"; //对话框标题
                            sd.Filter = "所有文件|*.*";
                            sd.ShowDialog();
                            string path = sd.FileName;
                            using (FileStream fsv = new FileStream(path, FileMode.Create, FileAccess.Write))
                            {
                                //byte[] bytes = Encoding.Default.GetBytes(this.textBox1.Text);
                                //string str = Encoding.UTF8.GetString(buf, 1, result - 1);
                                fsv.Write(buf, 1, buf.Length - 1);
                            }
                            MessageBox.Show("保存成功");
    
    
                        }
                        else if (buf[0] == 2) 
                        {
                            zheng();
                        }
                        
                    }
                    
                }
                catch
                {
                     
                }
    
            }
            void zheng() 
            {
                int x = this.Location.X;
                int y = this.Location.Y;
                for (int i = 0; i < 500; i++) 
                {
                    this.Location = new Point(x - 20, y - 20);
                    this.Location = new Point(x , y);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Control.CheckForIllegalCrossThreadCalls = false;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                string str = this.textBox1.Text.Trim();
                Byte[] buf = System.Text.Encoding.UTF8.GetBytes(str);
                client.Send(buf);
            }
    
    
        }
    }
    

      

  • 相关阅读:
    CLBZDQ
    CF1559D 题解
    DP 的凸优化
    正睿暑期集训7B
    基于 TiSpark 的海量数据批量处理技术
    PowerDesigner16.5下载和安装教程
    使用TiDB MPP
    使用 TiDB 构建实时应用
    oracle转mysql数据库
    kafka-jdbc-connector-sink实现kafka中的数据同步到mysql
  • 原文地址:https://www.cnblogs.com/mengluo/p/5648809.html
Copyright © 2011-2022 走看看