C# 而(true)只发生一次,没有错误
这个问题很直截了当。我通过按下按钮操作创建的线程中的while循环只发生一次,“this”不会再次重复。它确实接收到第一条消息,但由于while循环被卡住,因此不会接收到将来的消息。第一条消息不会打印出来 此外,在打印消息后,开关循环不会发生C# 而(true)只发生一次,没有错误,c#,multithreading,sockets,C#,Multithreading,Sockets,这个问题很直截了当。我通过按下按钮操作创建的线程中的while循环只发生一次,“this”不会再次重复。它确实接收到第一条消息,但由于while循环被卡住,因此不会接收到将来的消息。第一条消息不会打印出来 此外,在打印消息后,开关循环不会发生 private void button1_Click(object sender, EventArgs e) { Thread Listener = new Thread(ServerListener);
private void button1_Click(object sender, EventArgs e)
{
Thread Listener = new Thread(ServerListener);
Listener.Start();
}
public void ServerListener()
{
byte[] buf;
NetworkStream serverStream = clientSocket.GetStream();
while (true)
{
Console.WriteLine("this");
if (serverStream.DataAvailable)
{
buf = new byte[clientSocket.ReceiveBufferSize];
serverStream.Read(buf, 0, buf.Length);
string stringdata = Encoding.ASCII.GetString(buf);
Console.WriteLine("Received message: " + stringdata);
switch (stringdata)
{
case "ping":
buf = Encoding.ASCII.GetBytes("pong");
Console.WriteLine("Sent pong");
serverStream.Write(buf, 0, buf.Length);
serverStream.Flush();
break;
case "handshake":
buf = Encoding.ASCII.GetBytes("lol");
Console.WriteLine("Sent confirmation");
serverStream.Write(buf, 0, buf.Length);
serverStream.Flush();
break;
}
}
}
}
我确信会有人来告诉我如何使用任务。我知道如何使用任务,但我看不出长时间运行的任务和普通线程之间有什么区别 好的,现在我发布了有效的代码 客户:
using System;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
TcpClient clientSocket;
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
clientSocket = new TcpClient("127.0.0.1", 8001); // Note the change
Thread Listener = new Thread(ServerListener);
Listener.Start();
}
public void ServerListener()
{
byte[] buf;
Console.WriteLine("Thread started");
NetworkStream serverStream = clientSocket.GetStream();
while (true)
{
Console.WriteLine("this");
if (serverStream.DataAvailable)
{
buf = new byte[clientSocket.ReceiveBufferSize];
serverStream.Read(buf, 0, buf.Length);
string stringdata = Encoding.ASCII.GetString(buf);
Console.WriteLine("Received message: " + stringdata);
switch (stringdata)
{
case "ping":
buf = Encoding.ASCII.GetBytes("pong");
Console.WriteLine("Sent pong");
serverStream.Write(buf, 0, buf.Length);
serverStream.Flush();
break;
case "handshake":
buf = Encoding.ASCII.GetBytes("lol");
Console.WriteLine("Sent confirmation");
serverStream.Write(buf, 0, buf.Length);
serverStream.Flush();
break;
}
}
}
}
}
}
服务器:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
IPAddress IP = IPAddress.Parse("127.0.0.1");
TcpListener Listener = new TcpListener(IP, 8001);
Listener.Start();
Socket s = Listener.AcceptSocket();
Console.WriteLine("Conected");
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("ping"));
Console.WriteLine("Data sent");
s.Close();
Listener.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
Console.ReadKey();
}
}
}
您遇到的问题:
我想是你的服务器或者代码没有给我们看。在服务器上作为控制台应用程序运行,在客户机上作为winform应用程序运行。我了解到我需要对传出数据进行帧处理。有关框架示例,请参见以下代码。成帧告诉接收器预期的数据量
public void SendResponse(int command, Object[] args)
{
if (ClientSocket == null)
{
Console.WriteLine("Command: ClientSocket");
return;
}
var serverStream = this.ClientSocket.GetStream();
if (!serverStream.CanRead || !serverStream.CanWrite)
{
Console.WriteLine("Command: serverStream Error");
return;
}
byte[] toSend = null;
switch (command)
{
// 0 - genneral, 1 - handshake response
case 0:
toSend = Encoding.ASCII.GetBytes(args[0].ToString());
break;
case 1:
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
toSend = ImageToByte(bitmap);
}
break;
}
try
{
//--- Framing occurs here mainly
byte[] bufferedToSend = new byte[toSend.Length + 4];
byte[] lengthOfSend = BitConverter.GetBytes(toSend.Length);
Array.Copy(lengthOfSend, bufferedToSend, 4);
toSend.CopyTo(bufferedToSend, 4);
serverStream.Write(bufferedToSend, 0, bufferedToSend.Length);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
您是否尝试过在调试器中单步执行它以查看它在做什么?您的线程会因异常而死亡。捕获并检查它。我尝试捕获任何异常,如下所述。忽略Read()的返回值永远都是不正确的。实际得到的字节数完全不可预测。接下来会发生什么是胡乱猜测,从来没有什么好的。我添加了一个int来接收读取。。。如果这能让你快乐。尽管这无助于我的循环。我试着抓住它,但什么也没发生,因为没有错误(例外)。