Thread: UDP Socket Programming - Converting C# to C

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    3

    UDP Socket Programming - Converting C# to C

    I tried to convert C# to C

    server.cs:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace Node
    {
        class Program
        {        
            static void Main(string[] args)
            {            
                Console.WriteLine("Launching sink node");
    
    
                UdpClient client = new UdpClient(5000);            
    
    
                IPEndPoint serverEp = new IPEndPoint(IPAddress.Loopback, 33000);
    
    
                while (true)
                {
                    Console.WriteLine("Listening on UDP:5000");
                    byte[] buf = client.Receive(ref serverEp);
                    String request = Encoding.ASCII.GetString(buf).TrimEnd('\r');
    
    
                    Console.WriteLine("Received: " + request);
    
    
                    // messages consist of 4 times 255, length of payload, payload
                    var response = new byte[] { 255, 255, 255, 255, 4, (byte)'Y', (byte)'E', (byte)'S' };
                    client.Send(response, response.Length, serverEp);
                }
    
    
                Console.ReadLine();
            }
        }
    
    }
    server.c:


    Code:
     
        #include <stdio.h>             /* for printf() and fprintf() */                  
        #include <sys/socket.h>     /* for socket() and bind() */
        #include <arpa/inet.h>         /* for sockaddr_in and inet_ntoa() */
        #include <stdlib.h>         /* for atoi() */
        #include <string.h>            /* for memset() */
        #include <unistd.h>           /* for close() */
    
    
        #define  MAXSIZE 255            /* Longest string */
        #define SRC_PORT 5000
        #define DST_PORT 33000
    
    
        void DieWithError(char *errorMessage)    /* External error handling function */
        {
            DieWithError(errorMessage);
            exit(0);
        }
        
        int main(int argc, char *argv[])
        {
            int sock;                                 /* Socket */
            struct sockaddr_in ServAddr;         /* Local address */
            struct sockaddr_in ClntAddr;         /* Client address */
            unsigned int cliAddrLen;                 /* Length of incoming message */
            char recBuffer[MAXSIZE];                 /* Buffer for echo string */
            int recvMsgSize;                         /* Size of received message */ 
            int i;
            
            printf("Launching node \n");
        
            /* Create socket for sending/receiving datagrams */
            if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
                DieWithError("socket() failed");
        
            /* Construct local address structure */
            memset(&ServAddr, 0, sizeof(ServAddr));         /* Zero out structure */
            ServAddr.sin_family = AF_INET;                         /* Internet address family */
            ServAddr.sin_addr.s_addr = htonl(INADDR_ANY);         /* Any incoming interface */
            ServAddr.sin_port = htons(SRC_PORT);             /* Local port */
            
            /* Construct address structure */
            memset(&ClntAddr, 0, sizeof(ClntAddr));         /* Zero out structure */
            ClntAddr.sin_family = AF_INET;                         /* Internet address family */
            ClntAddr.sin_addr.s_addr = inet_addr("CLIENT_IP");         
            ClntAddr.sin_port = htons(DST_PORT);             
        
        
            /* Bind to the local address */
            if (bind(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
                DieWithError("bind() failed");
        
            for (;;) /* Run forever */
            {
                printf("Listening on UDP:5000 \n");
                /* Set the size of the in-out parameter */
                cliAddrLen = sizeof(ClntAddr);
        
                /* Block until receive message from a client */
                if ((recvMsgSize = recvfrom(sock,  recBuffer, MAXSIZE, 0,(struct sockaddr *) &ClntAddr, &cliAddrLen)) < 0)
                    DieWithError("recvfrom() failed") ;
        
                printf("Handling Client: %s\n", inet_ntoa(ClntAddr.sin_addr));
                printf("Received:");
                for(i = 0; recBuffer[i] != '\0'; ++i)
                    printf("%c",recBuffer[i]);
                printf("\n");
                
                /* Send response datagram back to the client */
                
                unsigned char sendBuffer[] = {255, 255, 255, 255, 4, 'Y', 'E', 'S'};
                if (sendto(sock,  sendBuffer, MAXSIZE, 0, (struct sockaddr *) & ClntAddr, sizeof(ClntAddr)) < 0)
                    DieWithError("sendto() failed");
                
            }
            /* NOT REACHED */ 
        }
    UDP Socket Programming - Server listening on Port 5000 - Client listening on Port 33000
    recvfrom() from one Port - 5000

    sendto() to other Port - 33000 to the same client.

    server.c can listen and receives from Client. But Server response is not receiving at Client.

    It works with server.cs code. The server can receive and send to the client.

    client.cs

    Sample code where Client receives from Server
    Code:
       
    byte[] buf = client.Receive(ref nodeEndPoint);     
    String response = Encoding.ASCII.GetString(buf.Skip(5).ToArray());
    
    return response == "YES";


    Please, some suggestions and advice would be great.
    Thank you
    Regards



    Last edited by johnrees200; 11-28-2017 at 11:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2013, 05:22 AM
  2. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  3. Help With Socket Programming
    By namasteall2000 in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-17-2009, 12:58 PM
  4. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  5. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM

Tags for this Thread