Re: [POSIX & Win32API] Getting the 'routing' informations
- From: Rafael <devlists@xxxxxxxxxx>
- Date: Mon, 19 Jan 2009 04:13:37 -0300
Hello Maciej, List
Maciej Piechotka escreveu:
> PS. Sample pseudocode:
I can see the pseudo, but where is the code?
> int test_socket (SOCKET s, struct sockaddr *sockaddr, socklen_t socklen)
> {
> /* ... */
> }
You should return EXIT_FAILURE; here, or people will call your post off-topic.
> I have a binded datagram/packet socket. Is it possible to get an
> information should I send a packet with given sockaddr through this
> socket?
I'm Sorry Maciej, but your question... mmm... I can't pick a side. So many ways to read this.
> I'd like to have at most 2 codes - for BSD sockets and winsock2.
But ok... winsock2 and berkeley sockets are (kind) the same. You can just use a couple ifdefs and you're fine.
on windows, includes should be:
#include <winsock.h>
on linux:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
on windows, you have to do this extra stuff:
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);
for both:
int thisSocket;
struct sockaddr_in destination;
destination.sin_family = AF_INET;
thisSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (thisSocket < 0)
{
printf("\nSocket Creation FAILED!");
return 0;
}
then, to close the socket, again we have some
windows stuff:
closesocket(thisSocket)
WSACleanup();
and linux stuff:
close(thisSocket)
Ok... that wasn't really a howto. Its just to show you that, a cross-platform implementation can work witout too many problems (with sockets...).
I usually get the cross-platform job done this way:
#ifdef __WIN32
#include <winsock.h>
#endif
#ifdef __LINUX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void closesocket(int socket) { close(socket); }
#endif
#ifdef __WIN32
WSADATA wsaData;
#endif
#ifdef __WIN32
WSAStartup(0x0202, &wsaData);
#endif
#ifdef __WIN32
WSACleanup();
#endif
Rafael
.
- Follow-Ups:
- Re: [POSIX & Win32API] Getting the 'routing' informations
- From: Maciej Piechotka
- Re: [POSIX & Win32API] Getting the 'routing' informations
- References:
- [POSIX & Win32API] Getting the 'routing' informations
- From: Maciej Piechotka
- [POSIX & Win32API] Getting the 'routing' informations
- Prev by Date: Re: Reading stderr
- Next by Date: Re: [POSIX & Win32API] Getting the 'routing' informations
- Previous by thread: Re: [POSIX & Win32API] Getting the 'routing' informations
- Next by thread: Re: [POSIX & Win32API] Getting the 'routing' informations
- Index(es):
Relevant Pages
|