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 + "\r\n");

        }

        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 Settings\All 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);
        }


    }
}