UDP Server problem

Hi all,
i want to build a UDP server which permanently sents data without receiving anything. I tried a bit with a Client-Server Test but it only works when the Server receives a message first. Otherwise i get the error code 10047 for sendto() in the server application. I put my code underneath, the send to the server and the receiving of the server is comment out.

Does anyone know what i am doing wrong?
Thanks for your reply.

Jan

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Server
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>

//Prototypes
int startWinsock(void);

int main()
{
  long rc;
  SOCKET s;
  char buf[256];
  char buf2[300];
  char bufpermanent[100] = "Hello";
  SOCKADDR_IN addr;
  SOCKADDR_IN remoteAddr;
  int         remoteAddrLen=sizeof(SOCKADDR_IN);

  rc=startWinsock();
  if(rc!=0)
  {
    printf("Error: startWinsock, error code: %d\n",rc);
    return 1;
  }
  else
  {
    printf("Winsock started!\n");
  }

  //UDP Socket erstellen
  s=socket(AF_INET,SOCK_DGRAM,0);

  if(s==INVALID_SOCKET)
  {
    printf("Error: Socket could not be created, error code: %d\n",WSAGetLastError());
    return 1;
  }
  else
  {
    printf("UDP Socket created!\n");
  }

  addr.sin_family=AF_INET;
  addr.sin_port=htons(1234);
  addr.sin_addr.s_addr=ADDR_ANY;
  rc=bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));

  if(rc==SOCKET_ERROR)
  {
    printf("Error: bind, error code: %d\n",WSAGetLastError());
    return 1;
  }
  else
  {
    printf("Socket at Port 1234\n");
  }

  while(1)
  {
    /*rc=recvfrom(s,buf,256,0,(SOCKADDR*)&remoteAddr,&remoteAddrLen);
    if(rc==SOCKET_ERROR)
    {
      printf("Error: recvfrom, error code: %d\n",WSAGetLastError());
      return 1;
    }
    else
    {
      printf("%d Bytes received!\n", rc);
      buf[rc]='\0';
    }
    printf("Received data: %s\n",buf);*/

    //Answer
    //sprintf(buf2,"Hello %s",buf);
    rc=sendto(s,buf2,strlen(buf2),0,(SOCKADDR*)&remoteAddr,remoteAddrLen);
    if(rc==SOCKET_ERROR)
    {
      printf("Error: sendto, error code: %d\n",WSAGetLastError());
      return 1;
    }
    else
    {
      printf("%d Bytes send!\n", rc);
    }
  }
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&wsa);
}


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Client
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>

//Prototypes
int startWinsock(void);

int main()
{
  long rc;
  SOCKET s;
  char buf[256];

  SOCKADDR_IN addr;
  SOCKADDR_IN remoteAddr;
  int         remoteAddrLen=sizeof(SOCKADDR_IN);

  rc=startWinsock();
  if(rc!=0)
  {
    printf("Error: startWinsock, Error code: %d\n",rc);
    return 1;
  }
  else
  {
    printf("Winsock started!\n");
  }

  //UDP Socket
  s=socket(AF_INET,SOCK_DGRAM,0);
  if(s==INVALID_SOCKET)
  {
    printf("Error: Socket could not be created, Error code: %d\n",WSAGetLastError());
    return 1;
  }
  else
  {
    printf("UDP Socket created!\n");
  }

  // addr
  addr.sin_family=AF_INET;
  addr.sin_port=htons(1234);
  addr.sin_addr.s_addr=inet_addr("127.0.0.1");

  rc = connect(s,(SOCKADDR*)&addr, sizeof(SOCKADDR_IN));

  while(1)
  {
    /*printf("Text: ");
    gets(buf);
    //rc=sendto (s,buf,strlen(buf),0,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
    rc = send(s,buf,strlen(buf),0);
    if(rc==SOCKET_ERROR)
    {
      //printf("Error: sendto, Error code: %d\n",WSAGetLastError());
      printf("Error: send, Error code %d\n",WSAGetLastError());
      return 1;
    }
    else
    {
      printf("%d Bytes send!\n", rc);
    }*/

    rc=recv(s,buf,256,0);

    if(rc==SOCKET_ERROR)
    {
      printf("Error: recv, Error code: %d\n",WSAGetLastError());
      return 1;
    }
    else
    {
      printf("%d Bytes received\n", rc);
      buf[rc]='\0';
      printf("Received data: %s\n",buf);
    }
  }
  return 0;
}
int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&wsa);
}
Maybe I'm missing something, but I think you need to fill in your remoteAddr struct (in your server) before you can use it in the sendto().
The whole point of Servers is to wait around for a client to connect. You need both ends of a TCP/IP link to be made before communications between them to happen.

The way you have written it is not the way server sockets are usually setup.
The usual steps are:

1. Create socket (you have done that)
2. Bind to a local interface (you have done that)
3. Listen (this means look out for incoming connections)
4. Accept (accept an incoming connection from a client)

5. Do the Sending and receiving (a successful call to Accept will return a new socket for you to send/receive on)
Topic archived. No new replies allowed.