我的TCP服务有问题
本文关键字:有问题 服务 TCP 我的 | 更新日期: 2023-09-27 18:07:54
我有一个c#服务,它创建了一个TCP服务器来接收来自iPad的消息。
它在我的联想电脑上运行得很好,没有任何问题,但当安装在我的目标电脑上时,它根本无法运行。这可能是什么原因呢?
我是开放的任何和所有的想法,因为我已经试图弄清楚了一个星期。这是两个非常相似的平台上完全相同的代码,但其中一个不起作用。protected override void OnStart(string[] args)
{
string programStarter = "Program Started " + System.DateTime.Now.ToString();
//Code that is supposed to create a log file in the application directory.
ProgramLogger.logPageCreator();
ProgramLogger.Log("Log page created/Found");
ProgramLogger.Log(programStarter);
//This finds the arduino port and sets current com to something other than nill
while (ArduinoLandLine.currentCOM == null)
{
ArduinoLandLine.AutodetectArduinoPort();
}
if (ArduinoLandLine.currentCOM != null)
{
//Log the new found com port and send it to log file.
ProgramLogger.Log("Arduino Found! " + ArduinoLandLine.currentCOM);
}
BWTCPListener.DoWork += TCPTalker.messageScanner;
BWTCPListener.RunWorkerAsync();
}
这是我的OnStart代码,每次运行完美。下面是tcpllistener代码:
public static void messageScanner(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
TCPResponse responseMessage = new TCPResponse();
TcpListener server = null;
try
{
Int32 port = 13000;
IPAddress localAddress = IPAddress.Parse("The Correct IP Address");
server = new TcpListener(localAddress, port);
server.Start();
Byte[] bytes = new Byte[256];
string data = null;
while (true)
{
Console.Write("waiting for connection.....");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("conntected to : " + localAddress);
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Flush();
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Recieved: " + data);
ProgramLogger.Log("iPad Message recieved: " + data);
if (data != null) Console.WriteLine(responseMessage.ToString());
stream.Flush();
if (data.ToString() == "1")
ArduinoLandLine.lightsOn();
else if (data.ToString() == "2")
ArduinoLandLine.lightsOff();
else if (data.ToString() == "3")
ArduinoLandLine.blink();
else if (data.ToString() == "4")
ArduinoLandLine.revolve();
else if (data.ToString() == "5")
ArduinoLandLine.revolve2();
else if (data.ToString() == "6")
ArduinoLandLine.revolve3();
else if (data.ToString() == "7")
ArduinoLandLine.revolve4();
else if (data.ToString() == "8")
ArduinoLandLine.revolveFade();
else if (data.ToString() == "9")
ArduinoLandLine.strobe();
else if (data.ToString() == "10")
ArduinoLandLine.random();
else if (data.ToString() == "11")
ArduinoLandLine.pulse();
else if (data.ToString() == "d")
ArduinoLandLine.projectorScreenDown();
else if (data.ToString() == "u")
ArduinoLandLine.projectorScreenUp();
worker.ReportProgress(0, responseMessage);
stream.Flush();
//byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
//stream.Write(msg, 0, msg.Length);
//Console.WriteLine("Sent: {0}", data);
}
client.Close();
}
}
catch (SocketException socketException)
{
Console.WriteLine("Could not connect to server:( SocketException: {0}", socketException);
}
finally
{
server.Stop();
}
我很确定你的函数注册到DoWork事件是错误的。请参阅MSDN https://msdn.microsoft.com/de-de/library/system.componentmodel.backgroundworker(v=vs.110).aspx的示例。
进一步的建议在我对你的问题的评论中。
是电脑的防火墙。允许我的程序访问不工作,但打开我的端口
您假设当读取完成时,您已经获得了完整的消息。TCP不是基于消息的。你可能接收到一个消息的一部分,多个消息或者TCP提供的字节流的任何部分。
例如,data.ToString() == "1"
可能与data == "11"
不匹配。