C# Socket网络通信 基础代码 请求品鉴

服务端关键代码

using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyFunc;
namespace _10_13_Socket网络通信
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //获取本机ip
             
            txtIp.Text = "192.168.1.53";
            txtPoint.Text = "50000";
            //忽略线程检查
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        Thread th;
        IPEndPoint point;
        private void BtnListen_Click(object sender, EventArgs e)
        {
            try
            {
                //创建侦听socket 采用Tcp stream流 IPV4协议
                Socket sktListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //获取ip和端口号
                 
                point = new IPEndPoint(IPAddress.Parse(txtIp.Text), Convert.ToInt32(txtPoint.Text));
                sktListen.Bind(point);//绑定通信信息(ip 和端口号)
                sktListen.Listen(10);//设置访问队列
                                     //由于侦听函数Accept()是一直在循环 如果用主进程调用此函数 程序会卡死 因此要创建新线程
                th = new Thread(Listen);
                th.IsBackground = true;
                th.Start(sktListen);
                Control.CheckForIllegalCrossThreadCalls = false;
            }
            catch
            {
 
 
            }
 
        }
        public void Listen(object o)
        {
            try
            {
                Socket newsktListen = o as Socket;//监听
                Socket socketLink;//监听数据
                newsktListen.Listen(10);
                while (true)//用死循环来无限等待客户端连接
                {
 
                    socketLink = newsktListen.Accept();//无限循环 直到接收到为止
                                                       //Accept结束 必定有客户端进入 这时就要给下拉框以及集合里添加数据了
                    GetReomtePoint(socketLink.RemoteEndPoint, socketLink);
                    txtMsg.AppendText(socketLink.RemoteEndPoint.ToString() + "链接成功\r\n");
                    Thread thMsg = new Thread(GetByte);
                    thMsg.Start(socketLink);
 
                }
 
            }
            catch (Exception)
            {
 
                throw;
            }
 
 
 
 
 
        }
        //客户连接成功之后 再创建一个线程来不断接收通讯信息
        public void GetByte(Object socketLink)
        {
            try
            {
                Socket newsocketLink = socketLink as Socket;
                //这个时候已经有客户端连接上了 写一个方法用来给下拉框赋值
 
                string str = "";
                while (true)
                {
                    byte[] buffer = new byte[1024];
                    int r = newsocketLink.Receive(buffer);
                    str = newsocketLink.RemoteEndPoint.ToString() + "Say:" + System.Text.Encoding.Default.GetString(buffer, 0, r);
                    if (r == 0)
                    {
                        break;
                    }
                    txtMsg.AppendText(str + "\r\n");
                }
            }
            catch (Exception)
            {
 
                throw;
            }
 
 
 
        }
        Dictionary<string, Socket> infodic = new Dictionary<string, Socket>();
        int i = 0;
        /// <summary>
        /// 这个方法是给下拉框赋值 因为后期也要选择客户信息 跟选定的IP通信 因此要用集合记录下来ip 和跟ip通信的socket
        /// </summary>
        /// <param name="point"></param>
        /// <param name="newsocketLink"></param>
        public void GetReomtePoint(EndPoint point, Socket newsocketLink)
        {
 
 
            infodic.Add(point.ToString(), newsocketLink);
            cmbIPEndPoint.Items.Add(point);
 
            cmbIPEndPoint.SelectedIndex = i;
            i++;
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
 
 
        }
 
        private void BtnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //插入0表示发送文本
                 
                //发送txtsendmsg中的txt 并给字节数组的第一个索引插入0
                byte []bytedata = System.Text.Encoding.Default.GetBytes(txtSendMsg.Text);
                bytedata = MyFunc.ByteOperate.Insert(bytedata, 0, 0);
                Send(bytedata,txtSendMsg.Text);
 
            }
            catch (Exception)
            {
 
                throw;
            }
            //根据key寻找value
 
        }
        //写一个发送的方法
        public void Send(byte []buffer,string txt)
        {
            Socket newsocketLink = infodic[cmbIPEndPoint.SelectedItem.ToString()];
 
            newsocketLink.Send(buffer);
             
            txtMsg.AppendText("Say:" + txt + "\r\n");
            //点击之后txtbox中的文本要清空
            txtSendMsg.Text = "";
        }
        OpenFileDialog ofd;
        private void BtnSendFile_Click(object sender, EventArgs e)
        {
            ofd = new OpenFileDialog();
            ofd.InitialDirectory = @"F:\PKX桌面";
            ofd.Title = "请选择要发送的文件";
            ofd.Filter = "图片文件|*.jpg|文本文件|*.txt|音乐文件|*.wav|视频文件|*.rmvb|所有文件|*.*";
            ofd.ShowDialog();
            string path = ofd.FileName;
            if (path=="")
            {
                return;
            }
            else
            {
                using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    byte[] buffer = new byte[fsRead.Length];
                    fsRead.Read(buffer, 0, buffer.Length);
                    buffer = MyFunc.ByteOperate.Insert(buffer, 1, 0);//插入1 表示发送文件
                                                                     //发送文件
                    Send(buffer, "发送文件:" + path);
                }
            }
             
             
        }
 
        private void BtnShake_Click(object sender, EventArgs e)
        {
            byte[] buffer = new byte[1];
            buffer = MyFunc.ByteOperate.Insert(buffer, 2, 0);//字节用2表示
            Send(buffer,"给对方发送震动");
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }
    }
}

