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