I would like to know why i am unable to send data when using threads with sockets.
I was wondering why when i send using the first method(without threads), it works, but using the second method, it fails to send data.
This is my declaration :
int main(int argc, char *argv[])
{
int sd, rc, i;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
h = gethostbyname(argv[1]);
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr,h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
/* socket creation */
sd = socket(AF_INET,SOCK_DGRAM,0);
/* bind any port */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
socklen_t remoteServLen = sizeof(remoteServAddr);
//this is my own class to store the pointers to the following variables
clientInfo ci(&sd,&rc,&remoteServLen,&remoteServAddr,&cliAddr);
/FIRST METHOD/
char *data;
char input[MAX_MSG];
std::cout << "Enter message to send (type /q to quit) : ";
std::cin >> input;
data = input;
rc = sendto(*(ci.getSd()), data, strlen(data)+1, 0,(struct sockaddr *) ci.getCliAddr(),*(ci.getCliLen()));
/FIRST METHOD/
/SECOND METHOD/
pthread_t thread[2];
int status;
status = pthread_create (&thread[1], NULL, sendFunction,&ci);
/SECOND METHOD/
}
/This is my thread method/
void* sendFunction (void* temp)
{
int status;
char *data;
char input[MAX_MSG];
int rc;
clientInfo *ci;
ci = (clientInfo*)temp;
status = pthread_detach (pthread_self ());
while(1)
{
std::cout << "Enter message to send (type /q to quit) : ";
std::cin >> input;
data = input;
rc = sendto(*(ci->getSd()), data, strlen(data)+1, 0,(struct sockaddr *) ci->getCliAddr(),*(ci->getCliLen()));
if(rc<0)
{
printf("Cannot send data %s \n",data);
}
}//end of while loop
return NULL;
}//end of sendFunction method
:) thanks in advance! :D