客户端关键代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyFunc;
namespace _10_14_Socket网络通信_客户端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket socClient;
        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
 
 
        }
 
        private void BtnSend_Click(object sender, EventArgs e)
        {
            try
            {
                socClient.Send(System.Text.Encoding.Default.GetBytes(txtSend.Text));
                txtMsg.AppendText("Say:" + txtSend.Text + "\r\n");
                txtSend.Text = "";
            }
            catch (Exception)
            {
 
                throw;
            }
 
 
        }
        public void GetByte()
        {
            try
            {
                IPAddress ip = IPAddress.Parse("192.168.1.53"); //new IPAddress(System.Text.Encoding.Default.GetBytes("192.168.1.53"));
                socClient.Connect(ip,50000);
                button1.Hide();
                string str = "";
                 
                while (true)//这里一定要死循环 循环接收 不能用socClient.Available != 0
                {
 
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    int r = socClient.Receive(buffer);
                    //判断收到的字节的第一个下标的值
                    switch (buffer[0])
                    {
                        case 0://是文本
                            {
                                buffer = MyFunc.ByteOperate.Remove(buffer, 0);
                                str = socClient.RemoteEndPoint.ToString() + "Say:" + System.Text.Encoding.Default.GetString(buffer, 0, r);
                                if (r == 0)
                                {
                                    break;
                                }
                                txtMsg.AppendText(str + "\r\n");
                            }
                            break;
                        case 1://是文件
                            {
                                 
                                buffer = MyFunc.ByteOperate.Remove(buffer, 0);//移除协议
                                                                              //接收到文件 需要打开保存窗口
                                SaveFileDialog sfd = new SaveFileDialog();
                                sfd.Title = "请选择要保存的文件路径和文件名称";
                                sfd.InitialDirectory = @"F:\PKX桌面";
                                sfd.ShowDialog(this);
                                if (sfd.FileName == "")
                                {
                                    return;
                                }
                                else
                                {
                                    using (FileStream fsWrite = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.Write))
                                    {
                                        fsWrite.Write(buffer, 0, buffer.Length);
                                    }
                                    MessageBox.Show("保存成功!");
                                }
                            }
                            break;
                        case 2://发送的是震动
                            {
                                this.Show();
                                for (int i = 0; i < 500; i++)
                                {
                                    //this.Location = new Point(220,300);
                                    //this.Location = new Point(230, 310);
                                    //this.Location.X = this.Location.X + 50;
                                    this.Location = new Point(this.Location.X + 10, this.Location.Y + 10);
                                    this.Location = new Point(this.Location.X - 10, this.Location.Y - 10);
                                }
                                
 
                            }
                            break;
                        default:
                            break;
                    }
 
 
 
 
                }
            }
            catch (Exception)
            {
 
                throw;
            }
 
        }
 
        private void Button1_Click(object sender, EventArgs e)
        {
 
 
            Thread th = new Thread(GetByte);
            th.IsBackground = true;
            th.Start();
            Control.CheckForIllegalCrossThreadCalls = false;
 
        }
 
        private void BtnShake_Click(object sender, EventArgs e)
        {
 
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞天的大鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值