Changing far pointers to pointers?

Started by
13 comments, last by jflanglois 17 years, 7 months ago
Ok, I'm using WSARecv for an IOCP server, and I am looking at the platform sdk example from feb 2003. Anyway, it is an echo server, but I'd like to parse it so basic commands can be read. The wsabuf is what contains the data, I believe, and it's buf is a far pointer. I tried all things I could think of and I always get an address printed. If someone can help me out about this, thatd be great!
Advertisement
Can we see the code you are using? You can safely ignore the FAR and use a char * (assuming this is C++ or a 32 bit platform).


jfl.
Im using a WSARecv function; which uses a wsabuf, which has 2 parts to its struct:
buf, a far char* and a ulong len.

Iam not getting the wsabuf.buf to get converted into something I can work with.
Alright, what language are you using, and could you please show some of your code?
C++ in VC++ 6.0:


Here's the struct that contains wsabuf
typedef struct _PER_IO_CONTEXT{		WSAOVERLAPPED               Overlapped;    char                        Buffer[MAX_BUFF_SIZE];    WSABUF                      wsabuf;    int                         nTotalBytes;    int                         nSentBytes;    IO_OPERATION                IOOperation;    SOCKET                      SocketAccept;     struct _PER_IO_CONTEXT      *pIOContextForward;} PER_IO_CONTEXT, *PPER_IO_CONTEXT;


I dont know what else to show you; the example is over 1600 lines and I did little to no modification.

All I'm trying to do is access the data recieved and the data being sent so I can parse it and handle it accordingly. I appreciate your willingness to help!
You are using that structure? According to the MSDN, WSARecv() just takes an array of WSABUFs. Those WSABUFs have a buffer and a length. You should probably copy from the buffer into your own char array so that you'll have a null terminated string. Then print that.
Well, it really depends on what you are trying to do with the data. Again, you can safely ignore the far specifier since it's a C-compatibility "feature". I'll give a few examples:


Copy the buffer to a vector:
std::vector< char > buffer( wsabuf.buf, wsabuf.buf + wsabuf.len );

(or)
std::vector< char > buffer;buffer.resize( wsabuffer.len );std::copy( wsabuf.buf, wsabuf.buf + wsabuf.len, buffer.begin() );


Send the buffer to std::cout (creating begin and end pointers this time):
char const * const begin = wsabuf.buf;char const * const end = wsabuf.buf + wsabuf.len;std::copy( begin, end, std::ostream_iterator( std::cout ) );


Parse directly (with no regard for endianness):
char const * const begin = wsabuf.buf;char const * const end = wsabuf.buf + wsabuf.len;for( char const * it = begin; it != end; ++it ) {  if( 32 == *reinterpret_cast< int * >( it ) ) {    std::cout << "We have the number 32. It stands for something." << std::endl;    // process data after 32.  }}



I don't know. What example are you using?

By the way, Visual C++ 6.0 is out of date. I highly recommend you update to Visual C++ 2005 Express Edition (you would also need the Platform SDK). It's free.


jfl.
Quote:Original post by Colin Jeanne
You are using that structure? According to the MSDN, WSARecv() just takes an array of WSABUFs. Those WSABUFs have a buffer and a length. You should probably copy from the buffer into your own char array so that you'll have a null terminated string. Then print that.


Straight from the MSDN:

int WSARecv(  SOCKET s,  LPWSABUF lpBuffers,  DWORD dwBufferCount,  LPDWORD lpNumberOfBytesRecvd,  LPDWORD lpFlags,  LPWSAOVERLAPPED lpOverlapped,  LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);


Also, I try copying it, and when it prints, it seems to print its ASCII value of the first character only. Im sufre I'm doing something wrong here, but the WSABUF has 2 things, yes, the buf and the len. I copy the buf part, but it usually gives me errors.

for instance:

&jBuf[0] = lpIOContext->wsabuf.buf;

wont compile

jBuf[0] = *lpIOContext->wsabuf.buf;

prints the ascii.

ive tried several other methods even if I think theyd be wrong, just because I'm out of ideas.



As for VC6.0 being out of date? It gets the job done fine, and I'd rather stick with it for now, thanks for your input though.
Er, I think I see your point where you can replace an array rather than a pointer, but still;

The server echos it fine, the data that is sent by the client is echo'd back.
When I say out of date, I mean pre-standard. That means that VC6.0 can barely be called a C++ compiler.

As for your code examples:

- The first is trying to assign to the value returned by the address-of operator, which is an rvalue.

- The second only copies the first character in the buffer to your array.

If you want to copy the data in the buffer to your array, then (assuming your array is large enough):
std::copy( wsabuf.buf, wsabuf.buf + wsabuf.len, jBuf );

As for printing ascii, what exactly are you expecting?


jfl.

This topic is closed to new replies.

Advertisement