Sending UDP packets.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kardon33
    New Member
    • May 2007
    • 158

    Sending UDP packets.

    Hello all,

    I am trying to send a few UDP packets and cant figure it fully out.

    I am running under UNIX OpenBSD 4.2

    Heres what I got so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/in_systm.h>
    #include <netinet/ip.h>
    #include <netinet/tcp.h>
    #include <arpa/inet.h>
    
     int main()
    {
    
    int port = 1936;
    char addr[15] = "2.0.0.2";
    
    // I know you intaililize the socket with socket
    sock = socket (AF_INET, SOCK_STREAM, 0);
    
    // This is where i get confused
    struct sockaddr_in addr;
    address.sin_family = AF_INET; //we're using inet
    address.sin_port = htons (port); //set the port
    
    connect (sock, (struct sockaddr *) &address, sizeof(address))
    
    //Create the packet
    sprintf(packet, "La la la la");
    
    //And send it
    write (sock, packet, strlen(packet));
    close (sock); // Close the connection
    
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You will not be able to use this code to send UDP packets because this

    sock = socket (AF_INET, SOCK_STREAM, 0);

    you will need to use SOCK_DGRAM to create a UDP socket.

    Comment

    • kardon33
      New Member
      • May 2007
      • 158

      #3
      Thanks for the help, I got that changed but am still in need of help.

      Please look at lines 19, 31, 35. these are the parts im not sure exactly how to do. Mostly binding to a remote host??

      Code:
      #include <stdio.h>
      
      /* for EXIT_FAILURE and EXIT_SUCCESS */
      #include <stdlib.h>
      
      /* network functions */
      #include <sys/types.h>
      #include <sys/socket.h>
      #include <netinet/in.h>
      
      
      int main()
      {
        int socket_desc;
        struct sockaddr_in address;
        int addrlen;
        int new_socket;
      
        char *addr = "10.0.0.146";
        
      /* create the master socket and check it worked */
        if ((socket_desc=socket(AF_INET,SOCK_DGRAM,0))==0)
        {
      /* if socket failed then display error and exit */
          perror("Create socket");
          exit(EXIT_FAILURE);
        }
      
      /* type of socket created */
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr(addr); // **** This is the part im not sure about ****
      
        address.sin_port = htons(1936);
      
        if (bind(socket_desc,(struct sockaddr *)&address,sizeof(address))<0) // Could someone explain this function and how im using it, I took it from a website and dont fully understand.
        {
      /* if bind failed then display error message and exit */
          perror("bind");
          exit(EXIT_FAILURE);
        }
      
      }

      Comment

      • kardon33
        New Member
        • May 2007
        • 158

        #4
        i got it sending a string that I take from the input :),

        for others,

        Code:
        int main(int argc, char **argv)
        {
          int s;
          int ret;
          char *buf;
          struct sockaddr_in addr;
        
          if (argc != 4) {
            puts("usage: send ipaddr port data");
            exit(1);
          }
        
          addr.sin_family = AF_INET;
          ret = inet_aton(argv[1], &addr.sin_addr);
          if (ret == 0) { perror("inet_aton"); exit(1); }
          addr.sin_port = htons(atoi(argv[2]));
          buf = argv[3];
        
          s = socket(PF_INET, SOCK_DGRAM, 0);
          if (s == -1) { perror("socket"); exit(1); }
        
          ret = sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
          if (ret == -1) { perror("sendto"); exit(1); }
        
          return 0;
        }

        Comment

        Working...