编了个模拟停等协议用socket通信的小程序,遇到了一些问题,求指导决

.Net技术 码拜 9年前 (2016-03-11) 817次浏览
学校实验模拟做一个停等实验的程序
本人用C#做的,代码如下
发送方:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static string frmhead;
        public static string frmtail;
        public static string frmdata;
        public static string frmtran;
        public static string[] arry = new string[10];
        public static int i;
        public static int maxi;
        public static int port = 4300;
        public static string host = "127.0.0.1";
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        Socket temp;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            frmhead = "DLESTX";
            frmtail = "DLEETX";
            label1.Text ="";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            s.Bind(ipe);
            s.Listen(0);
            temp = s.Accept();
            button2.Enabled = true;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            i = 0;
            arry = textBox1.Text.Split(" ");
            maxi = arry.Length;
            for (i = 0; i <= maxi - 1; i++)
            {
                frmdata = arry[i];
                frmdata.Replace("DLE", "DLEDLE");
                frmtran = frmhead + " " + frmdata + " " + frmtail;
                byte[] bs = Encoding.ASCII.GetBytes(frmdata);
                temp.Send(bs, bs.Length, 0);
                label1.Text += "已发送\n";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
                label1.Text += "已收到\n";
                string recvStr = "";
                recvStr = Encoding.ASCII.GetString(recvBytes, 0, bytes);
                label2.Text += recvStr;
            }
            button2.Enabled = true;
        }
    }
}

接受:方:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int port = 4300;
            string host = "127.0.0.1";
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            c.Connect(ipe);
            while(true)
            {
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0);
                recvStr = Encoding.ASCII.GetString(recvBytes, 0, bytes);
                label1.Text = label1.Text + recvStr + " ";
                byte[] bs = Encoding.ASCII.GetBytes("1");
                c.Send(bs, bs.Length, 0);
            }
        }
    }
}

运行后发送方可以正常运行,发送数据和接受数据看上去都正常,但接收方的小窗口会未响应,求指导决

解决方案

20

引用 3 楼 darkzch 的回复:
Quote: 引用 2 楼 Libby1984 的回复:

通讯等待阻塞了UI线程。
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
这段应该放到一个新开的Thread里面去执行

不好意思,本人没有学过线程怎么用,你能直接给本人说一下代码吗?

将button2_Click改成下面的试试

button2.Enabled = false;
                System.Threading.Thread sendThread = new System.Threading.Thread(new System.Threading.ThreadStart(new Action(() =>
                {
                     i = 0;
                    arry = textBox1.Text.Split(" ");
                    maxi = arry.Length;
                    for (i = 0; i <= maxi - 1; i++)
                    {
                        frmdata = arry[i];
                        frmdata.Replace("DLE", "DLEDLE");
                        frmtran = frmhead + " " + frmdata + " " + frmtail;
                        byte[] bs = Encoding.ASCII.GetBytes(frmdata);
                        temp.Send(bs, bs.Length, 0);
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            label1.Text += "已发送\n";
                        }));
                        byte[] recvBytes = new byte[1024];
                        int bytes;
                        bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            label1.Text += "已收到\n";
                            string recvStr = "";
                            recvStr = Encoding.ASCII.GetString(recvBytes, 0, bytes);
                            label2.Text += recvStr;
                        }));
                    }
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        button2.Enabled = true;
                    }));
                })));
                sendThread.Start();

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明编了个模拟停等协议用socket通信的小程序,遇到了一些问题,求指导决
喜欢 (0)
[1034331897@qq.com]
分享 (0)