我什至不好意思问这个问题,但是在谷歌搜索完之后(开始有 MSDN ......),我决定发布它:
刚刚开始学习客户端-服务器编程(使用 C#),并尝试使用 tcpClient 编写我的第一个代码。我正在编写服务器端和客户端。这是我的问题:客户端不断地向服务器发送一个字符串,然后服务器将一个字符串发送回客户端,依此类推。服务器不能连续发送2个字符串吗?他必须等待客户响应?这是客户端-服务器的原理吗??
再次,对不起这个糟糕的问题。谢谢...
<> 我会尝试发布我的一些代码(很长的......)。我试着剪掉多余的部分,所以希望代码有意义......(我用//*********标记了主要问题)
public class MServer2
{
Dictionary<String, String> nameAndPass = new Dictionary<String, String>();
Dictionary<String, List<String> > nameAndMail = new Dictionary<String, List<String>>();
public static void Main()
{
new MServer2();
}
public MServer2()
{
TcpListener server = new TcpListener(8500);
try
{
server.Start();
Console.WriteLine("started " + server);
while (true)
{
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("connection accepted " + client);
new Server1(client, nameAndPass, nameAndMail);
}
}
catch (Exception e)
{
Console.WriteLine("exception" + e);
}
finally
{
server.Stop();
}
}
class Server1
{
TcpClient client;
NetworkStream netStream;
Dictionary<String, String> nameAndPass1 = new Dictionary<String, String>();
Dictionary<String, List<String>> nameAndMail1 = new Dictionary<String, List<String>>();
internal Server1(TcpClient client, Dictionary<String, String> nameandPassFromFile, Dictionary<String, List<String> > nameAndMailsFromFile)
{
nameAndPass1 = nameandPassFromFile;
nameAndMail1 = nameAndMailsFromFile;
this.client = client;
Thread thr = new Thread(new ThreadStart(Run));
thr.Start();
}
public void Run()
{
try
{
netStream = client.GetStream();
StreamReader reader = new StreamReader(netStream);
StreamWriter writer = new StreamWriter(netStream);
writer.AutoFlush = true;
Console.WriteLine("beginning to receive loop");
writer.WriteLine("Choose your user name.");
strFromClient = reader.ReadLine();
userName = strFromClient;
writer.WriteLine("Choose your user password.");
strFromClient = reader.ReadLine();
password = strFromClient;
writer.WriteLine("Do you want to see the list of email addresses? (y/n)");
strFromClient = reader.ReadLine();
//***********************************************************************************
//HERE'S MY PROBLAM:
//HERE THE CLIENT WILL GET A STRING SHOWING HIS EMAILS, AND I WANT HIM TO GET ANOTHER STRING ASKING "Do you want to add an email address? (y/n)", BUT IT LOOKS LIKE THE SERVER "WAITS" FOR A RESPONSE FROM THE CLIENT TO SHOW THE NEXT STRING...
if (strFromClient == "y")
{
String tmpStr = null;
List<String> tmp = nameAndMail1[userName];
for(int i=0; i<nameAndMail1[userName].Count; i++)
{
tmpStr += tmp[i] + " ";
}
writer.WriteLine(tmpStr);
}
writer.WriteLine("Do you want to add an email address? (y/n)");
strFromClient = reader.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
编辑第 2 卷
好的!经过 2 小时的痛苦,我想我找到了问题,这要归功于Phil Frost(天才!)——问题可能出在客户身上……(我真是个大坑!)。
服务器确实连续发送了 2 个字符串,但是我愚蠢的客户端实现没有显示不遵循客户端发送的消息的消息(从服务器接收)...
所以我再次需要你的帮助。这是我如何设计客户表单的视图:
我缺乏经验导致我在按下“连接到服务器”按钮时连接到服务器,并且只有当按下“发送消息”按钮时,才会显示来自服务器的消息。问题是当接收到来自服务器的 2 条(或更多)消息而没有从客户端向服务器发送消息时 - 客户端不知道接收到了新消息!我应该在哪里接收来自服务器的消息?('where' 表示在哪个函数下,例如 - 现在它发生在 sendMessage_click 函数中)。
再次感谢到目前为止的所有帮助!