一.连接

1.服务器端开始监听

2.客户端Connect()连接服务器端

3.服务器端Accept()产生新的socket,一次连接完成,此时服务器端与客户端就可以使用Send和Receive发送和接受数据

二.发送数据和文件

1.一端发送数据时,在包头(也可以包尾)加上特殊一个字符(比如0和1),另一端先判断包的第一个字符是什么,比如0就是字符串,1就是文件,然后读取数据

2.一段发送文件时,应该先发送文件名,再发送文件,另一端先获取文件名,再根据文件名,把数据保存本地。

 

三.代码

客户端代码:

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

namespace SOCKET_Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.FormClosed += Form1_FormClosed;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            SafeClose(socketSend);
        }

        Socket socketSend;
        string FileName;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建负责通信的Socket
                socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(TxtIpAddress.Text.Trim());
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text.Trim()));
                // 获得要连接的远程服务器应用程序的ip地址和端口号
                socketSend.Connect(point);
                ShowMsg("连接成功");
                // 开启一个新的线程不停的接受服务端发来的消息
                Thread th = new Thread(Recive);
                th.IsBackground = true;
                th.Start();
            }
            catch { }     
        }
        /// <summary>
        /// 不停的接受服务器发来的消息
        /// </summary>
        void Recive()
        {
            try
            {
                while (true)
                {
                    byte[] buffer = new byte[1024 * 1024 * 3];
                    // 实际接受到的有效字节数
                    int r = socketSend.Receive(buffer);
                    if (r == 0)
                    {
                        break;
                    }
                    // 判断第一位
                    switch (buffer[0])
                    {
                        case 0:
                            string str = Encoding.UTF8.GetString(buffer, 1, r-1);
                            //MessageBox.Show(str);
                            ShowMsg(socketSend.RemoteEndPoint + ":" + str);
                            break;
                        case 1:                                                  
                                using(FileStream fsWrite=new FileStream(@"C:\"+FileName,FileMode.OpenOrCreate,FileAccess.Write))
                                {
                                    fsWrite.Write(buffer,1,r-1);
                                }
                                MessageBox.Show("保存成功!");                          
                            break;
                        case 3:
                            //获取文件文件名
                           FileName = Encoding.UTF8.GetString(buffer, 1, r - 1);
                            break;
                        case 2:
                            ZhenDong();
                            break;
                     
                    }                 
                   
                }
            }
            catch { }         
        }
        /// <summary>
        /// 震动
        /// </summary>
        void ZhenDong()
        {
            int posX = this.Location.X;
            int posY = this.Location.Y;
            for (int i = 0; i < 300; i++)
            {
                this.Location = new Point(posX-200,posY-200);
                this.Location = new Point(posX, posY);
            }
        }
        void ShowMsg(string str)
        {
            richTextBox1.AppendText(str + "\r\n");
        }
        /// <summary>
        /// 客户端给服务器发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (richTextBox2.Text.Trim() == "")
                {
                    MessageBox.Show("当前无发送内容!");
                    return;
                }
                string str = richTextBox2.Text.Trim();
                byte[] buffer = Encoding.UTF8.GetBytes(str);
                socketSend.Send(buffer);
                richTextBox2.Clear();
            }
            catch { }

        }
        public  void SafeClose( Socket socket)
        {
            if (socket == null)
                return;

            if (!socket.Connected)
                return;

            try
            {
                socket.Shutdown(SocketShutdown.Both);
            }
            catch
            {
            }

            try
            {
                socket.Close();
            }
            catch
            {
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SafeClose(socketSend);
        }
    }
}
客户端

相关文章: