![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
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! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
You could go back to using string and use the c_str() member function to convert the string to a const char *
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
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! |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
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(¤tTID, 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! |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
I'm not sure about why it shuts down, but this line looks wrong:
if( pthread_create(¤tTID, NULL, (void *(*)(void *)) handleClient, (void *) &clientDescriptor) == 0) |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
OK i tried what Kaja Fumei said and it didnt work.. any other ideas?
__________________
I am Addicted to Linux! |
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
no i checked it by hardcoding in the host and still didn't work.
__________________
I am Addicted to Linux! |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|