My program has two threads one for reading the data of the client (RXTHREAD) and putting that data of client in a structure called qstr(which contains client Ip address,message,etc), the object of this queue(put_inq) should be placed in a Structure called (que), the SXTHREAD should then pop the object put_inq and use the IP address to send back the data to the client?
Iam getting an error saying like this
:error: In function ‘void* RXTHREAD(void*)’:
: error: cannot convert ‘qstr’ to ‘char’ in assignment
The aim of the program is to put the data of the client in a stucture and pass the structure object to queue and pop the object of queue in SXTHREAD and reply back to the same client .
Regards
suraj
#include<iostream>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<unistd.h>
#define MAXLENGTH 100
#define MAXSIZE 10
using namespace std;
struct sockaddr_in server;
struct sockaddr_in client;
pthread_t tid;
char buf[MAXLENGTH];
int size, rcx,rcs;
socklen_t fromlen = 0;
int socketid;
int bufsize;
struct qstr
{
char msgbuffer[100];
int NoBytesRcv;
char *cliadd;
}put_inq;
struct que
{
int front,rear;
char queue[MAXSIZE];
}q;
int full(void)
{
if (q.rear == MAXSIZE)
return(1);
else
return(0);
}
int empty(void)
{
if (q.front == q.rear + 1)
return(1);
else
return(0);
}
void *RXTHREAD(void *)
{
while(1)
{
//size is no.of bytes received from the client program.
size = recvfrom(socketid,buf,sizeof(buf),0,(struct sockaddr *)&client,(socklen_t *) &fromlen);
if(size < 0)
cout<< " \n Error In Receiving "<<endl;
else
cout<<" \n Received Size of Data From Client Is : "<<size<<endl;
cout<<" The Clients IP Address Is :"<<inet_ntoa(client.sin_addr)<<endl;
put_inq.cliadd = inet_ntoa(client.sin_addr);
cout<<" The Clients IP Address Is :"<<put_inq.cliadd<<endl;
put_inq.NoBytesRcv = size;
cout<<" No.of Bytes Received Are : "<<put_inq.NoBytesRcv;
memcpy( put_inq.msgbuffer,&buf,size );
cout<<" \n The Message Is "<<put_inq.msgbuffer<<endl;
// Storting The Structure Object In que STRUCTURE
if(full() == 1)
{
cout<<"\n\nQueue Full\n";
break;
}
else
{
q.rear = q.rear + 1;
//cout<<"\nEnter An Element to Be Added ";
//cin>>x;
q.queue[q.rear] = put_inq;
if(q.rear == 1)
q.front ++;
}
}
}
void *SXTHREAD(void *)
{
while(1)
{
sleep(6);
if(empty() == 1)
{
cout<<"\n\nQueue Empty\n";
break;
}
else
{
cout<<q.queue[q.front]<<endl;//" Has Been Deleted!";
//s.front = s.front +1;
}
if(put_inq.NoBytesRcv > 0)
{
cout<<"\n Replying Client"<<endl;
cout<<" \n Clients IP Address IS :: "<<put_inq.cliadd<<endl;
cout<<" \n Enter A String "<<endl;
gets(buf);
cout<<"\n Sending............"<<endl;
sendto(socketid,buf,sizeof(buf),0,(struct sockaddr*)&client,fromlen);
cout<<" \n Message Sent "<<endl;
cout<<" \n Server Online "<<endl;
size=0;
}
else
cout<<" \n Nothing Received From Client "<<endl;
}
}
int main(void)
{
socketid = socket(AF_INET, SOCK_DGRAM , 0);
if(socketid < 0)
{
cout<<" \n Error Creating A Socket"<<endl;
}
server.sin_family=AF_INET;
server.sin_port=htons(9999);
server.sin_addr.s_addr=htonl(INADDR_ANY);
memset(&(server.sin_zero), '\0', 8);
cout<<" \n ***************** BINDING SOCKET ***************** "<<endl;
if( bind(socketid, (struct sockaddr *)&server, sizeof(server)) == -1)
{
cout<<" \n Error Binding The Socket"<<endl;
return(0);
}
cout<<"\n Socket Bound"<<endl;
fromlen=sizeof(struct sockaddr);
//Creating A Thread
cout<<" \n Starting Thread "<<endl;
rcx = pthread_create(&tid, NULL, RXTHREAD ,NULL);
rcs = pthread_create(&tid, NULL, SXTHREAD ,NULL);
if (rcx!=0 && rcs!=0)
{
cout<<" \n ERROR; return code from pthread_create() is %d%d\n"<<rcx<<rcs<<endl;
return 0;
}
while(1)
{
usleep(1600);
}
}