我遇到了一個問題,那就是當我嘗試從我的服務器寫入到我的多個客戶端時,它不斷寫入空字符串。我分享我的代碼和它的多客戶端服務器應用程序,客戶端可以寫入服務器,而服務器可以寫入所有正在運行的客戶端。客戶寫入服務器安靜罰款。但服務器是不是:(Tcp客戶端服務器窗體應用程序問題
Server代碼:
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.IO;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
namespace ServerGui
{
public partial class Form1 : Form
{
TcpListener tcpListen;
Thread listenThread;
String read = "";
ArrayList collect = new ArrayList();
int counter = 0;
ArrayList NS = new ArrayList();
TcpClient general = null;
readonly ManualResetEvent mre = new ManualResetEvent(false);
public Form1()
{
InitializeComponent();
}
private void connectServerToolStripMenuItem_Click(object sender, EventArgs e)
{
tcpListen = new TcpListener(IPAddress.Any, 3000);
listenThread = new Thread(new ThreadStart(ListenForClients));
listenThread.Start();
connectServerToolStripMenuItem.Enabled = false;
}
public void ListenForClients()
{
tcpListen.Start();
while (true)
{
TcpClient client = tcpListen.AcceptTcpClient();
collect.Insert(counter, client);
general = (TcpClient)collect[counter];
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate { textBox1.AppendText(client.Client.LocalEndPoint.ToString() + " Connected.");
textBox1.AppendText(Environment.NewLine); }));
}
else
{
textBox1.AppendText(client.Client.LocalEndPoint.ToString() + " Connected.");
textBox1.AppendText(Environment.NewLine);
}
Thread clientThread = new Thread(new ParameterizedThreadStart(Reader));
clientThread.Start(collect[counter]);
Thread clientThread1 = new Thread(new ParameterizedThreadStart(Writer));
clientThread1.Start(collect[counter]);
counter++;
}
}
public void Reader(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
StreamReader sr = new StreamReader(clientStream);
while (true)
{
try
{
read = sr.ReadLine();
}
catch
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate
{
textBox1.AppendText(tcpClient.Client.LocalEndPoint.ToString() +
" disconnected.");
textBox1.AppendText(Environment.NewLine);
}));
}
else
{
textBox1.AppendText(tcpClient.Client.LocalEndPoint.ToString() + " disconnected.");
textBox1.AppendText(Environment.NewLine);
}
return;
}
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate
{
textBox1.AppendText("<" + tcpClient.Client.LocalEndPoint.ToString() + "> saying: " + read);
textBox1.AppendText(Environment.NewLine);
}));
}
else
{
textBox1.AppendText("<" + tcpClient.Client.LocalEndPoint.ToString() + "> saying: " + read);
textBox1.AppendText(Environment.NewLine);
}
}
tcpClient.Close();
}
public void Writer(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream ns = tcpClient.GetStream();
mre.WaitOne();
while (true)
{
StreamWriter sw = new StreamWriter(ns);
string str = textBox2.Text;
sw.WriteLine(str);
sw.Flush();
if (textBox2.InvokeRequired)
{
textBox2.Invoke(new MethodInvoker(delegate
{
textBox2.Clear();
}));
}
else
textBox2.Clear();
}
tcpClient.Close();
}
private void button1_Click(object sender, EventArgs e)
{
mre.Set();
textBox1.AppendText(textBox2.Text);
textBox1.AppendText(Environment.NewLine);
textBox2.Clear();
}
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
mre.Set();
textBox1.AppendText(textBox2.Text);
textBox1.AppendText(Environment.NewLine);
textBox2.Clear();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
客戶端代碼:
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;
using System.Threading;
using System.IO;
namespace ClientGcui
{
public partial class Form1 : Form
{
NetworkStream general = null;
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void connectToServerToolStripMenuItem_Click(object sender, EventArgs e)
{
String str = "";
TcpClient client = new TcpClient();
IPEndPoint server = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
client.Connect(server);
textBox1.AppendText("Connected to server");
textBox1.AppendText(Environment.NewLine);
NetworkStream clientStream = client.GetStream();
general = clientStream;
StreamReader sr = new StreamReader(clientStream);
StreamWriter sw = new StreamWriter(clientStream);
Thread reader = new Thread(new ParameterizedThreadStart(readserver));
reader.Start(sr);
}
public void readserver(object client)
{
StreamReader sr = (StreamReader)client;
string read = "";
while (true)
{
try
{
read = sr.ReadLine();
}
catch
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate
{
textBox1.AppendText("Disconnected from Server.");
textBox1.AppendText(Environment.NewLine);
}));
}
else
{
textBox1.AppendText("Disconnected from Server.");
textBox1.AppendText(Environment.NewLine);
}
}
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate
{
textBox1.AppendText(sr.ReadLine());
textBox1.AppendText(Environment.NewLine);
}));
}
else
{
textBox1.AppendText(sr.ReadLine());
textBox1.AppendText(Environment.NewLine);
}
}
sr.Close();
}
private void button1_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(general);
sw.WriteLine(textBox2.Text);
sw.Flush();
textBox1.AppendText(textBox2.Text);
textBox1.AppendText(Environment.NewLine);
textBox2.Clear();
}
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
StreamWriter sw = new StreamWriter(general);
sw.WriteLine(textBox2.Text);
sw.Flush();
textBox1.AppendText(textBox2.Text);
textBox1.AppendText(Environment.NewLine);
textBox2.Clear();
}
}
}
}
好朋友,在閱讀完整答案之前,我得告訴你這是我在stackoverflow.com上的第二個問題。我不知道規則,我不知道如何正確格式化代碼。我試圖儘快擺脫問題,所以我沒有任何修改就複製了整個代碼。我對此表示歉意。感謝您閱讀我的問題並回答問題。希望我能在這裏得到我的解決方案。如果我問你你的Facebook,你似乎是一位專家嗎? – 2012-04-15 12:08:24