Android Question ServerSocket

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
My idea is to make a small B4A server, that must receive an ascii string from a client, pass it to a Bluetooth device, get an answer, and send back it to the client.
Besides the Bluetooth part on which I have no problems, I am now facing difficulties with ServerSocket.
First of all, I have Windows Client-Server C++ programs that work, so I am simply trying to partially reproduce that situation, keeping a Windows executable as the client and making a B4A App as the server. First step for me was to test the communication. So I started with the very simple following app:

Sub Process_Globals
Dim ServerS As ServerSocket
End Sub
Sub Globals
Dim MyIp As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
ServerS.Initialize(10250,"SockEvent") ' port is the same used by the client
MyIp=ServerS.GetMyIP ' this Ip is also hard coded in the client
ToastMessageShow(MyIp,True)
End If
End Sub
Sub Activity_Resume
ServerS.Listen
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub SockEvent_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerS.Listen 'Continue listening to new incoming connections
End Sub

Port and Ip are rigorously the same than those used by the Client-Server Windows programs.
I put the App in a Tablet and close the B4Bridge connection (I don't know whether this has effects or not).
Then I run the App on the Tablet and, after, try to connect with the same Windows client that works with the Windows server, without success.
The Windows client code snippet is:

WSADATA wsaData;
int wsaret=WSAStartup(0x101,&wsaData);

conn=socket(AF_INET,SOCK_STREAM,0);
addr=inet_addr(m_sServerIPAddress.c_str()); // the server IP address
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);

In this code "hp" is Always NULL, so the client doesn't connect. Of course, consequently, no event is raised on Android side.
Sorry if this post is not clear. Hope somebody understands, anyway. There is the possibility that I am missing something very "trivial"...
Thanks for your attention
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks for suggestions. No, the NewConnection event is not raised. As a matter of fact, to detect this event was my first aim, and tried it either with the debugger or with ToastMessage in release mode. (I put a ToastMessageShow inside the NewConnection event, not like in the code I attached, in which the Message is shown only on successful connection)
If I see well, the link gives a B4A+B4J example, that is not my case. I already tested that the combination B4A+B4J works, this is not the concern. The problem is Microsoft C/C++ and B4A. As a matter of fact I have an existing rather big client in C++/MFC. In all examples I saw here, it seems that the server is always done with B4J. But my problem is that the server has to run on a tablet or smartphone. If I understood correctly, with B4J I cannot make a program for the mobile device. Due to the above reasons, I thought to B4A. If with B4J I can make a service on the mobile device, this could be an alternative.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
It seems that the Listen process doesn't start.. I attach again the code.
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Dim ServerS As ServerSocket
End Sub
Sub Globals
Dim MyIp As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
ServerS.Initialize(10250,"SockEvent")
MyIp=ServerS.GetMyIP
ToastMessageShow(MyIp,True)
End If
End Sub
Sub Activity_Resume
ServerS.Listen
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub SockEvent_NewConnection (Successful As Boolean, NewSocket As Socket)
ToastMessageShow("NewConnectionRaised", False)
If Successful Then
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerS.Listen 'Continue listening to new incoming connections
End Sub
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
As I see the
B4X:
tags don't preserve indentation, or am I missing something else? I copy/pasted the code from B4A editor.
 
Upvote 0
Top