Zakozak90
Zakozak90

Reputation: 31

Segmentation fault in a simple UDP server/client

I'm trying to figure out how to do a simple UDP server in C. I will try to give message in parameter of the function for a better version.

But I keep having some basic segmentation fault and I don't know where this is coming from, even tho I tried to debug with "if(error == -1)" statement.

Here's my Server

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#define PORT 3000
#define MAXLINE 1000

int main(int argc, char const *argv[])
{
   char buffer[100]; 
   char *message ="hello world";  
   int udp_socket,len;
   struct sockaddr_in servaddr,cliaddr;
   
   //socket
   udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
   if (udp_socket<0){
       printf("erreur de création de socket");
       exit(-1);
   } 

   //bind 
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(PORT);
   servaddr.sin_addr.s_addr = INADDR_ANY;
   int error;
   error = bind(udp_socket, (struct sockaddr *)&servaddr, sizeof servaddr);
   if(error == -1){
       printf ("cannot get port %d",PORT);
       exit(-1);
   }

   //recvfrom
   len = sizeof(cliaddr);
   int error2 = recvfrom(udp_socket,buffer,sizeof(buffer),0,(struct sockaddr*)&cliaddr,&len);
    if(error2 <0){
       printf ("erreur recvfrom");
       exit(-1);
   }

   message[error2]='\0';
   puts(message);
   return 0;
   int k = sendto(udp_socket,message,MAXLINE,0,(struct sockaddr*)&cliaddr,sizeof(cliaddr));
   if (k>0) {
       printf ("error sendto");
       exit(-1);
   }
} 

And here's my Client

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

#define PORT 3000
#define MAXLINE 1000

int main(){

    char *message = "this is a test\n ";
    int port = 3000;
    char buffer[100];

    int sockfd,n;
    struct sockaddr_in servaddr;
    //clear servaddr

    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    servaddr.sin_port = htons(PORT);
    servaddr.sin_family = AF_INET;
    
    //create datagram socket
    sockfd =socket(AF_INET,SOCK_DGRAM,0);

   // connect to server
    if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
        printf("\n Error : Connect Failed \n");
        exit(0);
    }
    // request to send datagram
    // no need to specify server address in sendto
    // connect stores the peers IP and port
    sendto(sockfd, message, MAXLINE, 0, (struct sockaddr*)NULL, sizeof(servaddr));
    
    // waiting for response
    recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)NULL, NULL);
    puts(buffer);
  
    // close the descriptor
    close(sockfd);
}

Upvotes: 1

Views: 177

Answers (1)

dbush
dbush

Reputation: 223937

You're sending more bytes then you have buffer.

In both your server:

int k = sendto(udp_socket,message,MAXLINE,0,(struct sockaddr*)&cliaddr,sizeof(cliaddr));

And client:

sendto(sockfd, message, MAXLINE, 0, (struct sockaddr*)NULL, sizeof(servaddr));

You're telling them to send MAXLINE bytes, but message doesn't point to a string of that length. This means you're reading past the end of the string, triggering undefined behavior.

In both cases, send strlen(message)+1 bytes. That will send just the characters in the string plus the terminating null byte.

Upvotes: 1

Related Questions