Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct{
- uint8_t ver : 2;
- uint8_t t : 2;
- uint8_t tkl : 4;
- uint8_t code;
- uint16_t id;
- } coap_header;
- typedef struct{
- coap_header hdr;
- coap_buffer tok; //coap_buffer is another struct
- uint8_t numopt;
- coap_option opt[MAXOPT]; /coap_option is another struct
- coap_buffer payload;
- } coap_packet;
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include "coapHead.h"
- #define PORT 8080
- #define MAXLINE 1024
- int main(){
- int sockfd;
- char buffer[MAXLINE];
- char *hello = "Hello from server. Packet Received.";
- struct sockaddr_in servaddr, cliaddr;
- struct coap_packet *temp = malloc(sizeof(coap_packet));
- //Creating socket file descriptor
- if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
- perror("Socket creation failed");
- exit(EXIT_FAILURE);
- }
- memset(&servaddr, 0, sizeof(servaddr));
- memset(&cliaddr, 0, sizeof(cliaddr));
- //Filling information
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = INADDR_ANY;
- servaddr.sin_port = htons(PORT);
- //Binding socket with server address
- if(bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
- perror("Binding Failed!");
- exit(EXIT_FAILURE);
- }
- int i=0;
- while(i<6){
- int n, len;
- n = recvfrom(sockfd, temp, sizeof(temp), MSG_WAITALL, (struct sockaddr *)&cliaddr, &len);
- // buffer[n] = '