Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2006, 4:30 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 Question

I am just starting socket programming in c++; I am just using regular sockets with TCP protocol. I have it so I can transfer text; is there a way I could transfer a whole object or vector? I am guessing I would have to send it as binary. If someone could send me some good links so I can do some research on how to that, that would be great. Thanks in advance.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Feb 28th, 2006, 4:58 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
A whole object is easy, as long as your object doesn't contain pointers:
objecttype nameofobject;

send(sock, (const char *) &nameofobject, sizeof(objecttype));
I don't know how to safe this code is. It should work with two programs compiled by the same compiler, I guess.


You can't send a vector this way. You'll have to put all the objects in an array and send it at once, or send the objects of the vector one by one.
Polyphemus_ is offline   Reply With Quote
Old Feb 28th, 2006, 11:07 PM   #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
ok thanks.. i'll give it a try
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 1st, 2006, 1:54 AM   #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
I have a couple more questions on sockets and threads:
On the server side I have a loop that accepts client connections untill it reaches a certain mumber of clients. After it accepts a client it threads a fucntiuon which will receive some data from the client. I threaded that part so it can still accept more clients while its dealing with the previouse one. Looks like this:
for(int i = 0; i != numOfClients; ++i)
  {
    addrLen = sizeof(clientAddr);
    clientDescriptor = accept(socketDescriptor, (sockaddr *) & clientAddr,
                              (socklen_t *) & addrLen);
    
    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;
  }
  shutdown(socketDescriptor, SHUT_RDWR);
The problem: Say I have it so i want 4 clients to be accepted, on the last itteration through the loop to deal with a client... it seems like the threaded fucntion to deal with the client isnt being accepted... any ideas why that would be? Right after the last line in the above code is the end of that function.. so maybe it is exiting the function before the thread is fisnished? Aslo the threaded functions edits some variables... should i use a mutex to make sure multple threads are not trying to edit data at the same time if 2 clients connect at the same time?

Thanks for any help guys!
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 1st, 2006, 2:17 AM   #5
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
One problem I see right away is that you pass in (void *) &clientDescriptor to the thread function. This is a problem because once a new connection is accepted, the clientDescriptor variable gets changed for all the currently running threads. One way to fix this is to allocate a new int for each new client descriptor:
int *clientDescriptor = new int;
*clientDescriptor = accept(socketDescriptor, (sockaddr *) & clientAddr,
                              (socklen_t *) & addrLen);

pthread_t currentTID;
if( pthread_create(&currentTID, NULL, (void *(*)(void *)) handleClient, clientDescriptor) == 0)
  pthread_detach(currentTID);

If threads edit only local variables that no other threads can edit, you don't need a mutex. But if a thread is changing data that other running threads could also change, you're going to need some sort of lock (typically a mutex).
Kaja Fumei 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 11:51 PM.

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