Connection between PC and PDA through a GPRS connection. tcpListener. Problem: AcceptTcpClient()

I

Izaskun

Hello,
I'm trying to establish a connection between my PC an my PDA.

The PDA has a GPRS connection open and read data from the 14001 port.
The PC try to connect with the PDA but the PDA seems not to hear
anything.
The PDA keeps listening in the following sentence:
client = tcpListener.AcceptTcpClient() 'Always keep here waiting


Here you have my source code. Ay help will be appreciated.




Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports OpenNETCF.Net.Bluetooth
Imports OpenNETCF.Threading


Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
Dim workerThread As System.Threading.Thread
workerThread = New Threading.Thread(AddressOf receiveLoop)
workerThread.Start()
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub

Public Sub receiveLoop()
Dim strReceived As String

tcpListener = New System.Net.Sockets.TcpListener(14001)
tcpListener.Start()

strReceived = receiveMessage(MAX_MESSAGE_SIZE)
While True
If strReceived <> "" Then
Dim updateDelegate As New myDelegate(AddressOf
UpdateTextBox)
updateDelegate.Invoke(strReceived)
strReceived = receiveMessage(MAX_MESSAGE_SIZE)
End If
End While


End Sub

Private Function receiveMessage(ByVal BufferLen As Integer) As
String
Dim bytesRead As Integer = 0
Dim client As TcpClient = Nothing
Dim stream As System.IO.Stream = Nothing
Dim Buffer(MAX_MESSAGE_SIZE) As Byte
Dim str As String
Try
client = tcpListener.AcceptTcpClient() 'Always keep here
waiting
stream = client.GetStream()
bytesRead = stream.Read(Buffer, 0, BufferLen)
str = "->" +
System.Text.Encoding.Unicode.GetString(Buffer, 0, bytesRead)

Catch e As Exception
'dont display error if we are ending the listener
If True Then
MsgBox("Error listening to incoming message")
End If

Finally
If (Not stream Is Nothing) Then
stream.Close()
End If
If (Not client Is Nothing) Then
client.Close()
End If

End Try
Return str
End Function

Private Delegate Sub myDelegate(ByVal str As String)
Private Sub UpdateTextBox(ByVal str As String)
'---delegate to update the textbox control
Dim sMessagesArchive As String
sMessagesArchive += str
MsgBox(sMessagesArchive)
End Sub
End Class
 
P

Paul G. Tobey [eMVP]

How is the PC making the connection? What's the IP address of the PDA? How
is the PC getting that information?

Paul T.
 
I

Izaskun

Paul G. Tobey said:
How is the PC making the connection? What's the IP address of the PDA? How
is the PC getting that information?

Paul T.

The IP address is asigned dinamically by the PLMN (Public Land Mobile
Network), so everytime I establish a new GPRS connection I get a new
IP (ex.: 213.4.166.82). I write this data literally in my source code.


The PC source code is written in Java using sockets and streams:

public void connect()
{
try {
so= new Socket (host,port);
input = new DataInputStream(so.getInputStream());
output = new DataOutputStream(so.getOutputStream());
if (so != null && input != null && output != null) {
output.writeBytes("TEST,....\n");
String response="";
respuesta = input.readLine();
System.out.println("Server: " + response);
output.close();
input.close();
so.close();
}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}

But this code seems not to be able to reach the PDA, because it
returns a ConnectException: Connection refused: connect.

Thanks.
 
I

Izaskun

Paul G. Tobey said:
How is the PC making the connection? What's the IP address of the PDA? How
is the PC getting that information?

1.- The connection request make in the PC is written in Java:

public class conexion
{
public Socket so=null;
public String host="213.4.169.30";
public DataInputStream input=null;
public DataOutputStream output=null;
public int port=14001;
public conexion()
{

}
public static void main(String [] args)
{
conexion con= new conexion();
con.conectar();

}
public void conectar()
{
try {
so= new Socket (host,port);
input = new DataInputStream(so.getInputStream());
output = new DataOutputStream(so.getOutputStream());
if (so != null && input != null && output != null) {
output.writeBytes("TEST,....\n");
String response="";
response = input.readLine();
System.out.println("Server: " + response);
output.close();
input.close();
so.close();

}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

And when it try to connect with the IP address return an exception:
java.net.ConnectException: Connection refused: connect at
java.net.PlainSocketImpl.socketConnect(Native Method)

2.- The IP address of the PDA is assigned dinamically by the mobile
company.

3.- I'm still in test phase, so I write the IP address directly in the
Java source code.

Thanks
 
P

Paul G. Tobey [eMVP]

"Connection refused" is telling you something very important: there's no
socket waiting for connection on the port that you specify. I don't see
anything necessarily wrong with your Java code (it's been a long time since
I did that sort of thing in Java, though). You really need to sniff the
packets and just make sure that what you think is happening is really
happening. If the exception is correct, you should see a packet from the
Java end with the SYN bit set. The response packet from the PDA should have
the RST bit set. This may be repeated several times before the Java code
takes the hint and declares that the connection was refused.

Can you ping the PDA from the PC where the Java code will run? At a guess,
I'd say that there's either not real network connectivity between your PC
and the PDA, maybe because there's a proxy that is actually connecting stuff
outside the GPRS network to stuff inside it or something.

I don't see anything wrong with the .NET CF code. It works fine on an
Ethernet network for me, although you haven't done a good job of making
stopping it work very well.

Paul T.

Izaskun said:
Paul G. Tobey said:
How is the PC making the connection? What's the IP address of the PDA?
How
is the PC getting that information?

1.- The connection request make in the PC is written in Java:

public class conexion
{
public Socket so=null;
public String host="213.4.169.30";
public DataInputStream input=null;
public DataOutputStream output=null;
public int port=14001;
public conexion()
{

}
public static void main(String [] args)
{
conexion con= new conexion();
con.conectar();

}
public void conectar()
{
try {
so= new Socket (host,port);
input = new DataInputStream(so.getInputStream());
output = new DataOutputStream(so.getOutputStream());
if (so != null && input != null && output != null) {
output.writeBytes("TEST,....\n");
String response="";
response = input.readLine();
System.out.println("Server: " + response);
output.close();
input.close();
so.close();

}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

And when it try to connect with the IP address return an exception:
java.net.ConnectException: Connection refused: connect at
java.net.PlainSocketImpl.socketConnect(Native Method)

2.- The IP address of the PDA is assigned dinamically by the mobile
company.

3.- I'm still in test phase, so I write the IP address directly in the
Java source code.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top