943,154 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1516
  • C++ RSS
Mar 21st, 2010
0

LAN Chat Client (UDP)

Expand Post »
Hey all,

Trying to get a simple LAN based chat program going (required to use UDP and multicasting). I've tried both client/server and p2p implementations, yet I still can't seem to get it working.

The best I've gotten with client/server is that clients on pc1 and pc2 will connect to the server and can send data to the server and receive data back. However I couldn't get the server to send the data to all clients.

With p2p, I can get each client to send a message, which is then received by itself, not the other clients however.

Here is my current implementation of p2p (removed error checking for readability). Any help would be greatly appreciated.

C++ Syntax (Toggle Plain Text)
  1. char *dest = "224.1.2.3";
  2. unsigned short port = 31337;
  3. struct sockaddr_in m_castAddress, m_recAddress;
  4. SOCKET m_sendSocket, m_recSocket;
  5. fd_set m_checkSockets;
  6. struct timeval t;
  7.  
  8. m_castAddress.sin_family = AF_INET;
  9. m_castAddress.sin_port = htons(port);
  10. m_castAddress.sin_addr.s_addr = inet_addr(dest);
  11.  
  12. m_recAddress.sin_family = AF_INET;
  13. m_recAddress.sin_addr.s_addr = INADDR_ANY;
  14. m_recAddress.sin_port = htons(port);
  15.  
  16. m_sendSocket = socket(AF_INET, SOCK_DGRAM, 0);
  17.  
  18. m_recSocket = socket(AF_INET, SOCK_DGRAM, 0);
  19.  
  20. if (bind (m_recSocket, (SOCKADDR*) &m_recAddress, sizeof
  21.  
  22. ip_mreq mreq;
  23. mreq.imr_multiaddr.s_addr = inet_addr(dest);
  24. mreq.imr_interface.s_addr = INADDR_ANY;
  25.  
  26. setsockopt(m_sendSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));
  27. setsockopt(m_recSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));
  28.  
  29. DWORD dwBytesReturned = 0;
  30. BOOL bNewBehavior = FALSE;
  31. // disable an annoying feature of 2000 and XP which causes many problems
  32. WSAIoctl(m_sendSocket, SIO_UDP_CONNRESET, &bNewBehavior, sizeof(bNewBehavior), NULL, 0, &dwBytesReturned, NULL, NULL);
  33.  
  34. int result;
  35. int length = sizeof(m_recAddress);
  36. while(1)
  37. {
  38. //Initialise m_checkSockets to look at only the receiving socket.
  39. m_checkSockets.fd_count = 1;
  40. m_checkSockets.fd_array[0] = m_recSocket;
  41. char buffer[1000];
  42. //Ensure the client does not spend any time waiting for messages to arive.
  43. t.tv_sec = 0;
  44. t.tv_usec = 0;
  45.  
  46. int waiting = select(NULL, &m_checkSockets, NULL, NULL, &t);
  47. //If a message has been received
  48. if(waiting>0)
  49. {
  50. //Process message
  51. result = recvfrom(m_recSocket, buffer, 10000, 0, (SOCKADDR*) &m_recAddress, &length);
  52.  
  53. std::cout << std::endl << "Packet from Address:" << inet_ntoa(m_recAddress.sin_addr) << " m_port:" << m_recAddress.sin_port << " Size:" << result << " Data:" << std::endl << buffer << std::endl;
  54.  
  55. sprintf(buffer,"ACK");
  56. result = sendto(m_recSocket, buffer, (int)strlen(buffer)+1, 0, (SOCKADDR*)&m_recAddress, sizeof(m_recAddress));
  57. }
  58. else
  59. {
  60. //Else, check to see if the user has input a message.
  61. int result;
  62. std::cin.getline(buffer, 1000);
  63. result = sendto(m_sendSocket, (char *)(buffer), sizeof(buffer), 0, (SOCKADDR*)&m_castAddress, sizeof(m_castAddress));
  64.  
  65. int length = sizeof(m_recAddress);
  66.  
  67. if(memcmp(buffer,"QUIT",4) == 0)
  68. {
  69. break;
  70. }
  71. Sleep(1000);
  72. }
  73. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jmatt110 is offline Offline
1 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: stack , queue and linked list
Next Thread in C++ Forum Timeline: need help in this traingle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC