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

darkzch 2016-05-18 02:14:25
学校实验模拟做一个停等实验的程序
我用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);
}
}
}
}


运行后发送方可以正常运行,发送数据和接受数据看上去都正常,但接收方的小窗口会未响应,求解决
...全文
271 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
by_封爱 版主 2016-05-19
  • 打赏
  • 举报
回复
http://www.cnblogs.com/chenxizhang/archive/2011/09/10/2172994.html LZ看下上面的链接,都是最基础的都有讲解很不错...适合socket入门
  • 打赏
  • 举报
回复
引用 3 楼 darkzch 的回复:
[quote=引用 2 楼 Libby1984 的回复:] 通讯等待阻塞了UI线程。 byte[] recvBytes = new byte[1024]; int bytes; bytes = temp.Receive(recvBytes, recvBytes.Length, 0); 这段应该放到一个新开的Thread里面去执行
不好意思,我没有学过线程怎么用,你能直接给我说一下代码吗?[/quote] 将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();
darkzch 2016-05-18
  • 打赏
  • 举报
回复
引用 2 楼 Libby1984 的回复:
通讯等待阻塞了UI线程。 byte[] recvBytes = new byte[1024]; int bytes; bytes = temp.Receive(recvBytes, recvBytes.Length, 0); 这段应该放到一个新开的Thread里面去执行
不好意思,我没有学过线程怎么用,你能直接给我说一下代码吗?
  • 打赏
  • 举报
回复
通讯等待阻塞了UI线程。 byte[] recvBytes = new byte[1024]; int bytes; bytes = temp.Receive(recvBytes, recvBytes.Length, 0); 这段应该放到一个新开的Thread里面去执行
为轮子而生 2016-05-18
  • 打赏
  • 举报
回复
因为你等待Receive的过程放在了UI线程,所以会阻塞UI线程的消息,导致界面无法刷新。用多线程来实现通信,尤其是Receive()。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