- ahmad alhayek

  only need an answer of branch 4 only

NOS-LAB2: Process Communication Using Multithreading 1. Write a simple UDP program to send a simple packet to another server,

Show transcribed image text

Expert Answerinformation icon

  • Swapna's Avatar

    // Client side implementation of UDP client-server model
    #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>
      
    #define PORT 8080
    #define MAXLINE 1024
      
    // Driver code
    int main() {
    int sockfd;
    char buffer[MAXLINE];
    char *hello = "Hello from client";
    struct sockaddr_in servaddr;
      
    // 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));
      
    // Filling server information
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = INADDR_ANY;
      
    int n, len;
      
    sendto(sockfd, (const char *)hello, strlen(hello),
    MSG_CONFIRM, (const struct sockaddr *) &servaddr,
    sizeof(servaddr));
    printf("Hello message sent.\n");
      
    n = recvfrom(sockfd, (char *)buffer, MAXLINE,
    MSG_WAITALL, (struct sockaddr *) &servaddr,
    &len);
    buffer[n] = '\0';
    printf("Server : %s\n", buffer);
      
    close(sockfd);
    return 0;
    }
    Output :
    $ ./server
    Client : Hello from client
    Hello message sent.
    $ ./client
    Hello message sent.
    Server : Hello from server

    Comment 

ليست هناك تعليقات:

إرسال تعليق

ahmad alhayek تصميم ahmad alhayek جميع الحقوق محفوظة 2016

صور المظاهر بواسطة sndr. يتم التشغيل بواسطة Blogger.