我正在尝试构建一个简单的客户端服务器应用程序,其中服务器一直在监听,客户端可以发送消息。我使用http://brunov.info/blog/2013/02/09/tcpip-client-server-application-exchange-with-string-messages/作为参考。但是,当我在client.exe上的控制台上键入时,没有消息进入服务器并且程序挂起。我究竟做错了什么。请指教。
此致
这是我的Server.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ucSim1
{
class Program
{
static void Main(string[] args)
{
var loaclAddress = IPAddress.Parse("127.0.0.1");
var tcpListener = new TcpListener(loaclAddress, 81);
tcpListener.Start();
while (true)
{
Console.WriteLine("Waiting for a connection");
var tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Client Accepted");
Thread thread = new Thread(() => ClientSession(tcpClient))
{
IsBackground = true
};
thread.Start();
Console.WriteLine( "Client Session thread started");
}
}
private static bool tryRead(Stream stream, byte[] buffer, int offset, int count)
{
int bytesRead;
while (count > 0 && (bytesRead = stream.Read(buffer, offset, count)) > 0)
{
offset += bytesRead;
count -= bytesRead;
}
return count == 0;
}
private static void ClientSession(TcpClient tcpClient)
{
const int totalByteBuffer = 4096;
byte[] buffer = new byte[256];
using (var networkStream = tcpClient.GetStream())
using (var bufferedStream = new BufferedStream(networkStream, totalByteBuffer))
while (true)
{
if (!tryRead(bufferedStream, buffer, 0, 1))
{
break;
}
byte messageLen = buffer[0];
if (!tryRead(bufferedStream, buffer, 1, messageLen))
{
break;
}
var message = Encoding.ASCII.GetString(buffer,1,messageLen);
Console.WriteLine("Message Recieved: {0}", message);
}
}
}
}
这是Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ucSim2
{
class Program
{
private static byte[] msg2ByteArray(string message, Encoding enc)
{
var byteCount = enc.GetByteCount(message);
if (byteCount > byte.MaxValue)
{
throw new ArgumentException("Message size is greater than 255 bytes in the provided encoding");
}
var byteArray = new byte[byteCount+1];
byteArray[0] = (byte)byteCount;
enc.GetBytes(message,0,message.Length,byteArray,1);
return byteArray;
}
static void Main(string[] args)
{
String message;
using (var tcpClient = new TcpClient())
{
tcpClient.Connect("127.0.0.1", 81);
using (var networkStream = tcpClient.GetStream())
using (var bufferedStream = new BufferedStream(networkStream))
{
while (true)
{
byte[] buffer = new byte[256];
Console.WriteLine("Write Message");
message = Console.ReadLine();
var byteArray = msg2ByteArray(message, Encoding.ASCII);
bufferedStream.Write(byteArray, 0, byteArray.Length);
}
}
}
}
}
}
我在发布问题之后想出来...应该花更多时间才能开枪并发布问题... appologies
答案 0 :(得分:2)
发送消息后,您应该拨打Flush()
method:
bufferedStream.Write(byteArray, 0, byteArray.Length);
bufferedStream.Flush(); // add this line
答案 1 :(得分:2)
我不知道你想用BufferedStream做什么,但是当你在networkStream上写字时,它可以工作:
while (true)
{
byte[] buffer = new byte[256];
Console.WriteLine("Write Message");
message = Console.ReadLine();
var byteArray = msg2ByteArray(message, Encoding.ASCII);
networkStream.Write(byteArray, 0, byteArray.Length);
}
和
if (!tryRead(networkStream, buffer, 0, 1))
{
Console.WriteLine("No Message, process canceled.");
break;
}
byte messageLen = buffer[0];
if (!tryRead(bufferedStream, buffer, 1, messageLen))
{
Console.WriteLine("No Message, process canceled.");
break;
}