zoukankan      html  css  js  c++  java
  • 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 { }
            }
        }
    }
  • 相关阅读:
    移动开发 Native APP、Hybrid APP和Web APP介绍
    urllib与urllib2的学习总结(python2.7.X)
    fiddler及postman讲解
    接口测试基础
    UiAutomator2.0 和1.x 的区别
    adb shell am instrument 命令详解
    GT问题记录
    HDU 2492 Ping pong (树状数组)
    CF 567C Geometric Progression
    CF 545E Paths and Trees
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309072.html
Copyright © 2011-2022 走看看