UDP Socket

May 26, 2010 at 9:32am
closed account (G26pX9L8)
Hallo,

I have made a socket server using udp. Everything works fine but when the client write something to the server i see some strange characters. And when de server sends a message to th client the same happens. What goes wrong or is this just normal?
Last edited on Jun 19, 2010 at 3:53pm
May 26, 2010 at 9:37am
I can't guess your problem exactly but I can suggest the following

It depends on what you send.
If it's symbols from non ASCI table you need to set correct local.
May 26, 2010 at 9:59am
closed account (G26pX9L8)
I'm only sending words such as "Server Ready". Btw i don't know how to convert or something cause i'm a c++ noob
May 26, 2010 at 10:06am
Please try to create a buffer, init this buffer by "Server Ready" and send this buffer.
May 26, 2010 at 11:27am
closed account (G26pX9L8)
This is the code of the UDP 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <winsock2.h>

using namespace std;

int main()
{
    //init
    int server_length;
    int port = 123;
    int STRLEN = 256;
    char recMessage[STRLEN];
    char sendMessage[STRLEN];
    char *sendMes = "SERVER READY";
    WSADATA wsaData;
    SOCKET mySocket;
    SOCKET myBackup;
    SOCKET acceptSocket;
    sockaddr_in myAddress;

    //create socket
    if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
    {
        cerr<<"Socket Initialization: Error with WSAStartup\n";
        system("pause");
        WSACleanup();
        exit(10);
    }

    mySocket = socket(AF_INET, SOCK_DGRAM, 0);
    if (mySocket == INVALID_SOCKET)
    {
        cerr<<"Socket Initialization: Error creating socket"<<endl;
        system("pause");
        WSACleanup();
        exit(11);
    }

    myBackup = mySocket;

    //bind
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
    myAddress.sin_port = htons(port);

    if(bind(mySocket, (SOCKADDR*) &myAddress, sizeof(myAddress)) == SOCKET_ERROR)
    {
        cerr<<"ServerSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(14);
    }

    cout<<endl;
    while (1)
    {
        server_length = sizeof(struct sockaddr_in);
        recvfrom(mySocket, recMessage, STRLEN, 0, (SOCKADDR*) &myAddress, &server_length);
        cout<<recMessage<<endl;
        sendto(mySocket, sendMes , strlen(sendMes), 0, (SOCKADDR*) &myAddress, server_length);

    }

    return 0;
}

and the client receive something like 7d&SERVER READYux
May 26, 2010 at 12:15pm
You're not null terninating your string or checking how many characters were received or sent.
May 26, 2010 at 1:00pm
closed account (G26pX9L8)
And what is the best way to do that?
May 26, 2010 at 1:42pm
recvfrom returns the number of bytes read, or if an error occured (-1)...

if you entlongend your recvbuffer by one, then you could to the following

1
2
3
int ret = recvfrom(/*args*/);
/* check*/
recMessage[ret] = '\0';
May 26, 2010 at 3:00pm
closed account (G26pX9L8)
I add the code above to my server and in stand of 7d&SERVER READYux it returns 7d&SERVER READY. How can I make sure that the first three characters going out as well?
May 26, 2010 at 3:11pm
Do not send them ;)...
Topic archived. No new replies allowed.