相关文章推荐
小胡子的日光灯  ·  recvfrom error ...·  2 周前    · 
大力的馒头  ·  recvfrom error ...·  2 周前    · 
体贴的葡萄  ·  python ...·  2 月前    · 
聪明伶俐的刺猬  ·  Timon ...·  9 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

My code is behaving very strange on Windows, but working on Linux... This is my server.cpp:

#include <cstdio>
#include "packet.h"
#include "socket.h"
int main(int argc, char *argv[])
Socket s;
s.bindAt(1337);
for (int i = 0; i < 20; i++) {
    Packet p;
    int32_t a;
    char *b;
    int abc = s.receive();
    printf("abc = %d\n", abc);
    printf("error = %d\n", WSAGetLastError());
    p.getInt(&a);
    p.getString(&b);
    printf("int = %d\nstring = %s\n", a, b);
    delete[] b;
return 0;

and here is the socket.cpp:

Socket::Socket()
#ifdef _WIN32
WSADATA wsa;
if (sockNum == 0 && WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    throw 1;
#endif
sock = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef _WIN32
if (sock == INVALID_SOCKET)
#else
if (sock == -1)
#endif
    throw 2;
addrlen = 0;
sockNum++;
int Socket::bindAt(unsigned short port)
struct sockaddr_in sa = { 0 };
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
return bind(sock, (struct sockaddr *) &sa, sizeof(sa));
ssize_t Socket::receive()
ssize_t n;
#ifdef _WIN32
char msg[100];
n = recvfrom(sock,msg, sizeof(msg), 0,(SOCKADDR*) &addr, &addrlen);
#else
n = recvfrom(sock, p->buf, p->bufSize, 0,
        (struct sockaddr *) &addr, &addrlen);
#endif
/*if (n < 0)
    p->bufSize = 0;
    p->bufSize = n;*/
return n;

and basically the header of it:

typedef SOCKET socket_t;
typedef int ssize_t;
class Socket
public:
socket_t sock;
socklen_t addrlen;
struct sockaddr_in addr;
Socket();
~Socket();
int connect(const char *ip, unsigned short port);
int bindAt(unsigned short port);
ssize_t send(Packet *p);
ssize_t receive();

If I change the last 2 parameters of the recvfrom, the (SOCKADDR*) &addr, and &addrlen to NULL it works, but what is wrong with these 2 parameters?

@dtb Look earlier in the MSDN. Under SOCK_DGRAM it says This socket type uses the User Datagram Protocol (UDP) for the Internet address family (AF_INET or AF_INET6). – Barmar Jun 25, 2013 at 20:20 The last parameter of recvfrom() is an in/out parameter, which is why it is passed by pointer. On input, it specifies the total size of the buffer. On output, it returns the amount of buffer space actually used. It is not a good idea to use a class member for that value, unless you re-initialize the value every time recvfrom() is called. Initializing it only in the constructor is not good enough. – Remy Lebeau Jun 25, 2013 at 22:17 @RemyLebeau Isn't that reinitialization necessary whether it's a class member or an ordinary variable? – Barmar Jun 26, 2013 at 14:35 @Barmar: It is necessary every time recvfrom() is called. That is why I said initializing it only in the constructor is not enough. The first time recvfrom() is called, the variable will get updated, and may have the wrong value for the next call to recvfrom() if it is not re-initialized. – Remy Lebeau Jun 26, 2013 at 17:26 @RemyLebeau I wasn't questioning whether it's necessary to reinitialize it every time, I was questioning your statement that it's not a good idea to use a class member for it. If addr is a class member, addrlen should also be, so that you can keep the two together. – Barmar Jun 26, 2013 at 18:24 @Barmar: Personally, I would change the addr member to a SOCKADDR_STORAGE and not worry about keeping track of the addrlen at all. Socket::Receive() could then use a local variable for addrlen when calling recvfrom(). – Remy Lebeau Jun 26, 2013 at 19:19

The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).

So if you supply an uninitialized value of addrlen, it may be too small, and result in this error.

If addr is NULL, it means you don't want the address filled in, so addrlen is ignored.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.