Windows Chat Application

Hi there

I have some trouble with my Chat application.

The main goal is having a chat Server and a chat Client. Every message from a client should be sent to the server, the server then should broadcast the message to every other client.

It works all fine but the server is not broadcasting the message.

Are there any suggestions?

Chat 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
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
95
#pragma comment (lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

//Prototypen
int startWinsock(void);
int main()
{
  long rc;
  SOCKET s;
  char buf[256];
  SOCKADDR_IN addr;
  SOCKADDR_IN remoteAddr;
  int remoteAddrLen=sizeof(SOCKADDR_IN);
  std::string exitclient = "exit";
  std::string exitserver = "shutdown";
  char vergleich[256];

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

  //UDP Socket erstellen
  s=socket(AF_INET,SOCK_DGRAM,0);
  if(s==INVALID_SOCKET)
  {
    printf("Fehler: Der Socket konnte nicht erstellt werden, Fehler code: %d\n",WSAGetLastError());
    return 1;
  }
  else
  {
    printf("UDP Socket erstellt!\n---------------------------------\n");
  }

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


  // Daten austauschen
  while(1)
  {
    printf("Text eingeben: ");
    gets(buf);



    rc=sendto (s,buf,strlen(buf),0,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
    if(rc==SOCKET_ERROR)
    {
      printf("Fehler: sendto, Fehler code: %d\n",WSAGetLastError());
      return 1;
    }

    strcpy (vergleich, buf);

    if(vergleich == exitclient || vergleich == exitserver)
    {
        printf("******************************\nClient ende\n");
        system("PAUSE");
        return 0;
    }

    rc=recvfrom(s,buf,256,0,(SOCKADDR*)&remoteAddr,&remoteAddrLen);
    if(rc==SOCKET_ERROR)
    {
      printf("Fehler: recvfrom, Fehler code: %d\n",WSAGetLastError());
      return 1;
    }
    else
    {
      buf[rc]='\0';
      printf("%s\n",buf);
    }
  }
  closesocket(s);
  WSACleanup();       
  return 0;
}

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



Chat 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
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
95
96
97
98
99
#pragma comment (lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

//Prototypen
int startWinsock(void);
int main()
{
  long rc;
  SOCKET s;
  char buf[256];
  char buf2[300];
  SOCKADDR_IN addr;
  SOCKADDR_IN remoteAddr;
  int remoteAddrLen=sizeof(SOCKADDR_IN);
  std::string exitserver = "shutdown";
  char vergleich[256];

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

  //UDP Socket erstellen
  s=socket(AF_INET,SOCK_DGRAM,0);
  if(s==INVALID_SOCKET)
  {
    printf("Fehler: Der Socket konnte nicht erstellt werden, Fehler code: %d\n",WSAGetLastError());
    return 1;
  }
  else
  {
    printf("UDP Socket erstellt!\n");
  }

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

  rc=bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
  if(rc==SOCKET_ERROR)
  {
    printf("Fehler: bind, Fehler code: %d\n",WSAGetLastError());
    return 1;
  }
  else
  {
    printf("Socket an Port 1234 gebunden\n---------------------------------\n");
  }

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

    strcpy (vergleich, buf);

    if(vergleich == exitserver)
    {
        printf("******************************\nServer Ende\n");
        return 0;
    }

    //Antworten
    sprintf(buf2,"--> %s",buf);
    rc=sendto(s,buf2,strlen(buf2),0,(SOCKADDR*)&remoteAddr,remoteAddrLen);
    if(rc==SOCKET_ERROR)
    {
      printf("Fehler: sendto, Fehler code: %d\n",WSAGetLastError());
      return 1;
    }
  }
  closesocket(s);
  WSACleanup();       
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&wsa);
}
The server doesn't maintain a list of clients. The client addresses arrive in remoteAddr when the client sends a message in. These need to be collected, and when a message arrives, sendto is used on each entry to echo the message out.

The client doesn't appear to send anything. I'm not sure what you mean by it's working.

The client starts of with sento on an uninitialised buffer. Then it drops into revcfrom, which blocks until something arrives.

Also, when you send a message, you don't send the null terminator, not do you read the message into a zeroed buffer where the null can be applied for you. When you send a message, you send it without the null terminator. You appear to simply ignore message termination.

Also, your socket should be intialised as socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP). You get away with it because PF_INET has the same value as AF_INET and BSD sockets sets IPPROTO_UDP if it's zero and SOCK_DGRAM is specified. But it's wrong.
Topic archived. No new replies allowed.