• Advertisement

Archived

This topic is now archived and is closed to further replies.

Network programming in C++

This topic is 6448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm trying to port my networking routines to C++, but it doesn't seem to work. I'm using something like
struct sockaddr_in MySocket;
int SocketHandle

MySocket.sin_port=22333;

// protocol_number is set correctly
SocketHandle=socket( AF_INET, SOCK_STREAM, protocol_number );
bind( SocketHandle, &MySocket, sizeof( MySocket ) );
 
The compiler doesn't like this. It's telling me that it can't convert between sockaddr_in and sockaddr (which is the type specified in the syntax of bind). Trying to cast this, even with reinterpret_cast or via a void pointer fail. In pure C, this was no problem. The trouble is that I have to set MySocket.sin_port which isn't defined in the normal sockaddr structure. Does anyone know how to fix this? --- ls -lR/rm -rf / (Best compression everywhere. Kids, don't try this as root.) Edited by - Shadowdancer on 6/25/00 10:24:39 AM Edited by - Shadowdancer on 6/25/00 10:26:25 AM

Share this post


Link to post
Share on other sites
Advertisement
try:


struct sockaddr_in *MySocket;
int SocketHandle

MySocket->sin_port=22333;

// protocol_number is set correctly
SocketHandle=socket( AF_INET, SOCK_STREAM, protocol_number );
bind( SocketHandle, (sockaddr *)MySocket, sizeof( MySocket ) );


(Cast MySocket to type sockaddr instead of sockaddr_in)

Mark Collins (aka Nurgle)
me@thisisnurgle.org.uk

Share this post


Link to post
Share on other sites
Hey, thanks that fixed it. Guess I''ll have to re-read that chapter about using pointers...

---
ls -lR|rm -rf /
(Best compression everywhere. Kids, don''t try this as root.)

Share this post


Link to post
Share on other sites

  • Advertisement