Jump to content

Problem sending data to TCP port


Strahan

Recommended Posts

Hi.  I am writing a web app to control Zoom Player on my HTPC.  I found that ZP supports listening on a TCP port to accept control commands.  The docs say to quit ZP, send 5100,fnExit + ASCII 13/10 to port 4769.  My original client was C# and I implemented it there, works great.  I then realized WTF am I doing using an external .EXE to just send data to a TCP port?  Switched to using PHP's fsockopen.  Alas, that causes an error to pop up on ZP (List index out of bounds or some such).  Perplexed,  I wrote a GUI for the HTPC to just display an ASCII val breakdown of what it receives to compare:

 

phpport.jpg

 

So it looks like they both send the data the same.  :confused:  Any idea why the discrepancy?

 

Here is the code:

 

C# that works:

    string cmdstr = "";
    for (int x=2; x<=args.Length-1; x++) cmdstr += args[x] + " ";
    if (SendCommand(args[0], System.Convert.ToInt16(args[1]), cmdstr.Trim() + Environment.NewLine)) Console.WriteLine("Command string sent.");
    Environment.Exit(0);

    static bool SendCommand(string dest, int port, string cmd) {
      try {
        IPAddress[] ips = Dns.GetHostAddresses(dest);
        TcpClient client = new TcpClient();
        IPEndPoint serverEndPoint = new IPEndPoint(ips[0], port);
        client.Connect(serverEndPoint);
        NetworkStream clientStream = client.GetStream();
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = encoder.GetBytes(cmd);
        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush(); clientStream.Close(); clientStream.Dispose();
        client.Close();
        return true;
      } catch (Exception ex) {
        Console.WriteLine("Failed: " + ex.Message);
        return false;
      }
    }

 

The PHP that is being difficult:

  send_data("lioth", "4769", "5100,fnExit");

  function send_data($host,$port,$data='') { 
    $fp = fsockopen($host,$port);
    if($fp) { 
      fputs($fp, "$data\r\n"); 
      fclose($fp); 
    } 
    return; 
  } 

 

:wtf:

Link to comment
Share on other sites

Hmm, that's really interesting... Do you have wireshark running? It may be worthwhile to check the TCP communication to see how the packets are being split up.  Does the ZP return any data after a command is received?  Try a telnet connection and see what happens.  Sorry that's the only advice I have right now.. When you do find a solution, please post as I'd like to see!  :D

Link to comment
Share on other sites

OK, just ran it through Wireshark.  Annoyingly, the data part was identical.  There were some variances in the frame data, but I don't know if that's relevant (different boxes, different hardware, etc etc).  Then again, I'm no expert heh.  I won't put the screen shot inline since it's kinda big (1680x1050) but this is the link to it.

 

Telnet works fine:

0000 Zoom Player Home Professional
0001 7.00
1300 1
1000 0
5100,fnExit

Connection to host lost.

 

*sigh*  I have a post in to ZP as well, but at this point I can't figure out where the problem would lie; ZP or a TCP sending issue.  If the data are the same, it doesn't make sense for it to be TCP but then if ZP works fine with telnet and C#, doesn't make sense why it'd fail with just PHP.  Oh yea, I also tried VB.NET just to try a different flavor of TCP communicator..

 

    Sub Main()
      Dim tcpClient As New System.Net.Sockets.TcpClient()
      On Error Resume Next
      tcpClient.Connect("lioth", 4769)
      If Err.Number <> 0 Then
        Console.WriteLine("Error connecting") : End
      Else
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        If networkStream.CanWrite Then
          SendCommand("5100,fnExit" + vbCrLf, tcpClient, networkStream, True)
        Else
          Console.WriteLine("Error connecting")
        End If
      End If
    End
  End Sub

  Function SendCommand(ByVal strCommand, ByVal TcpClient, ByVal NetworkStream, Optional ByVal SkipResponse = False)
    Dim sendBytes As [byte]() = Encoding.ASCII.GetBytes(strCommand)
    NetworkStream.Write(sendBytes, 0, sendBytes.Length)
    If Not SkipResponse Then
      Dim bytes(TcpClient.ReceiveBufferSize) As Byte
      NetworkStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))
      Dim returndata As String = Encoding.ASCII.GetString(bytes)
      If InStr(returndata, Chr(0)) > 0 Then returndata = returndata.Substring(0, InStr(returndata, Chr(0)) - 1)
      SendCommand = returndata
    End If
  End Function

 

Works great.  *siiiiigh* hehe

Link to comment
Share on other sites

Well, I didn't figure out what was wrong but I did find a workaround.  I never could get PHP to send the data and have it work with fsockopen, but I fired up C# and wrote a class library that sends the data then registered it and I just call the COM component from the webpage.  Took a little wrestling to learn how to get PHP and COM to work together (permissions problems mostly) but now it works smoothly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.