Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Sync Problems or Pointer Problems ? HELP: (5 Items)
   
Sync Problems or Pointer Problems ? HELP  
im receiving data from a server with a socket udp, then i created a thread running paralel with the graphic interface 
who uses the socket to receive the data, but when i execute i have memory fault: core dump message.. i dunno if its a 
sync problem or problem with pointers...  my code inside the abmain here

/*********************************************************************************************/	

	if (pthread_create(&child, NULL, &ChildFn1, 0) !=0){
	perror("Pthreads Error");
        pthread_join(child, NULL);
	}
/*********************************************************************************************/
	PtMainLoop( );
	PtExit( 0 );

	return 0;
	} //end of main 

/*********************************************************************************************/	
	void *ChildFn1(void *arg){
	int Socket_con_Servidor;
	
        Socket_con_Servidor = Abre_Conexion_Udp();
	
	if(Socket_con_Servidor == -1){
	printf("Cant establish connection with server\n");
        exit(-1);
	}

	while(1){
	Recibir_Mensaje(Socket_con_Servidor);
        sleep(1);
         }

        return NULL;
} 

/*****************************************************
//and my socket code here

int Abre_Conexion_Udp(){
struct sockaddr_in Direccion;
int sd = sizeof(Direccion);

	sd = socket (AF_INET, SOCK_DGRAM, 0);
	if (sd == -1){
	return (-1);
	}
	
	bzero(&Direccion, sizeof(Direccion)); 
	Direccion.sin_family = AF_INET;
	Direccion.sin_port = htons(8880); //listening port
	if (inet_aton("192.168.30.255", &Direccion.sin_addr) == 0)  
	perror("Wrong IP address");
	
	if (bind (sd, (struct sockaddr *)&Direccion, sizeof(Direccion)) == -1){
		printf("Bind problem\n");
		close(sd);
		return -1;
	}
	return (sd);
}

/******************************************************************************/
/* Receiving data from server*/

void Recibir_Mensaje(int sd){

struct sockaddr_in Servidor;
int bytes, LargoServidor = sizeof(Servidor);
char buffer[N];
	
	memset(buffer, 0, N);
	
	bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&Servidor, &LargoServidor);
	//printf("\nBuffer: %s", buffer);
	PtSetResource(Multi, Pt_ARG_TEXT_STRING, buffer, 0);
	memset(buffer, 0, N);		
}

as you can see im trying to put the buffer received from my udp socket into a multitext
Re: Sync Problems or Pointer Problems ? HELP  
Jorge,

http://www.qnx.com/developers/docs/6.3.2/photon/prog_guide/ipc.html#ReceivingQNXMessages

The above link  starts with :
-----
Interprocess Communication
A Photon application can't always work in isolation -- sometimes it needs to communicate with other processes. 
...
...
------

Hope this helps.
-Jeevan



Re: Sync Problems or Pointer Problems ? HELP  
thanks man.. ill read about this comunnication interprocess.. i hope get the solve of my problem
Re: Sync Problems or Pointer Problems ? HELP  
> thanks man.. ill read about this comunnication interprocess.. i hope get the 
> solve of my problem


Welcome Jorge.

See I would change the docs from:

Interprocess Communication
A Photon application can't always work in isolation -- sometimes it needs to communicate with other processes

To:

Interprocess Communication
A Photon application can't always work in isolation -- sometimes it needs to communicate with other processes/threads

J.
AW: Sync Problems or Pointer Problems ? HELP  
Hi Jorge,

I see two potential issues right now. First, we're not quite sure that
you always receive a terminating ascii NUL character at the end of each
message. If it's missing (e.g., because the message was too large to fit
into the buffer), the string you are trying to set might effectively
extend _very_ far.

Second, more important: You are doing multi-threading in a Photon
application here. Be aware that Photon was originally designed with a
single-threaded approach in mind, so actually accessing the GUI from
more than one thread (here: main loop thread and udp comm thread) is
potentially fatal. You can use the Photon Library's Locking mechanism,
provided via API functions PtEnter() and PtLeave(). Refer also to the
online help, topic
   Photon micro GUI / Programmer's Guide / Parallel Operations / Threads

Or, you might decide to go single-threaded. You could use the function
"PtAppAddFd()" see docs) to install a callback to be called whenever
some data becomes ready for read on your UDP socket.

Hope this helps,
- Thomas

> -----Ursprungliche Nachricht-----
> Von: Jorge Inostroza [mailto:community-noreply@qnx.com]
> Gesendet: 15 September 2008 18:07
> An: momentics-community
> Betreff: Sync Problems or Pointer Problems ? HELP
> 
> 
> im receiving data from a server with a socket udp, then i 
> created a thread running paralel with the graphic interface 
> who uses the socket to receive the data, but when i execute i 
> have memory fault: core dump message.. i dunno if its a sync 
> problem or problem with pointers...  my code inside the abmain here
> 
> /*************************************************************
********************************/	
> 
> 	if (pthread_create(&child, NULL, &ChildFn1, 0) !=0){
> 	perror("Pthreads Error");
>         pthread_join(child, NULL);
> 	}
> /*************************************************************
********************************/
> 	PtMainLoop( );
> 	PtExit( 0 );
> 
> 	return 0;
> 	} //end of main 
> 
> /*************************************************************
********************************/	
> 	void *ChildFn1(void *arg){
> 	int Socket_con_Servidor;
> 	
>         Socket_con_Servidor = Abre_Conexion_Udp();
> 	
> 	if(Socket_con_Servidor == -1){
> 	printf("Cant establish connection with server\n");
>         exit(-1);
> 	}
> 
> 	while(1){
> 	Recibir_Mensaje(Socket_con_Servidor);
>         sleep(1);
>          }
> 
>         return NULL;
> } 
> 
> /*****************************************************
> //and my socket code here
> 
> int Abre_Conexion_Udp(){
> struct sockaddr_in Direccion;
> int sd = sizeof(Direccion);
> 
> 	sd = socket (AF_INET, SOCK_DGRAM, 0);
> 	if (sd == -1){
> 	return (-1);
> 	}
> 	
> 	bzero(&Direccion, sizeof(Direccion)); 
> 	Direccion.sin_family = AF_INET;
> 	Direccion.sin_port = htons(8880); //listening port
> 	if (inet_aton("192.168.30.255", &Direccion.sin_addr) == 0)  
> 	perror("Wrong IP address");
> 	
> 	if (bind (sd, (struct sockaddr *)&Direccion, 
> sizeof(Direccion)) == -1){
> 		printf("Bind problem\n");
> 		close(sd);
> 		return -1;
> 	}
> 	return (sd);
> }
> 
> /*************************************************************
> *****************/
> /* Receiving data from server*/
> 
> void Recibir_Mensaje(int sd){
> 
> struct sockaddr_in Servidor;
> int bytes, LargoServidor = sizeof(Servidor);
> char buffer[N];
> 	
> 	memset(buffer, 0, N);
> 	
> 	bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct 
> sockaddr *)&Servidor, &LargoServidor);
> 	//printf("\nBuffer: %s", buffer);
> 	PtSetResource(Multi, Pt_ARG_TEXT_STRING, buffer, 0);
> 	memset(buffer, 0, N);		
>...