我的TCP服务问题

时间:2015-08-14 14:22:05

标签: c# tcp

我有一个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代码。这是TCPListener代码:

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();
  }

3 个答案:

答案 0 :(得分:0)

我很确定您的功能注册到DoWork事件是错误的。请参阅MSDN https://msdn.microsoft.com/de-de/library/system.componentmodel.backgroundworker(v=vs.110).aspx上的示例。

在我对你的问题的评论中提出了进一步的建议。

答案 1 :(得分:0)

这是计算机防火墙。允许我的程序访问不起作用,但打开我的端口

答案 2 :(得分:0)

您假设读取完成后您已收到完整的消息。 TCP不是基于消息的。您可能会收到一条消息,多条消息或TCP提供的字节流的任何子部分。

例如,如果data.ToString() == "1",则data == "11"可能不匹配。两个消息一次。

相关问题