客户端发送信息给服务器以及服务器接收客户端发来的信息(socket)

服务器端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SOCKET网络编程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnWatch_Click(object sender, EventArgs e)
        {
            //创建一个负责监听的Socket
            Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //创建IP地址和端口号对象
            IPAddress ip = IPAddress.Any;//这句Any是获取自己当前本机的IP,
            //IPAddress ip=IPAdress.Parse(txtIP.Text); 这句是获取textbox里面输入的地址
            IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));//这里是获取文本框里输入的端口号,创建了一个端口对象,需要传入ip跟端口号
            //让负责监听的Socket绑定IP地址跟端口号
            socketWatch.Bind(point);
            ShowMsg("监听成功");
            //设置监听队列
            socketWatch.Listen(10);
            Thread th = new Thread(listen);
            th.IsBackground = true;
            th.Start(socketWatch);

        }
        void listen(object o)
        {
            Socket socketWatch = o as Socket;//as的用法就是如果可以的话,那就直接转换过去
            while (true)
            {
                //负责监听的Socket 来接收客户端的连接 创建跟客户端通信的Socket
                Socket socketSend = socketWatch.Accept();
                //如果接收成功,把发送端的地址显示出来
                ShowMsg(socketSend.RemoteEndPoint.ToString() + "连接成功");
                //客户端连接成功后,服务器应该接收客户端发来的消息
                //开启一个新线程,不停的接收客户端发来的信息
                Thread th = new Thread(Recive);
                th.IsBackground = true;
                th.Start(socketSend);

            }
        }
        /// <summary>
        /// 服务器端接收客户端发来的信息
        /// </summary>
        /// <param name="o"></param>
        void Recive(object s)
        {
            Socket socketSend = s as Socket;
            while (true)
            {
                //客户端连接成功后,服务器应该接收客户端发来的消息
                byte[] buffer = new byte[1024 * 1024 * 5];
                int r = socketSend.Receive(buffer); //实际接收到的字节数
                if(r==0)
                {
                    break;
                }
                string str = Encoding.UTF8.GetString(buffer, 0, r);//把接收到的字节转成字符串
                ShowMsg(socketSend.RemoteEndPoint + ":" + str);
            }
        }
        void ShowMsg(string str)
        {
            txtShow.AppendText(str + "\r\n");
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}
View Code

 

 

 

客户端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 接收端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket sok = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private void btnWatch_Click(object sender, EventArgs e)
        {

            IPAddress ip = IPAddress.Parse(txtIP.Text);
            IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
            sok.Connect(point);
            ShowMsg("连接成功");





        }
        void ShowMsg(string str)
        {
            MessageBox.Show(str + "\r\n");
        }
        /// <summary>
        /// 客户端给服务器发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string str = txtSend.Text.Trim();
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            sok.Send(buffer);

        }
    }
}
View Code

 

posted @ 2021-08-27 22:51  静态类  阅读(258)  评论(0编辑  收藏  举报