• Socket-客户端向服务器端发送消息


    客户端:

    界面:

     代码:

    复制代码

    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;
    
    namespace Test2.Client
    {
        public partial class Form1 : Form
        {
            Socket clientSocket;
            IPEndPoint ipEndPoint;
            public Form1()
            {
                InitializeComponent();
                button1.Text = "连接";
                button2.Text = "发送";
                button3.Text = "关闭";
            }
            private void button1_Click(object sender, EventArgs e)
            {
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ipEndPoint = new IPEndPoint(IPAddress.Parse("xxxxxxxxx"), 52555);
                clientSocket.Connect(ipEndPoint);
            }
    
            private void button2_Click(object sender, EventArgs eCiHoybeegLjsa)
            {
                clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(textBox1.Text));
                
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                SafeClose(clientSocket);
            }
            public  void SafeClose( Socket socket)
            {
                if (socket == null)
                    return;
    
                if (!socket.Connected)
                    return;
    
                try
                {
                    socket.Shutdown(SocketShutdownsEoqRziHLTgBkjWjs.Both);
                }
                catch
                {
                }
    
                try
                {
                    socket.Close();
                }
                catch
                {
                }
            }
        }
    }

    复制代码

    服务器端:

    界面:

     代码:

    复制代码

    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;
    
    namespace Test2.Server
    {
        public partial class Form1 : Form
        {
      
            public Form1()
            {
                InitializeComponent();
                this.FormClosed += Form1_FormClosed;
            }
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                SafeClose(socketSend);
            }
            public void SafeClose( Socket socket)
            {
                if (socket == null)
                    return;
    
                if (!socket.Connected)
                    return;
    
                try
                {
                    socket.Shutdown(SocketShutdown.Both);
                }
                catch
                {
                }
    
                try
                {
                    socket.Close();
                }
                catch
                {
                }
            }
    
    
            Socket socketSend;
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    //点击开始监听时 在服务端创建一个负责监听IP和端口号的Socket
                    Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPAddress ip = IPAddress.Any;                //创建对象端口
                    IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(52555));
                    socketWatch.Bind(point);//绑定端口号
                    MessageBox.Show("Watching");
                    socketWatch.Listen(10);//设置监听
                                           //创建监听线程
                    Thread thread = new Thread(Listen);
                    thread.IsBackground = true;
                    thread.Start(socketWatch);
                }
                catch { }
    
            }
    
            void Listen(object o)
            {
                try
                {
                    Socket socketWatch = o as Socket;
                    while (true)
                    {
                        socketSend = socketWatch.Accept();//等待接收客户端连接
                        //ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功!");
                        //开启一个新线程,执行接收消息方法
                        Thread r_thread = new Thread(Received);
                        r_thread.IsBackground = true;
                        r_thread.Start(socketSend);
                    }
                }
                catch { }
            }
    
            void Received(object o)
            {
                try
                {
                    Socket socketSend = o as Socket;
                    while (true)
                    {
                        //客户端连接服务器成功后,服务器接收客户端发送的消息
                        byte[] buffer = new byte[1024 * 1024 * 3];
                        //实际接收到的有效字节数
                        int len = socketSend.Receive(buffer);
                        if (len == 0)
                        {
                            break;
                        }
                        string str = Encoding.UTF8.GetString(buffer, 0, len);
    
                        this.Invoke(new Action(() =>
                        {
                            listBox1.Items.Add(str + "
    ");
                        }));
                    }
                }
                catch { }
            }
        }
    }
  • 相关阅读:
    C语言字符编码处理
    Linux gperf命令
    C语言命令行处理
    深入分析Java ClassLoader原理
    微软Build2016:Xamarin杂记
    Ubuntu下配置Tomcat以指定(非root)身份执行
    Android之弹出多级菜单
    OC基础:Date
    Linux IO 多路复用是什么意思?
    c语言实现输出一个数的每一位
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309072.html
Copyright © 2020-2023  润新知