PC Review


Reply
Thread Tools Rate Thread

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

 
 
Izaskun
Guest
Posts: n/a
 
      4th Apr 2005
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
 
Reply With Quote
 
 
 
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      4th Apr 2005
How is the PC making the connection? What's the IP address of the PDA? How
is the PC getting that information?

Paul T.

"Izaskun" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Izaskun
Guest
Posts: n/a
 
      5th Apr 2005
"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com> wrote in message news:<(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Izaskun
Guest
Posts: n/a
 
      5th Apr 2005
"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      5th Apr 2005
"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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com>
> wrote in message news:<(E-Mail Removed)>...
>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WAVECOM GPRS MODEM Q2406A, CONNECTION PROBLEM tony Microsoft Dot NET Compact Framework 0 26th May 2006 11:17 AM
Getting the connected IP from TcpListener.AcceptTcpClient Benny Raymond Microsoft C# .NET 1 19th Oct 2005 09:11 PM
how to check wheather active sync connection or any connection is there(gprs,wlan) raghu Microsoft Dot NET Compact Framework 1 23rd Sep 2005 05:59 PM
PC Connection, GPRS Connection - bit random? =?Utf-8?B?Um9iIFM=?= Microsoft Dot NET Compact Framework 2 29th Apr 2005 03:06 PM
Web service connection using GPRS - problem Pankaj S. Microsoft Dot NET Compact Framework 6 1st Nov 2004 03:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:11 PM.