send and recv in winsock2

so im just getting into network programming and im trying to make a chat server and client so i can send and receive messages. I have written both a chat and a client, with no error reporting (i know, i know). But the message that comes through on the server side is "hiiinaovH+". Ive represented this as well as i can but really they are non alphabetic symbols coming through. why does this happen? Also, if you guys could point me to some helpful tutorials, thatd be awesome.


server code:
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
44
45
46
47
#include <iostream>
#include <winsock2.h>

int main()
{
    WSAData wsa;
    WORD Version=MAKEWORD(2,1);
    WSAStartup(Version, &wsa);

    SOCKET Listen=socket(AF_INET, SOCK_STREAM, NULL);
    SOCKET sock=socket(AF_INET, SOCK_STREAM, NULL);

    SOCKADDR_IN Server;

    Server.sin_addr.s_addr=inet_addr("127.0.0.1");
    Server.sin_family=AF_INET;
    Server.sin_port=htons(100);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

    listen(Listen, 1);

    int size=sizeof(Server);

    std::cout<<"Listening...";

    for (;;)
    {
        if (sock=accept(Listen, (SOCKADDR*)&Server, &size))
        {
            std::cout<<"\nConnection was reached";
            break;
        }
    }

    char buf[4096];
std::cout<<std::endl;
    while (recv(sock,buf,4096,0))
{
    std::cout<<buf;
}

    WSACleanup();
    std::cin.get();
    return 0;
}


client code:
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
44
45
46
47
#include <iostream>
#include <winsock2.h>

int main()
{
    WSAData wsa;
    WORD Version=MAKEWORD(2,1);
    WSAStartup(Version, &wsa);

    SOCKADDR_IN Client;

    Client.sin_addr.s_addr=inet_addr("127.0.0.1");
    Client.sin_family=AF_INET;
    Client.sin_port=htons(100);


SOCKET sock=socket(AF_INET, SOCK_STREAM, NULL);

std::cout<<"Press enter to continue...";
std::cin.get();

for (;;)
{
    if (connect(sock, (SOCKADDR*)&Client, sizeof(Client)))
        {
            break;
        }
}

char buf[4096];
std::cout<<"Connection sent!";
std::cin.get();


    char *mymsg="hiiiii";

    int smsg=send(sock,mymsg,sizeof(mymsg),0);





closesocket(sock);
WSACleanup();
return 0;
}
Where to begin ...

It's best if you read as much of this as you have patience for. http://beej.us/guide/bgnet/

But once you get your head around what's involved, I recomend you use a a library for comms. One good example is Boost. There's support of ASIO for high performace environments, or TCP Streams for ease of use. For example: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/overview/networking/iostreams.html

But you need to understand a bit about networking before using it.
Last edited on
Hi Try this..

on server side line 36

char buf[4096]; //36

ZeroMemory(buf,sizeof(buf)) ; //add this line 37

hope it helps.
Last edited on
josepho16 wrote:
But the message that comes through on the server side is "hiiinaovH+".
The reason for this is in the client code on line 37:
sizeof(mymsg) // This is the size of the pointer (in your case obviously 4)

use strlen instread:
strlen(mymsg) + 1 // Note: + 1 is important to send the trailing 0
Topic archived. No new replies allowed.