●●●●一个很简单.但估计你也不会的问题●●●●
一个看次简单,但有点难的问题,
本段代码是一个多线程端口通讯的程序.
问题不是端口通讯 也不是多线城的问题,这些我都做出来了.
问题是按了监听 按钮 也就是(Button1)
我本意是要这Button1隐藏,可老是隐藏不了.望大侠们告之! 多谢
请大家测试了再发贴,别乱说哦。
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
public partial class Admin_Info_AddInfo : PageBase
{
public static TcpListener myList;
public static Socket s;
protected void Page_Load(object sender, EventArgs e)
{
}
//启动程序
public void Fist()
{
int prot = Convert.ToInt32(this.TextBox1.Text.Trim());
try
{
IPAddress ipAd = IPAddress.Parse(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString());
myList = new TcpListener(ipAd, prot);
myList.Start();
while (true)
{
s = myList.AcceptSocket();
AcceptSocketClass accept = new AcceptSocketClass(s);
accept.StartThread();
}
}
catch (Exception e)
{
this.Label1.Text = e.Message;
}
}
//监听程序
public class AcceptSocketClass
{
public static int times = 0;
Socket socket;
public AcceptSocketClass(Socket s)
{
socket = s;
}
public void StartThread()
{
byte[] b = new byte[1024];
int k = socket.Receive(b);
if (k > 0)
{
string str = Convert.ToString(Encoding.BigEndianUnicode.GetString(b, 0, k));
//写入数据库
int nmuber = str.LastIndexOf("#");
if (nmuber > 0)
{
//数据库操作
}
else
{
//数据库操作
}
Thread td = new Thread(new ParameterizedThreadStart(DoAction));
td.Start(times);
times++;
}
}
private void DoAction(object t)
{
Thread.Sleep(10000);
ASCIIEncoding asen = new ASCIIEncoding();
socket.Send(asen.GetBytes("The" + ((int)t + 1).ToString() + "th string was Echoed by the server."));
Thread.CurrentThread.Abort();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Button1.Visible = false;
Fist();
}
}