Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 59
Registered: ‎05-06-2012
My Device: Blackberry torch 9850
My Carrier: Voila
Accepted Solution

socket connection issue

Hi,

Here is my code to open a client side socket connection to a server running on the vmware host that i am  sure it's working well (the server):

 

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>

 

    int _socket;
    struct sockaddr_in name;

 

bool connect(char *ip, int port)
{
    _socket= socket(AF_INET, SOCK_STREAM, 0);
    if(_socket< 0)
    {
        return false;
    }
    struct hostent *host;
    struct in_addr addr= {0};
    addr.s_addr= inet_addr(ip);
    host= gethostbyaddr((char*)&addr, 4, AF_INET);
    name.sin_family= AF_INET;    name.sin_addr.s_addr= *((unsigned long*)host->h_addr);
    name.sin_port= htons(port);
    if (bind(_socket, (struct sockaddr *)&name, sizeof(name) ))
    {
        return false;
    }
    listen(_socket, 1);
    _socket= accept(_socket, 0, 0);
    return true;
}

 

connect("127.0.0.1", 44444);

 

When invoking the connect() method it's block on the accept() (the server get open before client does). Thus, I need to know if the issue is:

1_  on my client code.

or

2_ vmware can't communicate to the host computer using the local host computer address.

(or even both)

 

Will appreciate any help.

Thanks.

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: socket connection issue

I don't use the simulator often, but my experience with other virtual machines is that 127.0.0.1 refers to the simulated device, not the host device. Try to get an online server going to check if I am correct.

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 59
Registered: ‎05-06-2012
My Device: Blackberry torch 9850
My Carrier: Voila

Re: socket connection issue

Okay, I will look for that by trying to etablish the connection using router address (wifi).

But, it's seem like I have to configure the wifi hardware for simulator first.

I'll keep you posted and thank also for the reply.

Developer
Posts: 59
Registered: ‎05-06-2012
My Device: Blackberry torch 9850
My Carrier: Voila

Re: socket connection issue

I have left a screenshoot of the adapter configuration of vmware that let us see it's sharing the host address (I may missunderstand).

By the way, when trying to connect to a wifi connection on the bb simulator it keep on searching.

Please help.

Thanks

Highlighted
BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: socket connection issue

You will need to use bridged mode in VMWare so that the BlackBerry Simulator VM gets its own IP address, instead of sharing the IP address of the host computer.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 59
Registered: ‎05-06-2012
My Device: Blackberry torch 9850
My Carrier: Voila

Re: socket connection issue

Okay I will look for that.
Thank for reply.
Developer
Posts: 59
Registered: ‎05-06-2012
My Device: Blackberry torch 9850
My Carrier: Voila

Re: socket connection issue

[ Edited ]

After a lot a workaround, I finally realize that the last three

methods attach the specified address and get ready to listen and

accept one connection from an unknown client. I meant it was to

create a server, that why it stop on the accept() method.
So, the code for a client socket side must look like this (almost

similar) :

bool open_connection(char *ip, int port)
{
    _socket= socket(AF_INET, SOCK_STREAM, 0);
    if(_socket< 0)
    {
        return false;
    }
    struct hostent *host;
    struct in_addr addr= {0};
    addr.s_addr= inet_addr(ip);
    host= gethostbyaddr((char*)&addr, 4, AF_INET);
    name.sin_family= AF_INET;
    name.sin_addr.s_addr= *((unsigned long*)host->h_addr_list

[0]);
    name.sin_port= htons(port);
    if (connect(_socket, (struct sockaddr *)&name, sizeof

(name) ) )
    {
        return false;
    }
    return true;
}

Then, to rely the  host and the simulator to a socket connection I

used the same one for loading appliction to it.
One thing : when I try to etablish the connection when my computer

is connected to the internet both, the bind and the connect,

function failed.
@Mshom : When I have tried to set the adapter to brigde the

simulator can't get an "ip" address ("Getting ip.... is written

botton left coner").
By the way think all of you who answer me.