码迷,mamicode.com
首页 > Windows程序 > 详细

C# Socket服务端和客户端通话

时间:2020-07-19 16:18:59      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:监听   指定   art   sys   sockets   start   cli   ssg   输入   

服务端

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

namespace tSocket
{
    class Program
    {
        byte[] bytes = new byte[1024];
        Socket cSocket;
        static void Main(string[] args)
        {
            Program p = new Program();
            //打开链接
            p.open();
            //向服务端发送消息
            Console.WriteLine("请输入你要对服务端发送的消息:");
            string mes = Console.ReadLine();
            string con = p.messge(mes);
            Console.WriteLine("接受到服务端的消息:" + con);


        }
        byte[] data = new byte[1024];
        string messge(string mes)
        {
            //将发送的消息转成字节数组
            bytes = Encoding.UTF8.GetBytes(mes);
            //发送
            cSocket.Send(bytes);
            while (true)
            {
                //接受服务端发送的消息,放入字节数组
                int len = cSocket.Receive(data);
                //将字节数组转成可读明文
                string con = Encoding.UTF8.GetString(data, 0, len);
                ////返回
                return con;
            }
          
        }
        /// <summary>
        /// 打开链接
        /// </summary>
        void open()
        {
            //创建Socket对象 指定连接方式
            cSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //创建IP,端口
            IPAddress ip = IPAddress.Parse("10.116.253.10");
            int port = 7526;
            //封装IP和端口
            IPEndPoint Ipoint = new IPEndPoint(ip, port);
            //打开链接
            cSocket.Connect(Ipoint);
        }
    }
}

客户端

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

namespace ServerSocket
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //创建Socket对象,指定他的链接方式
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //建立IP
            string ip = "10.116.253.10";
            //创建端口
            int prot = 7526;//1~9999
            IPAddress IPAdd = IPAddress.Parse(ip);
            //封装IP和端口
            IPEndPoint point = new IPEndPoint(IPAdd, prot);
            //绑定IP和端口
            serverSocket.Bind(point);
            //开始监听
            serverSocket.Listen(100);
            Console.WriteLine("开始监听!");

            int i = 0;
            while (true)
            {
                i++;
                //接受客户链接
                
               Socket cSocket = serverSocket.Accept();
               Console.WriteLine("接受第"+i+"个客户的连接!");
               Client c = new Client(cSocket);
            }

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

namespace ServerSocket
{
    class Client
    {
        Socket sSocket;
        byte[] data = new byte[1024];
        Thread t;
        public Client(Socket cSocket)
        {
            //接受客户的连接
            sSocket = cSocket;
            //创建线程
            t = new Thread(Mess);
            //开始线程
            t.Start();
        }

        void Mess()
        {
            try
            {
                while (true)
                {
                    //将用户发送的数据以一个字节数组装起
                    int length = sSocket.Receive(data);
                    Console.WriteLine("接受客户端发的消息!");
                    string mess = Encoding.UTF8.GetString(data, 0, length);

                    if (mess == "con")
                    {
                        string con = "DataSource =.";
                        byte[] bytes = Encoding.UTF8.GetBytes(con);
                        sSocket.Send(bytes);
                    }
                    Console.WriteLine("接到用户的消息:" + mess);
                }
            }
            catch (Exception)
            {
                sSocket.Close();
            }



        }
    }
}

 

C# Socket服务端和客户端通话

标签:监听   指定   art   sys   sockets   start   cli   ssg   输入   

原文地址:https://www.cnblogs.com/netlock/p/13339116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!