![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
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! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
A whole object is easy, as long as your object doesn't contain pointers:
objecttype nameofobject; send(sock, (const char *) &nameofobject, sizeof(objecttype)); 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. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
ok thanks.. i'll give it a try
__________________
I am Addicted to Linux! |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
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(¤tTID, NULL, (void *(*)(void *)) handleClient, (void *) &clientDescriptor) == 0)
pthread_detach(currentTID);
// else there was an error and thread not created;
}
shutdown(socketDescriptor, SHUT_RDWR);Thanks for any help guys!
__________________
I am Addicted to Linux! |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
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(¤tTID, 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). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|