设为首页 加入收藏
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 企业资讯 服务专区 客服中心
服务项目
案例展示 更多 >>
·生产流程监控 2020/11/26
·上位机软件定制开发 2020/9/29
·手机端公众号集成开发 2020/6/5
·业务技能培训学习软件 2020/6/5
·润宇司法行政管理综合平… 2010/12/6
·连锁店管理系统软件 2010/7/31
·医院管理系统软件 2010/7/27
·驾校查询系统软件 2010/7/15
·生产管理系统软件 2010/1/30
·人事管理系统软件 2010/1/30
联系人:李先生
电  话:029-87878512
手  机:13468700578
地  址:西安市丝路国际创意梦工厂4号楼
在线咨询:  762176615
Email:junsoft@126.com
 
当前的位置 >> 返回首页 >> 解决方案
软件定制开发中Socket通信
发布者:西安软件公司   发布时间:2023/4/1   阅读:次

可以看到,总的工作流程就是:

  1. 服务端监听
  2. 客户端(可以有多个)请求连接
  3. 确认连接进行通信

针对服务器端的工作流程是:

  1. 建立Socket对象
  2. Bind(ip, port)绑定端口。比如说本地的(127.0.0.1, 6666)
  3. Listen()开始监听
  4. Accept()接受客户端连接
  5. Send()和Receive()进行通信
  6. Close()关闭套接字

而针对客户端的工作流程是:

  1. 建立Socket对象
  2. Connet(ip, port)向某个IP下的PORT端口发起连接请求,接受后开始通信
  3. Send()和Receive()进行通信
  4. Close() 关闭套接字

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace socket
{
    class Program
    {

        static Socket serverSocket;
      
       

        static void Main(string[] args)
        {
            SetListen();  //开始监听

        }

        static void SetListen()
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                //这里最好改成IPAddress.Any, 这样放到服务器上就会同时监听所有网卡上的端口,比如有内外双网的服务器
                serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
                serverSocket.Listen(2);

                Thread thread = new Thread(Listen);
                thread.Start(serverSocket);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
           
        }

        static void Listen(object so)
        {
            Socket serverSocket = so as Socket;

            while (true)
            {
                try
                {
                    Socket clientSocket = serverSocket.Accept(); //接受客户端接入
                    // 获取链接IP地址
                    string clientPoint = clientSocket.RemoteEndPoint.ToString();

                    //开启新线程来不停接受信息
                    Thread rec = new Thread(Receive);
                    rec.Start(clientSocket);

                    //开启新线程来不停发送信息
                    Thread sen = new Thread(Send);
                    sen.Start(clientSocket);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message, ex);
                    break;
                }
            }
            serverSocket.Close();
        }

        static void Receive(object so)
        {
            Socket clientSocket = so as Socket;

            string clientPoint = clientSocket.RemoteEndPoint.ToString();
            while (true)
            {
                try
                {
                    byte[] buffer = new byte[1024];
                    int len = clientSocket.Receive(buffer);
                    //Console.WriteLine(len.ToString(), buffer);
                    if (len == 0) break;

                    string msg = Encoding.UTF8.GetString(buffer, 0, len);
                    Console.WriteLine("客户端说:"+ msg);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }

        static void Send(object so)
        {
            Socket clientSocket = so as Socket;

            while (true)
            {
                //获取控制台输入
                string input = Console.ReadLine();

                byte[] msg = Encoding.UTF8.GetBytes(input);
                clientSocket.Send(msg);
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

 

namespace socket_client
{
    class Program
    {
        static Socket clientSocket;

        static void Main(string[] args)
        {
           
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
           
            //连接
            Connect();
        }

        //连接客户端,连接后开始监听发送过来的消息并且把输入的信息发给服务端
        static void Connect()
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
           
            try
            {
                //连接
                clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));

                //独立线程来接受来自服务端的数据
                Thread receive = new Thread(Receive);
                receive.Start(clientSocket);

                //独立线程来发送数据给服务端
                Thread send = new Thread(Send);
                send.Start(clientSocket);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static void Receive(object so)
        {
            Socket clientSocket = so as Socket;


            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Green;  // 修改文字颜色为绿色
                try
                {
                    byte[] buffer = new byte[1024];
                    int len = clientSocket.Receive(buffer);
                    if (len > 0)
                    {
                        string msg = Encoding.UTF8.GetString(buffer);
                       
                        Console.WriteLine("服务端说:"+ msg);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    break;
                }
            }
            clientSocket.Close();
        }

        static void Send(object so)
        {
            Socket clientSocket = so as Socket;

            while (true)  // 监听键盘输入
            {
                Console.ForegroundColor = ConsoleColor.Red;
                //获取键盘输入
                string input = Console.ReadLine();
                //编码
                byte[] buffer = Encoding.UTF8.GetBytes(input);
                //发送
                clientSocket.Send(buffer);
            }
        }
    }
}

 


网站首页 | 关于我们 | 售后服务 | 网站地图 | 查看留言 | 在线留言 | 客服中心
© 版权所有:西安润宇软件科技有限公司 
公司地址:西安市丝路国际创意梦工厂4号楼 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2011-2023 Xbwbw.com Inc. All Rights Reserved 
技术支持:西安润宇软件科技有限公司  陕ICP备11000720号