C language +eXosip2 + pthread

Hi all

I am programming simple eXosip2 server for processing SIP text messages. I need to listen on this server on two ports concurrently, but i am failing in this.

I can see from outputs that ports are open, also using netstat -lun list theese ports as listening. But if some message is received, server is processing this messsage only on one port. Could you plese help me with this, I am new in multithreading.
Here isstructure of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void *sip_listn(void *arg);

int main()
{
        //initializing exosip
	int i=0;
        TRACE_INITIALIZE (6, stdout);
        i=eXosip_init();
        if (i!=0) { fprintf (stderr, "eXosip_init FAILED!!!\n"); return -1; }

	int  port1=5060;
	int  port2=5070;
	int  port3=5080;
	
	pthread_t thread1;	// this is our thread identifier
     	pthread_t thread2;	// this is our thread identifier

     	pthread_create(&thread1,NULL,&sip_listn,(void *) &port1);
	pthread_create(&thread2,NULL,&sip_listn,(void *) &port2);
     
     	pthread_join(thread1,NULL);
     	pthread_join(thread2,NULL);

        return 0;
}

void *sip_listn(void *arg)
{
	int i;
	int k;
 	int *port;
	port = (int*) arg;
        eXosip_event_t *event;

fprintf (stderr, "Opening port... p:%d\n",*port);
i = eXosip_listen_addr (IPPROTO_UDP, NULL, *port, AF_INET, 0);
if (i!=0) 
{ eXosip_quit(); fprintf (stderr, "Could not initialize transport layer p:%d -> error: %d\n",*port,i); return NULL; }  
else { fprintf (stderr, "Port is open p: %d\n",*port); } 

	fprintf (stderr, "\n SIP server -> Listening on port: %d\n",*port);

        for (;;)
        {
                event = eXosip_event_wait (0, 50);
                eXosip_lock();
                eXosip_automatic_action ();
                eXosip_unlock();

        if (event == NULL)
        {
                ;
                printf ("\n Waiting for message: %d\n",*port);
        } else {
                //some code
        }


As you can see both threads are created, ports are open and everything seems to be ready for message processing, but...


Opening port... p:5060
Opening port... p:5070
Port is open p: 5060

 SIP server -> Listening on port: 5060
Port is open p: 5070

 SIP server -> Listening on port: 5070

 Waiting for message: 5070

 Waiting for message: 5060

 Waiting for message: 5070

 Waiting for message: 5060

 Waiting for message: 5060




$ netstat -lun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
                      
udp        0      0 0.0.0.0:5060            0.0.0.0:*                          
                       
udp        0      0 0.0.0.0:5070            0.0.0.0:*                          
                                     


When I try to send message, only one port is responding and I cant figure out why. I am 99% sure that there is problem with threads but I am not very skilled in this, so if you could help me, or tell me what am I doing wrong....

As you can see, sip port (5060) is responding to ma other app port (10060), but when i send SIP message to 5070 port there is no response....



$ sudo tcpdump -i lo

12:41:22.129940 IP localhost.10060 > localhost.sip: SIP, length: 355
12:41:22.132653 IP localhost.sip > localhost.10060: SIP, length: 290
12:41:39.052895 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:39.554075 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:40.565402 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:42.577414 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:46.589357 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:50.602628 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:54.615451 IP localhost.10060 > localhost.5070: UDP, length 355
12:41:58.626382 IP localhost.10060 > localhost.5070: UDP, length 355

No, I don't think that this has to do with thread.

On line 36 eXosip_listen_addr() returns a value stored in i. I guess it's the identifier for the port listening.
On line 45 I would have expected that the identifier i need to be passed to eXosip_event_wait(). Otherwise how does eXosip_event_wait() know what port to wait for?
Last edited on
I dont think it is a problem, eXosip_event_wait() has two inputs but any of them is port number or port identifier.


eXosip_event_t* eXosip_event_wait (	int tv_s, int tv_ms )		

Wait for an eXosip event.
Parameters:
tv_s	timeout value (seconds). 
tv_ms	timeout value (mseconds).
Well, it is indeed possible that the threads are the problem. That is when the library isn't thread safe.

Taking a look at this documentation

http://www.antisip.com/doc/exosip2/group__eXosip2__event.html

shows that you don't need multiple threads. When such an event occures you can find out with eXosip_event_geteventsocket() what the source of the event is (if you even care).

So put all the library stuff in one thread. 'Open' both ports with eXosip_listen_addr() one after another and you're supposed to be happy
I cannot open multiple ports...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	int port1=5070;
	int port2=5080;

	int i=0;
           TRACE_INITIALIZE (6, stdout);
           i=eXosip_init();
           if (i!=0) { fprintf (stderr, "eXosip_init FAILED!!!\n"); return -1; }

           fprintf (stderr, "Opening port... p:%d\n",port1);
           i = eXosip_listen_addr (IPPROTO_UDP, NULL, port1, AF_INET, 0);
           if (i!=0) 
           { eXosip_quit(); fprintf (stderr, "Could not initialize transport layer p:%d -> error: %d\n",port1,i); return NULL; }  
           else { fprintf (stderr, "Port is open p: %d\n",port1); } 

          fprintf (stderr, "Opening port... p:%d\n",port2);
          i = eXosip_listen_addr (IPPROTO_UDP, NULL, port2, AF_INET, 0);
          if (i!=0) 
          { eXosip_quit(); fprintf (stderr, "Could not initialize transport layer p:%d -> error: %d\n",port2,i); return NULL; }  
          else { fprintf (stderr, "Port is open p: %d\n",port2); } 



Opening port... p:5070
Port is open p: 5070
Opening port... p:5080
Could not initialize transport layer p:5080 -> error: -3
So the return code is an error. What's the meaning of -3?

Two ports bound at one address. I doubt that sockets are able to handle that. Why do you need two ports?

What about this eXosip_set_socket()
I dont know waht is the return code, it doesnt have to be an error. I am using eXosip2 examples to open port. And if value in "i" is not equal to "0" it means that some error occured.
Value -3 is in variable "i" but i dont know what that value means...

I have no idea what to check or try next....
Topic archived. No new replies allowed.