我正在尝试研究一个简单的tcp客户端/服务器的基础知识,以便我可以实现一个通知系统,该系统几乎可以在任何地方工作,并允许像应用程序这样的“守护程序”进行通信。客户端之一可能是监视器,该监视器查看消息并显示所有重置的状态并进行协调,例如,通过暂停一个或多个,查看已处理了多少文件,数据库的状态等等。首先,我必须有一个非常简单的服务器,该服务器等待来自客户端的消息并将其广播到其他客户端。这是我目前在服务器上的尝试(基于http://csharp.net-informations.com/communications/csharp-chat-server.htm中的示例)。
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Collections;
namespace erc.bre
{
class NotificationServer
{
public static Hashtable clientsList = new Hashtable();
static void Main(string[] args)
{
System.Net.IPAddress addr = System.Net.IPAddress.Parse("127.0.0.1");
TcpListener serverSocket = new TcpListener(addr, 1025);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
Console.WriteLine("Chat Server Started ....");
counter = 0;
while ((true))
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
byte[] bytesFrom = new byte[10025];
clientSocket.ReceiveBufferSize = bytesFrom.Length; // added
string dataFromClient = null;
NetworkStream networkStream = clientSocket.GetStream();
// Console.WriteLine("Bytes received: {0}", (int)clientSocket.ReceiveBufferSize);
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
clientsList.Add(dataFromClient, clientSocket);
broadcast(dataFromClient + " Joined ", dataFromClient, false);
Console.WriteLine(dataFromClient + " Joined chat room ");
handleClinet client = new handleClinet();
client.startClient(clientSocket, dataFromClient, clientsList);
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine("exit");
Console.ReadLine();
}
public static void broadcast(string msg, string uName, bool flag)
{
foreach (DictionaryEntry Item in clientsList)
{
TcpClient broadcastSocket;
broadcastSocket = (TcpClient)Item.Value;
NetworkStream broadcastStream = broadcastSocket.GetStream();
Byte[] broadcastBytes = null;
if (flag == true)
{
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
}
else
{
broadcastBytes = Encoding.ASCII.GetBytes(msg);
}
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
broadcastStream.Flush();
}
} //end broadcast function
}//end Main class
public class handleClinet
{
TcpClient clientSocket;
string clNo;
Hashtable clientsList;
public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
{
this.clientSocket = inClientSocket;
this.clientSocket.ReceiveBufferSize = 10025; // added
this.clNo = clineNo;
this.clientsList = cList;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
rCount = Convert.ToString(requestCount);
NotificationServer.broadcast(dataFromClient, clNo, true);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}//end while
}//end doChat
} //end class handleClinet
}//end namespace
您会注意到Console.WriteLine,我在其中打印出接收到的字节数。我添加了此内容,因为我在下一条声明大小超出范围的异常中得到了例外。它打印出一个408,000字节的数字。
这里是客户端程序,做了很多修改,因为在我的Mac版VisualStudio 2017中,我无法使用Windows窗体来创建任何项目,甚至无法按照示例创建项目。由于一旦确定了机制后就将代码放入Web应用程序中,因此我只是试图让客户端输入其ID,然后键入一行,让服务器选择并重新广播。其他客户端将从命令行执行相同的操作(每个新客户端一个)。它当前接受userId并将其发送。我发送“ John”,它似乎以5个字节发送“ John $”。
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace erc.bre
{
public class NotificationClient
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;
string display = "";
string userName = "";
string message = "";
bool needsInvocation = true;
static NotificationClient nc = new NotificationClient();
public NotificationClient()
{
}
public static int Main(string[] args)
{
nc.msg();
return 0;
}
private void sendUserJoined()
{
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
}
private void InputReceived()
{
readData = "Conected to Chat Server ...";
msg();
clientSocket.Connect("127.0.0.1", 1025);
serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(userName + "$");
Console.WriteLine("Sending {0} bytes as '{1}'", outStream.Length, System.Text.Encoding.Default.GetString(outStream);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
Thread ctThread = new Thread(getMessage);
ctThread.Start();
}
private void getMessage()
{
while (true)
{
serverStream = clientSocket.GetStream();
int buffSize = 0;
byte[] inStream = new byte[10025];
buffSize = clientSocket.ReceiveBufferSize;
serverStream.Read(inStream, 0, buffSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
readData = "" + returndata;
msg();
}
}
private void msg()
{
if (needsInvocation)
{
needsInvocation = false;
Console.Out.WriteLine("Starting Client");
Console.Out.WriteLine("Input your callerID: ");
nc.userName = nc.ReadLine();
nc.InputReceived();
nc.sendUserJoined();
nc.getMessage();
return;
}
display = display + Environment.NewLine + " >> " + readData;
Console.Out.WriteLine(display);
}
private string ReadLine()
{
string line = "";
char ch;
Console.WriteLine("Enter text when desired, 'enter' to end");
do
{
int x = Console.Read();
try
{
ch = Convert.ToChar(x);
if (ch == 0x0a)
{
return line;
}
}
catch (OverflowException e)
{
Console.WriteLine("{0} Value read = {1}.", e.Message, x);
ch = ' ';
}
line += ch;
} while (ch != 0x0a);
return line;
}
}
}
服务器控制台显示:
聊天服务器已启动.... 收到的字节数:408300
未处理的异常:System.ArgumentOutOfRangeException:指定的参数不在有效值范围内。 参数名称:大小 在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) 在/Users/woo/Projects/erc-caml/bre/NotificationServer/NotificationServer.cs:line 33中的ConsoleApplication1.NotificationServer.Main(String [] args)处 bash:第1行:45360中止陷阱:6“ / usr / local / share / dotnet / dotnet”“ /Users/woo/Projects/erc-caml/bre/NotificationServer/bin/Debug/netcoreapp2.1/NotificationServer.dll”
客户端控制台显示:
启动客户端 输入您的来电显示: 根据需要输入文本,“输入”以结束 约翰
已选择连接到聊天服务器... 发送5个字节作为“ John $”
未处理的异常:未处理的异常:System.ArgumentOutOfRangeException:指定的参数不在有效值范围内。 参数名称:大小 在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) 在/Users/woo/Projects/erc-caml/bre/NotificationMonitor/NotificationClient.cs:line 60中的erc.bre.NotificationClient.getMessage() 在/Users/woo/Projects/erc-caml/bre/NotificationMonitor/NotificationClient.cs:line 77
中的erc.bre.NotificationClient.msg()中为什么服务器看到超过40万个字节?它在等待消息时在内部吞噬东西吗?不管我等了多久,这个数字看起来都一样。
答案 0 :(得分:1)
clientSocket.ReceiveBufferSize
返回与接收字节数不匹配的接收缓冲区的大小。使用网络流( serverStream )的 Read 方法并检查其结果,该结果告诉您已读取了多少字节。
有关更多信息,请参见NetworkStream.Read(Byte[], Int32, Int32) Method和TcpClient.GetStream。