Question

In: Computer Science

I am trying to write a UDP socket program in which there is a server program...

I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong.

This is the input and expected output:

> gcc udpclient.c –o clientout
> gcc udpserver.c –o serverout
> ./serverout 52007 ‘to celebrate’ &
> ./clientout odin 52007 ‘Today is a good day’
Today is a good day to celebrate

//UDP echo client program
#include
#include
#include
#include
#include
#include
#include
#include

#define PORT 52010

int main(int argc, char* argv[])                           //Three arguments to be checked later
{
   //Declare and define variables
   int s;                                       //Socket descriptor
   int len;                                    //Length of string to be echoed
   char* servName;                                //Server name
   int servPort;                                   //Server Port
   char* string;                                   //String to be echoed
   char buffer[256+1];                               //Data buffer
   struct sockaddr_in servAddr;                           //Server socket address
   //Check and set program arguments
   if(argc != 3)
   {
       printf("Error: three arguments are needed!");
       exit(1);
   }
   servName = argv[1];
   servPort = atoi(argv[2]);
   string = argv[3];
   //Build server socket address
   memset(&servAddr, 0, sizeof(servAddr));
   servAddr.sin_family = AF_INET;
   inet_pton(AF_INET, servName, &servAddr.sin_addr);
   servAddr.sin_port = htons(PORT);
   //Create socket
   if ((s = socket(PF_INET, SOCK_DGRAM, 0) < 0));
   {
       perror("Error: Socket failed!");
       exit(1);
   }
   //Send echo string
   len = sendto(s, string, strlen(string), 0, (struct sockaddr*)&servAddr, sizeof(servAddr));
   //Receive echo string
   recvfrom(s, buffer, len, 0, NULL, NULL);
   //Print and verify echoed string
   buffer[len] = '\0';
   printf("Echo string received: ");
   fputs(buffer, stdout);
   //Close the socket
   close(s);
   //Stop the program
   exit(0);
} //End of echo client program

//UDP echo server program
#include
#include
#include
#include
#include
#include
#include
#include

#define PORT 52010

int main(void)
{
   //Declare and define variables
   int s;                                //Socket descriptor(reference)
   int len;                            //Length of string to be echoed
   char buffer[256];                       //Data buffer
   struct sockaddr_in servAddr;                   //Server (local) socket address
   struct sockaddr_in clntAddr;                   //Client (remote) socket address
   int clntAddrLen;                       //Length of client socket address
   //Build local (server) socket address
   memset(&servAddr, 0, sizeof(servAddr));               //Set socket structure to all 0
   servAddr.sin_family = AF_INET;                   //Family field
   servAddr.sin_port = htons(PORT);               //Default port number
   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);           //Default IP address
   //Create socket
   if((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
   {
       perror("Error: socket failed!");
       exit(1);
   }
   //Bind socket to local address and port
   if((bind(s, (struct sockaddr*)&servAddr, sizeof(servAddr))) < 0);
   {
       perror("Error: bind failed!");
       exit(1);
   }
   for(;;)           //Run forever
   {
       //Receive String
       len = recvfrom(s, buffer, sizeof(buffer), 0,
               (struct sockaddr*)&clntAddr, &clntAddrLen);
       //Send String
       sendto(s, buffer, len, 0, (struct sockaddr*)&clntAddr, sizeof(clntAddr));
   } //End of for loop
}//End of echo server program

Solutions

Expert Solution

// Server 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 *celebrate = "celebrate from server";

    struct sockaddr_in servaddr, cliaddr;

      

    // 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 server information

    servaddr.sin_family    = AF_INET; // IPv4

    servaddr.sin_addr.s_addr = INADDR_ANY;

    servaddr.sin_port = htons(PORT);

      

    // Bind the socket with the server address

    if ( bind(sockfd, (const struct sockaddr *)&servaddr,

            sizeof(servaddr)) < 0 )

    {

        perror("bind failed");

        exit(EXIT_FAILURE);

    }

      

    int len, n;

  

    len = sizeof(cliaddr); //len is value/resuslt

  

    n = recvfrom(sockfd, (char *)buffer, MAXLINE,

                MSG_WAITALL, ( struct sockaddr *) &cliaddr,

                &len);

    buffer[n] = '\0';

    printf("Client : %s\n", buffer);

    sendto(sockfd, (const char *)celebrate, strlen(celebrate),

        MSG_CONFIRM, (const struct sockaddr *) &cliaddr,

            len);

    printf("celebrate message sent.\n");

      

    return 0;

}


Related Solutions

Socket-based Java server program
Write a Socket-based Java server program that responds to client messages as follows: When it receives a message from a client, it simply converts all the letters into ASCII characters and sends back them to the client. Write both client and server programs demonstrating this.
I am needing to initialize a server socket and wait for incoming connections in C programming....
I am needing to initialize a server socket and wait for incoming connections in C programming. I am also required to create a thread to handle my client and able to pass messages between them asynchronously. I would ideally like to start my server with pthreads to allow for multiple connections. I have 2 functions started: int serverRun(){ this will be the meat of the server code with sockets } int serverStart() { This is where I want to initiate...
Write a Java program that establishes a TCP connection with a mail server through the socket...
Write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. Your program sends SMTP commands to local mail server, receives and processes SMTP commands from local mail server. It sends email message to one recipient at a time. The email message contains a typical message header and a message body. Here is a skeleton of the code you'll need to write: import java.io.*; import java.net.*; public class EmailSender...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
I am trying to write a program in Java: Pick 4 cards and display the cards...
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly. import java.util.Scanner; import...
I am trying to write code for a program in Visual Studo using Visual Basic programming...
I am trying to write code for a program in Visual Studo using Visual Basic programming language that computes the factorial of an entered number by using a For Loop. My problem is that it keeps re-setting the variable for Factorial. Below is my code if anyone can help. I want it to multiply the given number by that number - 1, then continuing to do so until it hits zero without multiplying by zero. Private Sub BtnCalculate_Click(sender As Object,...
I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
4. How does a client socket interact with a server socket to exchange data?
4. How does a client socket interact with a server socket to exchange data?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT