学校实验模拟做一个停等实验的程序
本人用C#做的,代码如下
发送方:
本人用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
将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();