PHP Code:
[CODE]/* threadclient.c */
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
void *cthr_do();
void *cthr_send();
void *cthr_recv();
int main()
{
int k;
pthread_t new_thr;
int check = pthread_create(&new_thr, NULL, cthr_do, NULL );
if(check!=0)
printf("The thread cannot be created!");
pthread_join (new_thr, NULL);
printf("The whole process terminated\n");
return 0;
}
void *cthr_do()
{
printf("thread run sucessful\n");
int sock, bytes_received, connected, s, r;
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in6 server_addr;
host = gethostbyname("::1");
if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin6_family = AF_INET6;
server_addr.sin6_port = htons(5000);
server_addr.sin6_addr= in6addr_any/*((struct in_addr *)host->h_addr)*/;
/*bzero(&(server_addr.sin_zero),8);*/
/*memset(&(server_addr.sin_zero),0,8);*/
connected = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (connected== -1)
{
perror("Connect");
exit(1);
}
printf("The connection is established.\n");
pthread_t send_thr, recv_thr;
int chk_send = pthread_create(&send_thr, NULL, cthr_send, (void *)sock);
if(chk_send!=0)
printf("The send message thread cannot be created!");
int chk_recv = pthread_create(&recv_thr, NULL, cthr_recv, (void *)sock);
if(chk_recv!=0)
printf("The receive message thread cannot be created!");
while (1)
{
s = pthread_join (send_thr, NULL);
r = pthread_join (recv_thr, NULL);
if (s && r)
break;
}
close(sock);
printf("closed socket\n");
}
void *cthr_send(void *arg)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
char send_data [1024], recv_data[1024];
while (1)
{
printf("\n SEND (q or Q to quit) : ");
gets(send_data)/*,1024,stdin)*/;
if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
{
send((int) arg, send_data,strlen(send_data), 0);
/*close ((int) arg);*/
break;
}
else
send((int) arg, send_data,strlen(send_data), 0);
}
if ( pthread_cancel(recv_thr) == -1 )
{
perror("pthread_cancel failed");
exit(3);
}
// if (pthread_cancel(recv_thr)!= 0)
// perror ("Thread cancelation "); ;
}
void *cthr_recv(void *arg)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
char send_data [1024] , recv_data[1024];
int bytes_received;
while (1)
{
bytes_received = recvfrom((int) arg, recv_data, 1024-1, 0, NULL, NULL);
/*recv((int) arg,recv_data,1024,0);*/
if (bytes_received == -1)
{
/*
* Socket in error state
*/
perror ("Client recv(): ");
exit(1);
}
printf("\nThe number of digit received: %d\n", bytes_received);
recv_data[bytes_received] = '\0';
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data , "Q") == 0)
{
send((int) arg, "q", strlen("q"), 0);
// close((int) arg);
break;
}
else
printf("\n RECEIVED DATA = %s \n" , recv_data);
fflush(stdout);
}
// if (pthread_cancel (send_thr)!= 0)
// perror ("Thread cancelation "); ;
}[/CODE]
When i compile this code with the error shown.