C#,在Client-Server伺服器架构中,server里ListenClient的str1值无法显示在Form1的textbox中??
为什么下图1中显示的回传信息,在图2中窗体结构里,server的listenclient中的str1值,运行时无法回传显示在Textbox中?在Form1中:
private void button1_Click(object sender, EventArgs e)
{
try
{ ………………
textBox1.Text = "Server started at: " + serverIP.ToString() + ":" + Port;
ListenClient lc = new ListenClient(serverSocket,str1);
// 执行绪
ThreadStart serverThreadStart = new ThreadStart(lc.ServerThreadProc);
textBox1.Text = textBox1.Text + " \r\n" + str1;
Thread serverthread = new Thread(serverThreadStart);
serverthread.Start();
}
在ListenClient中:
namespace ServerSocket
{
class ListenClient
{
public string str1;
private System.Net.Sockets.Socket serverSocket;
private System.Net.Sockets.Socket clientSocket;
public ListenClient(Socket serverSocket, String str1)
{
this.serverSocket = serverSocket;
this.str1 = str1;
}
public void ServerThreadProc()
{
while (true)
{
try
{
clientSocket = serverSocket.Accept();
IPEndPoint serverInfo = (IPEndPoint)serverSocket.LocalEndPoint;
IPEndPoint clientInfo = (IPEndPoint)clientSocket.RemoteEndPoint;
str1= "Server: " + serverInfo.Address.ToString() + ":" + serverInfo.Port.ToString() + "\r\n";
str1+="Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString()+"\r\n";
…………
} --------------------编程问答-------------------- 把你的代码修改如下试试:
// 执行绪
ThreadStart serverThreadStart = new ThreadStart(lc.ServerThreadProc);
Thread serverthread = new Thread(serverThreadStart);
serverthread.Start();
textBox1.Text = textBox1.Text + " \r\n" + lc.str1;
--------------------编程问答-------------------- 试过了,窗体的Textbox里还是无法显示相关的内容~
补充:.NET技术 , C#