Sockets ( not receiving data )

Hi guys

I seem to be able to get a connection between the server and client but when the server tries to print out the buffer it prints and empty buffer just saying " the client says " and no string following that

I must be doing something wrong with the strings perhaps

client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <iostream>
#include <winsock2.h>

using namespace std;

int main()
{
    WSADATA wsa;
    SOCKET server;
    SOCKADDR_IN serverAddr;

    char buffer[] = {'h','e','l','l','o','\0'};
    cout << sizeof(buffer) << endl;

    long success = WSAStartup(MAKEWORD(2,0),&wsa);

    server = socket(AF_INET,SOCK_STREAM,0);

    serverAddr.sin_addr.s_addr = inet_addr("192.168.0.206");
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(4447);


    while(1){

    connect(server,(SOCKADDR*)&serverAddr,sizeof(serverAddr));

    send(server,buffer,2000,NULL);
    }

    closesocket(server);
    WSACleanup();
    cout << "finished" << endl;
    cout << sizeof(buffer) << endl;
}



server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <winsock2.h>

using namespace std;

int main()
{
    WSADATA wsa;
    SOCKET server,client;
    SOCKADDR_IN serverAddr,clientAddr;

    WSAStartup(MAKEWORD(2,0),&wsa);

    server = socket(AF_INET,SOCK_STREAM,0);

    serverAddr.sin_addr.s_addr = INADDR_ANY;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(4447);

    bind(server,(SOCKADDR*) &serverAddr, sizeof(serverAddr));
    listen(server,0);

    cout << "listening" << endl;

    char buffer[2000];
    int clientAddrSize = sizeof(clientAddr);

    if(client = accept(server,(SOCKADDR*)&clientAddr,&clientAddrSize) != INVALID_SOCKET){

        cout << "client is now connected" << endl;
        recv(client,buffer,sizeof(buffer),NULL);
        cout << "client says :: " << buffer << endl;

    }else{
      cout << "nope" << endl;
    }

    closesocket(client);
    WSACleanup();

    return 0;
}



the recv function is giving a -1 value back meaning there was an error with the data being sent. but not sure where this error could be occuring

*edit more information that may help

the error code I get is 10038 - it was discussed on SO - https://stackoverflow.com/questions/3948164/10038-socket-error

but I fail to see where one of my sockets may be closed

moving the accept function before the while loop solved the problem but not too sure why it solved the problem if anybody could elobarate on this.

1
2
3
4
5
6
7
8
9
10
11
12

    client = accept(server,(SOCKADDR*)&clientAddr,&clientAddrSize);

    while(1){

        int bites = recv(client,buffer,sizeof(buffer),NULL);
        if(bites == SOCKET_ERROR)
            printf(" error %ld. \n", WSAGetLastError());

         cout <<  " client saye :: " << buffer << endl;
    }


also I now decided to send my message which is just hello in a loop so it will constantly print hello on the server, but on the server sometimes hello wil be incomplete, is there anyway to only print hello when the whole sring has been transmitted?

1
2
3
4
5
6
7
8
9
10

char buffer[] = {'h','e','l','l','o','\0'};

    connect(server,(SOCKADDR*)&serverAddr,sizeof(serverAddr));

    while(1){

    send(server,buffer,sizeof(buffer),NULL);
    }



thanks
Last edited on
The Client
The server is expecting a single message. So the client shouldn't have a while loop. It should:
connect
send
close

Don't send 2000 bytes. Send strlen(buffer), which is the 5 bytes of "hello". There's no need to send the trailing null as recv will return the number of bytes received.

The Server
The code is mostly ok. But you're not checking return codes.

listen() should have a buffer size, on a Windows sample, 5 is a reasonable value.

accept() returns a socket connection to the client. So call recv(), then close() on it.

There's more stuff I can say, but the code ought to work with the changes. If it doesn't, the return codes will tell you what's failed, where and why.
also I now decided to send my message which is just hello in a loop so it will constantly print hello on the server, but on the server sometimes hello wil be incomplete, is there anyway to only print hello when the whole sring has been transmitted?


Both send() and recv() can be partially successful.
You can try to send "hello", but send() might return say 2, indicating that only "he" was sent, and that you need to try again to send "llo".

Similarly, at the receiver, you might only end up with recv() returning say 3, where you have "hel" in the buffer, and you need to call recv() again to fetch "lo".

Your only guarantee with a stream connection is that the bytes you send will arrive in the order you send them. There is NO additional synchronisation between the two ends, like for example assuming a 1:1 correspondence between send and recv calls, that buffers always match, or that buffers have specific terminators.
Topic archived. No new replies allowed.