1

I have three applications:

PPSGPS - this is transmitting gps corrdinates of tcp

PPS - vb6 application that is currently connecting to PPSGPS and has no issues

TestProj - the program I am referring to below. I cannot get it to connect to the PPSGPS

I am writing a C# application that needs to connect to the PPSGPS and get the lat/lon also..

This is the code I wrote as a test:

private void button1_Click(object sender, EventArgs e)
{
          if (client == null)
          {
            client = new TcpClient("localhost", 5106);
          }

          // Get the stream
          NetworkStream stream = client.GetStream();
          Byte[] data = new Byte[256];

          // String to store the response ASCII representation.
          String responseData = String.Empty;

          // Read the first batch of the TcpServer response bytes.
          int bytes = stream.Read(data, 0, data.Length);
          responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
          Console.WriteLine("Received: {0}", responseData);

          // Close everything.
          stream.Close();
          client.Close();    
}

This is all happening on the same machine.

I see the port is open from netstat:

C:\>netstat

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    SSSVR1:4320            localhost:5106         ESTABLISHED

When I run the test program on the laptop, I am getting this:

************** Exception Text **************
System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 127.0.0.1:5106

Anyone see what I'm doing wrong on the test?

==== EDIT ====

I added the client.connect to the code below and still received the same message:

private void button1_Click(object sender, EventArgs e)
{
  IPAddress hostIPAddress1 = IPAddress.Parse("127.0.0.1");
  var port = 5106;

  if (client == null)
  {
    client = new TcpClient(hostIPAddress1.ToString(), port);
  }

  var endp = new IPEndPoint(hostIPAddress1, port);
  client.Connect(endp);
  // Get the stream
  NetworkStream stream = client.GetStream();
  Byte[] data = new Byte[256];

  // String to store the response ASCII representation.
  String responseData = String.Empty;

  // Read the first batch of the TcpServer response bytes.
  int bytes = stream.Read(data, 0, data.Length);
  responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  Console.WriteLine("Received: {0}", responseData);

  // Close everything.
  stream.Close();
  client.Close();
}

==== EDIT #2 ====

This is the code that is in PPSGPS that is 'transmitting' the data that I'm trying to retrieve:

Private Sub SendToPPS(ByVal GPS_Message As String) If mbDebug_PPSGS Then LogToFile("Starting Sub SendToPPS") End If Dim CloseSocket As Boolean

If PPSIsRunning() Then
  CloseSocket = False
  If mbDebug_PPSGS Then
    LogToFile("SendToPPS() PPS is Running.  Sending: " & GPS_Message)
  End If
  ' Don't try to send to PPS if PPS is not running

  Try
    ' Create a TcpClient.
    ' Note, for this client to work you need to have a TcpServer 
    ' connected to the same address as specified by the server, port
    ' combination.
    If SocketClient Is Nothing Then
      ' The socket is not open yet.  Open it now. 
      Dim port As Int32 = 5106
      SocketClient = New TcpClient("localhost", port)
    End If


    ' Translate the passed message into ASCII and store it as a Byte array.
    Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(GPS_Message)

    ' Get a client stream for reading and writing.
    Dim stream As NetworkStream = SocketClient.GetStream()

    ' Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length)

  Catch err As ArgumentNullException
    LogToFile("SendToPPS() ArgumentNullException: " & err.ToString)
    CloseSocket = True
  Catch err As SocketException
    LogToFile("SendToPPS() SocketException: " & err.ToString)
    CloseSocket = True
  End Try
Else
  CloseSocket = True
  LogToFile("SendToPPS() PPS is Not Running.")
  ' PPS is not running
  ' Make sure Socket is closed
End If

If CloseSocket Then
  If Not SocketClient Is Nothing Then
    Try
      SocketClient.Close()
    Catch ex As Exception
      LogToFile("SendToPPS() Closing Socket Exception: " & ex.ToString)
    End Try

    SocketClient = Nothing
  End If

End If

If mbDebug_PPSGS Then
  LogToFile("SendToPPS() Ending Sub SendToPPS")
End If

End Sub

ErocM
  • 4,505
  • 24
  • 94
  • 161
  • Is the VB6 program running while you're running your test? It could be the application you're connecting to only supports a single connection at a time. And is port 5106 definitely the right one? – James Thorpe Mar 01 '13 at 16:31
  • Yes 5106 is the right one. I have the source to the PPSGPS. Is there a setting that allows multiple connections for tcp or can only 1 connection be established at a time? – ErocM Mar 01 '13 at 16:32
  • Not as such - the application needs to be coded for it. Also there's no call to client.Connect() in there - is the client already assigned to something else? – James Thorpe Mar 01 '13 at 16:33
  • Have a look a these SO answers to the same error: http://stackoverflow.com/a/11053525/219344 and http://stackoverflow.com/a/2972662/219344 Do these solutions provide any help? – Jens H Mar 01 '13 at 16:45
  • @JensH I looked at those and I don't see where those have anything to do with my issue. I think it's the code that wrongs and not the computer. – ErocM Mar 01 '13 at 16:48
  • @JamesThorpe I added the client.Connect to the code and got the same message. I'm new to tcp so I'm not sure how this should work. – ErocM Mar 01 '13 at 16:49
  • It seems like the other end of the connection itself just isn't letting you connect. You could try telnet just to see if a basic connection can be made - open a command prompt and enter: telnet localhost 5106 – James Thorpe Mar 01 '13 at 16:52
  • Test this yourself using telnet. – usr Mar 01 '13 at 16:56
  • Telnet did fail. I have updated my post to include the server code. Is there something I need to change in the PPSGPS code to be able to connect different than what I have? – ErocM Mar 01 '13 at 17:17
  • Ah - PPSGPS is the client - it's expecting to connect to and send data to a server when it starts. You'll need to look into the TcpListener class. Effectively when your new app starts, it needs to start listening on port 5106 and then accept the connection from PPSGPS when it starts – James Thorpe Mar 01 '13 at 17:25
  • k that brings up can a computer have two TcpListeners on the same port on the same computer? – ErocM Mar 01 '13 at 18:07
  • ah nvm, I have the code I'll just create another tcpclient and drop the same information into another port – ErocM Mar 01 '13 at 18:12

0 Answers0