none
I am unable to establish UDP Connection between Server written in C++ and Client written in C# RRS feed

  • Question

  • Sending and Receiving Data using UDP between C++ and C# Applications
    Server console App (C++)

    void main()
    
    {
    

    WSADATA wsaData; SOCKET SendSocket; sockaddr_in RecvAddr; int Port = 27250; //port number char SendBuf[32]="From port 27250"; int BufLen = 32; char* IP_ADDRESS_S="127.0.0.1"; //IP Address WSAStartup(MAKEWORD(2,2), &wsaData); SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS_S); cout<<"Sending a datagram to the receiver..."; sendto(SendSocket, SendBuf, BufLen, 0, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr)); cout<<"Finished sending. Closing socket."; closesocket(SendSocket); cout<<"Exiting."; WSACleanup();

    }

    Client console App (C#)

    A udp client/receiver written in C# using UdpClient class

     namespace UDPServer {
     class Program {
      static void Main(string[] args {
      RecData(27250); }       //Port number
      static void RecData(int Port) {
      UdpClient client = null;
      try {
       client = new UdpClient(Port);
         }
      catch (Exception ex)
         {
       Console.WriteLine(ex.Message);
        }
        // IPAddress addr=IPAddress.Parse("127.0.0.1");
       IPEndPoint RemoteServer = new IPEndPoint(IPAddress.Any, 0); //IP address
       for (; ; )
       {
       try {
      byte[] RecPacket = client.Receive(ref RemoteServer);
      Console.WriteLine("Connected to the client {0}, {1}", RemoteServer, 
       Encoding.ASCII.GetString(RecPacket));
       }
      catch (Exception ex)
       {
     Console.WriteLine(ex.Message);
      }}
      }}
      }


    • Edited by GhazanfarAli Wednesday, January 31, 2018 9:10 AM
    Wednesday, January 31, 2018 9:07 AM

All replies