直接上代码 请帮我看看 怎么改?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.Sockets;
using System.Net;
using System.Threading;
using System.Timers;namespace ElecMenuServerApp
{
public partial class Form1 : Form
{
private int listenPort = 8080;
private static int buffer_size = 1024;
private static byte[] receiveBytes = new byte[buffer_size];
private static int bytesCount; private Socket serverSokect = null;
private static Socket client = null; private List<string> datas = new List<string>();
public Form1()
{
InitializeComponent(); startRecBtn.Enabled = true;
stopRecBtn.Enabled = false;
} private void startRecBtn_Click(object sender, EventArgs e)
{
Thread recThread = new Thread(new ThreadStart(Receive));
recThread.Start();
recThread.IsBackground = true; startRecBtn.Enabled = false;
stopRecBtn.Enabled = true;
} private void stopRecBtn_Click(object sender, EventArgs e)
{
if (serverSokect != null)
{
serverSokect.Close();
serverSokect = null;
} startRecBtn.Enabled = true;
stopRecBtn.Enabled = false;
} private void Receive()
{
try
{
serverSokect = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, listenPort);
serverSokect.Bind(ipe);//绑定
serverSokect.Listen(100); while (true)
{
client = serverSokect.Accept(); bytesCount = client.Receive(receiveBytes, receiveBytes.Length, 0);
string data = "收到信息:" + "\n" + Encoding.UTF8.GetString(receiveBytes, 0, bytesCount);
//
if (data != null)
{
datas.Add(data);
}
this.Invoke(new AddCon(addcon));
//
client.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
private delegate void AddCon();
private void addcon()
{
if (datas.Count > 0)
{
for (int i = 0; i < datas.Count; i++)
{
RichTextBox rTextBox = new RichTextBox();
rTextBox.Location = new Point(3, 21 + i * 105);
rTextBox.Size = new Size(450, 100);
rTextBox.BorderStyle = BorderStyle.FixedSingle;
rTextBox.ScrollBars = RichTextBoxScrollBars.Both;
rTextBox.Text = datas[i];
rTextBox.Show();
if (panel1.Controls.Count < datas.Count)
{
panel1.Controls.Add(rTextBox);
}
}
}
} private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
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.Sockets;
using System.Net;
using System.Threading;
using System.Timers;namespace ElecMenuServerApp
{
public partial class Form1 : Form
{
private int listenPort = 8080;
private static int buffer_size = 1024;
private static byte[] receiveBytes = new byte[buffer_size];
private static int bytesCount; private Socket serverSokect = null;
private static Socket client = null; private List<string> datas = new List<string>();
public Form1()
{
InitializeComponent(); startRecBtn.Enabled = true;
stopRecBtn.Enabled = false;
} private void startRecBtn_Click(object sender, EventArgs e)
{
Thread recThread = new Thread(new ThreadStart(Receive));
recThread.Start();
recThread.IsBackground = true; startRecBtn.Enabled = false;
stopRecBtn.Enabled = true;
} private void stopRecBtn_Click(object sender, EventArgs e)
{
if (serverSokect != null)
{
serverSokect.Close();
serverSokect = null;
} startRecBtn.Enabled = true;
stopRecBtn.Enabled = false;
} private void Receive()
{
try
{
serverSokect = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, listenPort);
serverSokect.Bind(ipe);//绑定
serverSokect.Listen(100); while (true)
{
client = serverSokect.Accept(); bytesCount = client.Receive(receiveBytes, receiveBytes.Length, 0);
string data = "收到信息:" + "\n" + Encoding.UTF8.GetString(receiveBytes, 0, bytesCount);
//
if (data != null)
{
datas.Add(data);
}
this.Invoke(new AddCon(addcon));
//
client.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
private delegate void AddCon();
private void addcon()
{
if (datas.Count > 0)
{
for (int i = 0; i < datas.Count; i++)
{
RichTextBox rTextBox = new RichTextBox();
rTextBox.Location = new Point(3, 21 + i * 105);
rTextBox.Size = new Size(450, 100);
rTextBox.BorderStyle = BorderStyle.FixedSingle;
rTextBox.ScrollBars = RichTextBoxScrollBars.Both;
rTextBox.Text = datas[i];
rTextBox.Show();
if (panel1.Controls.Count < datas.Count)
{
panel1.Controls.Add(rTextBox);
}
}
}
} private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
解决方案 »
- Chart图表转换成二进制流的二进制流如何指定到div中
- C#中以下代码运行不显示行号,帮忙诊诊,谢谢
- 点击FlowLayoutPanel容器中的某一个控件,FlowLayoutPanel容器得到焦点,能够实现滚轮控制滚动条.......
- 关于DataList问题!
- Winform 和 Webform读取配置文件的区别!
- 在net中如何调用Win32 API : GetBitmapBits
- C#辅助工具开发工程师,工作地点:上海
- 在vs2005环境中,窗体上有控件AxWindowsMediaPlayer,但一播放视频时就在窗体外出现播放器,怎么在窗体上播放啊?
- 急求精确求出100的阶乘的详细方法
- sos!!!怎样在点击按钮时改变它的style再点击其他的按钮是该按钮又恢复它原来的style
- 亟待解决邮件附件下载的问题
- 找不到问题了,大神来看下
报错倒是没有报错,就是每当我发送信息过来的时候,必须最小化然后最大化窗口过后才有反应。
还有就是 当datas.count > 1的时候 richtextbox永远只显示一个 发再多的信息过来也没用
楼主 你的panel1里面每次都添加进去了空间 只是他们的坐标都是相同的 这些控件被覆盖掉了
for (int i = 0; i < datas.Count; i++)rTextBox.Location = new Point(3, 21 + i * 105);if (panel1.Controls.Count < datas.Count)
你看下这三条代码
进入循环后 i=0 当i=0时 你的if语句一定是panel1.Controls.Count < datas.Count 这个你应该能理解吧 先有传值过来 然后在生成控件 而且一定只比datas.Count小1 所以坐标就是(3,21)
当i=1 或i=2,=3.... 此时因为你的panel1里面又添加了一个控件 自然 就不会再小于 datas.Count了后面的循环 if里面的比较都是相等的 所以 你每次进入这个循环 只有i=0的时候才有效 而i=0 坐标就又是(3,21) 控件重复
你改下将rTextBox.Location = new Point(3, 21 + i * 105); 改成rTextBox.Location = new Point(3, 21 + (datas.Count-1) * 105);
但是还有个问题 现在是判断panel1.controls.count < datas.count
如果我在panel1里面除了添加richtextbox空间 我还要添加button label等空间 那怎么判断了?
如果去掉循环 当datas.count > 0 并且没有数据进来的时候 ,最后一个窗口岂不是一直重复叠加?
你就定义一个静态变量static judge=0 在Receive方法中
if (data != null)
{
datas.Add(data);
judge=1;
}
else
{
judge=0;
}
然后那个 if (datas.Count > 0)
用if(judge==1) 来替代