我遇到了一个问题,那就是当我尝试从服务器写入多个客户端时,它一直在写入空字符串。我正在共享我的代码及其多个客户端服务器应用程序,其中客户端可以写入服务器,而服务器可以写入所有正在运行的客户端。写入服务器的客户端安静得很好。但服务器不是:(
服务器代码:
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();
}
}
}
}