我正在尝试在 winforms 中构建一个基本的服务器客户端应用程序。但是服务器什么也不做。如果我可以这么说,只是有点打开并挂起。我究竟做错了什么。我制作的应用程序如下:Server Winform
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 ServerWinForms
{
public partial class Form1 : Form
{
public delegate void AddText(TcpClient tcp, RichTextBox rtb);
public AddText myDelegate;
Thread myThread;
public Form1()
{
InitializeComponent();
myDelegate = new AddText(ClientSession);
}
void begin(Object obj)
{
var loaclAddress = IPAddress.Parse("127.0.0.1");
var tcpListener = new TcpListener(loaclAddress, 81);
tcpListener.Start();
while (true)
{
var tcpClient = tcpListener.AcceptTcpClient();
Form1 myForm1 = (Form1)obj;
myForm1.Invoke(myForm1.myDelegate);
//rtb.AppendText("Waiting for connection ");
// Console.WriteLine("Waiting for a connection");
//rtb.AppendText("Client Accepted ");
//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;
}
public static void ClientSession(TcpClient tcpClient, RichTextBox rtb)
{
const int totalByteBuffer = 4096;
byte[] buffer = new byte[256];
// UC ucObj = new UC();
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);
RichTextBox rcb = new RichTextBox();
rtb.AppendText(message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
myThread = new Thread(begin);
myThread.Start(this);
//begin();
}
}
}
客户端 Winform(虽然我相信这里一切正常,但仍然......)
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.IO;
namespace ClientWinForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
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;
}
void sendMsg()
{
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 = richTextBox.Text;
var byteArray = msg2ByteArray(message, Encoding.ASCII);
bufferedStream.Write(byteArray, 0, byteArray.Length);
bufferedStream.Flush();
//}
}
}
}
private void btnSend_Click(object sender, EventArgs e)
{
sendMsg();
}
}
}