UNIX Socket FAQ

You are not logged in.

#1 2003-09-25 09:34 PM

adico
Guest

Re: socket question

Hi,

I have been given a c++ program which already connects to the server. My task is to do the following in the recv function:

/* read 4 bytes from socket */
/* convert Bigendian to long integer */
/* read in message 1024 bytes at a time. /*

I have very little experience with sockets programming, thus I dont know where to start.  Could anyone help? That is, if you have or know of a good link that covers programming socket in C++, would you please share it.

Thanks in advance.

#2 2003-09-26 01:25 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,739
Website

Re: socket question

Um, what's wrong with the Unix Socket FAQ forum right here?? ;-)
It doesn't specifically cover C++, no...  But, the sockets API
stuff is all strictly C library stuff natively, anyway...  But, there's
no reason you can't write C code in a C++ compiler, either... ;-)
Actually, I take that back: This question does indeed cover C++ a bit... ;-)

Offline

#3 2004-04-14 10:43 PM

aukahi
Member
From: Honolulu
Registered: 2004-04-14
Posts: 14

Re: socket question

the recv function's prototype is as follows:

int recv(int socket_descriptor, void *buffer, size_t buffer_length, int flags);
recv returns the number of bytes read.  due to the nature of internet communication, you cannot tell garantee that you can read x bytes at 1 time.

if you have access to an unix machine, type man recv to see the flags.  otherwise, you can just put 0 (zero) in it's place.

to read 4 bytes and only 4 bytes:

int bytes = 0, bytes_read;
char buffer[4];
while (bytes_read < 4) {
    bytes = recv(sd, &buffer[bytes], sizeof(buffer) - bytes_read;
    bytes_read += bytes;
}


to read 1024 bytes at a time, just put it in a while loop like this:

char buffer[1024];
while (1)
    recv(sd, &buffer, sizeof(buffer));


please note the recv will return -1 on error.

for converting big endian to long int, i assume to you are referring to converting network-byte order to host-byte order.
use the function
unsigned long int ntohl(unsigned long int network_lonf);

Offline

#4 2004-04-15 11:51 AM

Anders Goude
Member
From: Sweden
Registered: 2002-08-01
Posts: 6

Re: socket question

First of all, you need to check the return value of recv to see if the call was successful or not. Since you talked about this in your post, I assume that you only left this out in the example.

I found some small misstakes in the first code.

int bytes = 0, bytes_read;
char buffer[4];
while (bytes_read < 4) {
    bytes = recv(sd, &buffer[bytes], sizeof(buffer) - bytes_read;
    bytes_read += bytes;
}


First, you set bytes to 0, but not bytes_read. bytes_read is the one that needs to be 0.
Second, you use &buffer[bytes] in recv(). But bytes is the return value of the last recv call. If your loop will run more than twice, this will cause values in the buffer to be overwritten.

Use &buffer[bytes_read] instead.
Then the code will look something like this:

int bytes, bytes_read=0; 
char buffer[4]; 
while (bytes_read < 4) { 
    bytes = recv(sd, &buffer[bytes_read], sizeof(buffer) - bytes_read,0);
    if(bytes == 0) {
         //The connection has been closed
    }
    else if(bytes == -1) {
         //There were some error
    }
    bytes_read += bytes; 
}


For the second loop where you read the 1024 bytes, you need to do it in the same way as with the four bytes

Offline

#5 2004-04-16 12:32 AM

aukahi
Member
From: Honolulu
Registered: 2004-04-14
Posts: 14

Re: socket question

yeah, i know, i was kind of half asleep at that time.

Offline

#6 2004-05-05 07:37 PM

sleepymish
Member
Registered: 2004-05-05
Posts: 7

Re: socket question

This is an excellent socket tutorial for begineers!

http://www.ecst.csuchico.edu/~chafey/pr … info1.html

Offline

Board footer

Powered by FluxBB