Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket and thread in C
    primarykey
    data
    text
    <p>I have the following code:</p> <p>The file is <code>principal.c</code> that it has the function main and others methods.</p> <p>In main():</p> <pre><code>connectAll(&amp;NUM_THREADS); printf("TOTAL clients that to connect %d\n",NUM_THREADS); pthread_t thread[NUM_THREADS]; pthread_attr_t attr; /* Initialize and set thread detached attribute */ pthread_attr_init(&amp;attr); pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE); paramThread params[NUM_THREADS]; for(t=0;t&lt;NUM_THREADS;t++) { printf("What's IP [%s] to connected now? %d\n",regIPConnected[t].ip,regIPConnected[t].idSocket); params[t].idSocket=regIPConnected[t].idSocket; params[t].idThread=t; rc = pthread_create(&amp;(thread[t]), &amp;attr,conectaThread, &amp;(params[t])); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); } }//END FOR /* Free attribute and wait for the other threads */ pthread_attr_destroy(&amp;attr); for(t=0; t&lt;NUM_THREADS; t++) { rc = pthread_join(thread[t], &amp;status); if (rc&lt;0) { printf("ERROR; return code from pthread_join() is %d\n", rc); } printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status); }//END FOR printf("Main: program completed. Exiting.\n"); pthread_exit(NULL); } void *conectaThread (void *arg){ paramThread* params = (paramThread*) &amp;arg; int idSocket=params-&gt;idSocket; long tid=params-&gt;idThread; printf("Thread %ld starting...\n",tid); printf ("ID SOCKET THREAD %d\n",idSocket); while (1) /* Run forever */ { int state=readSocket(idSocket,"status\n"); ... } </code></pre> <p>The structure of paramThread is:</p> <pre><code>typedef struct { int idSocket; long idThread; void (*pf)(int, long); } paramThread; </code></pre> <p>And another file, called socket.c</p> <pre><code>int openSocketInet (char *servIP,unsigned short servPort) { int optval,statusSocket=0; socklen_t optlen = sizeof(optval); /* Create the socket, using TCP */ socketClient = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); printf("OK/NOK socket %d",socketClient); if (socketClient&lt;0) { switch (errno) { /*differents cases of errno*/ default: perror("socket()"); statusSocket= -1; }//END SWITCH }//END IF /* Check the status for the keepalive option */ if(getsockopt(socketClient, SOL_SOCKET, SO_KEEPALIVE, &amp;optval, &amp;optlen) &lt; 0 &amp;&amp; statusSocket==0) { statusSocket=checkGET_SETSockOpt(socketClient,1); printf("Error getsockopt() is %d\n",statusSocket); }//END IF printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF")); int fl = fcntl(socketClient, F_GETFL); fcntl(socketClient, F_SETFL, fl | O_NONBLOCK); /* Set the option active */ optval = 1; //--&gt;TRUE optlen = sizeof(optval); if(setsockopt(socketClient, SOL_SOCKET, SO_KEEPALIVE, &amp;optval, optlen) &lt; 0 &amp;&amp; statusSocket==0) { statusSocket=checkGET_SETSockOpt(socketClient,0); printf("Error setsockopt() is %d\n",statusSocket); }//END IF printf("SO_KEEPALIVE set on socket\n"); /* Check the status again */ if(getsockopt(socketClient, SOL_SOCKET, SO_KEEPALIVE, &amp;optval, &amp;optlen) &lt; 0 &amp;&amp; statusSocket==0) { statusSocket=checkGET_SETSockOpt(socketClient,1); printf("Error getsockopt() is %d\n",statusSocket); }//END IF printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF")); if (connect(socketClient, (struct sockaddr *)&amp;addressServer, sizeof(addressServer)) &lt; 0 &amp;&amp; statusSocket==0) { switch (errno) { /*diffents cases of errno to connect()*/ default: perror("connect()"); statusSocket= -1; }//END SWITCH }//END IF /* Construct the server address structure */ bzero(&amp;addressServer, sizeof(addressServer)); /* Hay que poner todo a ceros */ addressServer.sin_family = AF_INET; /* Internet address family */ addressServer.sin_port = htons(servPort); /* puerto en formato de red */ /* dirección IP en formato de red */ int errInetAton=inet_aton(servIP, &amp;addressServer.sin_addr); if (errInetAton&lt;0 &amp;&amp; statusSocket==0) { printf("Fail the conversion of IP\n",servIP); perror("inet_aton() Motive"); statusSocket= -1; }//END IF printf("The IP %s to connect with sock %d\n",servIP,socketClient); //To return identify a socket or fail if (statusSocket&lt;0) return statusSocket; else return socketClient; } </code></pre> <p>When I running the result is:</p> <pre> socket(): Success --> to sock 3 SO_KEEPALIVE is OFF SO_KEEPALIVE set on socket SO_KEEPALIVE is ON The IP x.x.x.x to connect with sock 3 socket(): Illegal seek --> to sock 4 SO_KEEPALIVE is OFF SO_KEEPALIVE set on socket SO_KEEPALIVE is ON The IP y.y.y.y to connect with sock 4 </pre> <p>I wish I could connect to different ip having the same socket</p> <hr> <p>Thanks, the problem is another:</p> <pre><code>What's IP [x.x.x.x] to connected now? 3 What's IP [y.y.y.y] to connected now? 4 What's IP [z.z.z.z] to connected now? 5 Out for Thread 5 starting... ID SOCKET THREAD 5 Method READ! Send to socket id [5] and data is '[status]' STATUS THREAD 0 Thread 5 starting... ID SOCKET THREAD 5 Method READ! Send to socket id [5] and data is '[status]' Thread 5 starting... ID SOCKET THREAD 5 Method READ! Send to socket id [5] and data is '[status]' STATUS THREAD 0 Info: (-3.35 dB) Method WRITE! Send to socket id [5] and data is '[outlevel 0:50 ]' El conector está marcado como no bloqueante y la operación solicitada lo bloquearía. Method WRITE! Error send() is 0 Level modify of Vol 50% Info: (-24.13 dB) Level modify of Vol 50% Info: (-52.60 dB) Method WRITE! Send to socket id [5] and data is '[outlevel 0:50 ]' El conector está marcado como no bloqueante y la operación solicitada lo bloquearía. Method WRITE! Error send() is 0 Level modify of Vol 50% Method READ! Send to socket id [5] and data is '[status]' hi0! send(): No route to host Method READ! Error send() is -1 STATUS THREAD -1 Method READ! Send to socket id [5] and data is '[status]' # </code></pre> <p>But the socket number is incorrect should be 3 or 4, the next code :</p> <p>In main()</p> <pre><code>... for(t=0;t&lt;NUM_THREADS;t++) { printf("What's IP [%s] to connected now? %d\n",regIPConnected[t].ip,regIPConnected[t].idSocket); </code></pre> <p>idSocket=regIPConnected[t].idSocket;</p> <pre><code> rc = pthread_create(&amp;(thread[t]), &amp;attr,conectaThread,&amp;idSocket); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); } }//END FOR printf("Out for\n\n!"); /* Free attribute and wait for the other threads */ pthread_attr_destroy(&amp;attr); for(t=0; t&lt;NUM_THREADS; t++) { rc = pthread_join(thread[t], &amp;status); if (rc&lt;0) { printf("ERROR; return code from pthread_join() is %d\n", rc); } printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status); }//END FOR printf("Main: program completed. Exiting.\n"); pthread_exit(NULL); </code></pre> <p>}</p> <p>The other code is same. Why not to get the first socket "3" and get the finish socket 5?? and why to finish the threads?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload