Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 3rd, 2006, 10:03 PM   #1
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
Question Socket Problem!

In my program I have a server that will accepts a certain number of players for a card game. Each time a player is accepted it calls a function where the server gets the IP adress from the player that just connected and stores the IP in a vector. Then after all the players have connected i need the server to to loop threw the vector of IPs and send some info back to each player. As it loops threw the vector i wanted to use gethostbyname fucntion passing in the IP to connect to each player. The problem is that functions wants a char* to be passed in, and my vector is type string. I didn't know how to convert a string to that so i decided to make the vector of type char*. the problem with that is, when i call the functions to get the ip from the player, i would make a char to store the ip and then push it into the vector. But after that fucntion is done.. the vector isnt pointing to anything anymore and the data is lost. So how do i get this to work? a friend said i had to "new" the char in the function b4 pushing it into the vector.. but not sure how to do that. Any help would be great.. thanks guys. if you need me to post code i can.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 3rd, 2006, 11:33 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
You could go back to using string and use the c_str() member function to convert the string to a const char *
The Dark is offline   Reply With Quote
Old Mar 4th, 2006, 12:34 AM   #3
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
I tried that before and it didn't work, but i am thinking it was another bug because it works fine now. thanks
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 4th, 2006, 5:02 PM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
OK I have another problem. Like I said above clients connect to the server passing their IPs in and then stored into a vector. Then after all the players have connect I want the server to loop threw all the IPs and send all the players a message that the game is starting. The problem is when the server tries sending somehting to the clients the server just stops running for some reason. Here is some code, I think i may be going about this a bit wrong:

int main(int argc, char *argv[])
{
  cout << "How many players are there going to be?" << endl;
  int numOfPlayers;
  cin >> numOfPlayers;
  cout << endl;
  
  acceptPlayers(numOfPlayers);
  sendMessage();
  
  return 0;
}

void acceptPlayers(int players)
{
  int numOfPlayers = players;
  // allocate and initialize permanent socket
  protoent *protTableEnt = getprotobyname("tcp");
  int socketDescriptor = socket(PF_INET, SOCK_STREAM, protTableEnt->p_proto);

  sockaddr_in servAddr = { 0 };
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = INADDR_ANY;
  servAddr.sin_port = htons(1042);
  bind(socketDescriptor, (sockaddr *) & servAddr, sizeof(servAddr));

  // connect to clients
  listen(socketDescriptor, 26);

  int addrLen;
  sockaddr_in clientAddr = { 0 };
  int clientDescriptor;
  for(int i = 0; i != numOfPlayers; ++i)
  {
    addrLen = sizeof(clientAddr);
    clientDescriptor = accept(socketDescriptor, (sockaddr *) & clientAddr,
                              (socklen_t *) & addrLen);
     
    // create thread to get player's ip and store them into a vector
    pthread_t currentTID;
    if( pthread_create(&currentTID, NULL, (void *(*)(void *)) handleClient, (void *) &clientDescriptor) == 0)
      pthread_detach(currentTID);
    // else there was an error and thread not created;
    sleep(2);
  }
  shutdown(socketDescriptor, SHUT_RDWR);
}

void sendMessage()
{
  for(int i = 0; i !=  vecIP.size(); ++i)
  {
    hostent *hostTableEntry;
    hostTableEntry = gethostbyname(vecIP[i].c_str());

    // allocate and initialize socket
    protoent *protTableEnt = getprotobyname("tcp");
    int socketDescriptor = socket(PF_INET, SOCK_STREAM, protTableEnt->p_proto);
  
    // connect
    sockaddr_in sockAddr = { 0 };
    sockAddr.sin_family = AF_INET;
    memcpy(&sockAddr.sin_addr, hostTableEntry->h_addr, hostTableEntry->h_length);
    sockAddr.sin_port = htons(1042);
    connect(socketDescriptor, (sockaddr *) & sockAddr, sizeof(sockAddr));
  
    // send data
    char ip[25] = "Ready to start game!";
    send(socketDescriptor, ip, strlen(ip),0);
  
    shutdown(socketDescriptor,SHUT_RDWR);
  }
}
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 5th, 2006, 6:15 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
I'm not sure about why it shuts down, but this line looks wrong:
    if( pthread_create(&currentTID, NULL, (void *(*)(void *)) handleClient, (void *) &clientDescriptor) == 0)
You are passing the address of the same local variable to each new thread. This means that they will all (eventually) see the same value and that when the local variable goes out of scope, the value will be overwritten.
The Dark is offline   Reply With Quote
Old Mar 5th, 2006, 1:22 PM   #6
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
Quote:
Originally Posted by The Dark
I'm not sure about why it shuts down, but this line looks wrong:
    if( pthread_create(&currentTID, NULL, (void *(*)(void *)) handleClient, (void *) &clientDescriptor) == 0)
You are passing the address of the same local variable to each new thread. This means that they will all (eventually) see the same value and that when the local variable goes out of scope, the value will be overwritten.
I pointed this out in his last thread too: http://www.programmingforums.org/for...67&postcount=5
Kaja Fumei is offline   Reply With Quote
Old Mar 5th, 2006, 3:18 PM   #7
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
OK i tried what Kaja Fumei said and it didnt work.. any other ideas?
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 5th, 2006, 4:29 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Without all the code, I am just stabbing in the dark (hehe) here. You seem to by passing an IP address into gethostbyname, but gethostbyname takes a host name has its parameter, not an IP address. You should at the very least check the return value to function calls such as gethostbyname. If it is returning NULL, then your program will crash, as you dereference the pointer.
The Dark is offline   Reply With Quote
Old Mar 5th, 2006, 4:58 PM   #9
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
no i checked it by hardcoding in the host and still didn't work.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 5th, 2006, 6:21 PM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Then like The Dark said show us more code.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC