C#连接Socket

最近几天做了C#连接Socket的小项目,把连接Socket做成了单独的类

Socekt连接是先实例化Socekt的对象client准备连接,再实例化Endpoint类的对象时后绑定IP和Port,将实例化的Endpoint类的对象委托进client的Conneect()方法进行连接

接收使用Receive()方法,创建字节型数组bytes作为缓冲区,用来接收Socket发来的数据,Receive()返回值类型为整形,所以把Receive()的返回值赋值给整形变量len

发送使用send()方法,发送字节型数值buffer

各种基础功能都了出来,小规模测试后没有问题,现在发出来供大家参考

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

namespace WifiConnect
{
    class WifiConnect
    {
        private Socket client;
        private int len;//接收数据
        private string recvStr = "";//接数组转字符串
        private string srxStr = "";//控件操作数据
        private Thread cntthread;//连接线程
        private Thread readthread;//接收线程
        private Thread sendthread;//发送线程

        private void Wifi_cnt(object o)//连接wifi方法
        {
            while (true)
            {               
                while (client.Connected == false||client==null)//判断是否绑定了IP地址
                {                    
                    List<object> list = o as List<object>;
                    string ip = (string)list[0];
                    int port = (int)list[1];
                    EndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);//设置IP与端口
                    client.Connect(point);//进行连接
                }
            }
        }
        public void Wifi_start(string ip,int port)//启动连接线程
        {
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            List<object> list = new List<object>();
            list.Add(ip);
            list.Add(port);
            cntthread = new Thread(Wifi_cnt);//启动连接线程
            cntthread.IsBackground = true;
            cntthread.Start(list);
        }
        public void Wifi_stop()//断开wifi连接
        {
            if(readthread != null&&readthread.IsAlive)
            {
                readthread.Abort();
            }      
            if(sendthread!=null&&sendthread.IsAlive)
            {
                sendthread.Abort();
            }
            cntthread.Abort();
            client.Shutdown(SocketShutdown.Both);
            client.Close();
        }
        public void Send_single(String sendmsg)//单次发送数据
        {
            if (client != null && client.Connected == true)
            {
                byte[] buffer = new byte[2048];//发送字符串转数组
                buffer = Encoding.Default.GetBytes(sendmsg);
                client.Send(buffer);
            }
            else
            {
                MessageBox.Show("请先连接wifi后再次尝试");
            }
        }
        private void Send_repeated(object o)//多次发送数据方法
        {
            while(true)
            {
                List<object> list = o as List<object>;
                string sendmsg = (string)list[0];
                int time = (int)list[1];
                byte[] buffer = new byte[2048];//发送字符串转数组
                Thread.Sleep(time);
                buffer = Encoding.Default.GetBytes(sendmsg);
                client.Send(buffer);
            }
        }
        public void Send_times(String sendmsg,int time)//启动多次循环发送线程
        {
            if (client != null && client.Connected == true)
            {
                List<object> list = new List<object>();
                list.Add(sendmsg);
                list.Add(time);
                sendthread = new Thread(Send_repeated);
                sendthread.IsBackground = true;
                sendthread.Start(list);
            }
            else
            {
                MessageBox.Show("请先连接wifi后再次尝试");
            }
        }
        public void Send_stop()//关闭循环发送线程
        {
            if (sendthread != null && sendthread.IsAlive == true)
            {
                sendthread.Abort();
            }         
        }
        private void Read_wifi()//读取数据方法
        {
            while(true)
            {
                if (recvStr.Length > 256)
                {
                    recvStr = "";
                }
                byte[] bytes = new byte[128];
                len = client.Receive(bytes);//接收数据
                recvStr += Encoding.ASCII.GetString(bytes, 0, len);
                valadate(recvStr);//判断数据是否正确
            }                          
        }
        public void Read_data()//启动接收线程
        {  
            if(client!=null&&client.Connected == true)
            {
                readthread = new Thread(Read_wifi);
                readthread.IsBackground = true;
                readthread.Start();
            }
            else
            {
                MessageBox.Show("请先连接wifi后再次尝试");
            }
        }
        public void Read_stop()//关闭接收线程
        {
            if(readthread!=null&&readthread.IsAlive==true)
            {
                readthread.Abort();
            }
        }
        private void valadate(String rxstr)//加入对读取数据的判断
        {
            if (rxstr.Substring(0, 4) == "0030")
            {
                srxStr = recvStr;
                Console.WriteLine(srxStr);
            }
        }
    }
}

这里是我上传的资源,可以免费下载,已经做好了封装的类,拖拽可直接使用【免费】C#通过Socket连接wifi资源-CSDN文库

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值