0

Hello everyone,

This is my first program in C and I have ran into weird issue. I am implementing a simple server & client program in C.

From my main() in client I call a send_message() function which I believe binds with server and sends the message to the server side.

The function proto type for send_message is: int send_message(int Port_num, char*input)
where Port_num is for port number to connect to and
input: is a user input.

For a test I tried printing the user input as such send_message(3100,"HELLO"). This prints Hello in the send_message function

BUT, if I instead get a user input and try pass it to the function, I do not get an output.
This is how I am passing the user input

char id;
scanf("%d",&id);
send_message(3100, &id);

I have pasted my complete code here as well. Thank you for any input.

server.c

#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "functions.h"


#define STR_SIZE 4000

int PORT;



void* server_thread_func(void *arg) 
{ 
    const int Size = 64;
    int Array[Size];
     char buf[STR_SIZE];
    unsigned int addr_len;
    char serverport[STR_SIZE];
    char input[10];
    int id =0;
    int rv, val, sockfd, totbytes;  
    struct sockaddr_in their_addr;
    struct addrinfo hints, *servinfo, *p;

    val = 1;
    sprintf(serverport, "%d", PORT);

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;
    if ((rv = getaddrinfo(NULL, serverport, &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return (NULL);
    }

    for (p = servinfo; p != NULL; p = p->ai_next)   
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) 
        {
            perror("server: socket");
            continue;
        }

        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)) == -1) 
        {
            perror("setsockopt");
            exit(1);
        }

        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) 
        {
            close(sockfd);
            perror("server: bind");
            continue;
        }

        break;
    }

    if (p == NULL) 
    {
        fprintf(stderr, "server: failed to bind\n");
        return (NULL);
    }

    freeaddrinfo(servinfo);

    printf("server: listening for connections...\n");

    while (1)
    {
        if((totbytes = recvfrom(sockfd, buf, STR_SIZE-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1)
        {
            continue;
        }
        printf("Response from %s\n", inet_ntoa(their_addr.sin_addr));

        printf("Size of message is %d bytes\n", totbytes);

        //buf[totbytes] = '\0';
        if(strcmp(buf,"readlock"))
        {
        /** printf("Enter an index value: \n");
         scanf("%d",&id);
         //sprintf("Index: %d",&id);
        printf("yes \n");
        get_lock(id);**/

        }
        //printf("1 \n");
        printf( buf);
    }
    close(sockfd);
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s port\n", argv[0]);
        return 1;
    }

    PORT = atoi(argv[1]);

    pthread_t server_thread_id;
    pthread_create(&server_thread_id, NULL, server_thread_func, NULL);

    if (pthread_join(server_thread_id, NULL))
    {
        printf("error joining thread");
        abort();
    }

    return 0;
}

client.c

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "functions.h"

#define STR_SIZE 5000


int read_message(int sockfd) 
{
    int totbytes;
    char buf[STR_SIZE];
    unsigned int addr_len;
    struct sockaddr_in their_addr;

    if((totbytes = recvfrom(sockfd, buf, STR_SIZE-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1)
    {
        perror("recvfrom");
        exit(1);
    }

    printf("Response from %s\n", inet_ntoa(their_addr.sin_addr));

    printf("Message is %d bytes\n", totbytes);

    buf[totbytes] = '\0';

    printf("Message: %s\n", buf);

    return (totbytes);
}

int send_message(int c_port, char *buf) 
{   
    int sockfd, totbytes;
    struct sockaddr_in their_addr;

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        perror("server: socket");
        exit(1);
    }
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(c_port);
    their_addr.sin_addr.s_addr = INADDR_ANY;

    memset(&(their_addr.sin_zero), '\0', 8);

    if((totbytes = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
    {
        perror("sendto");
        exit(1);
    }

    printf("SendMessage: %s",buf);
    printf("sent %d bytes to %s\n", totbytes, inet_ntoa(their_addr.sin_addr));

    close(sockfd);

    return (totbytes);
}

int main()
{
  char id = 0;
  printf("Enter the client id between 1 and 64 \n");
  scanf("%d",&id);
  //char *n =
 send_message(3100, &id);

 return 0;
}

Please note that this is not completely my own code. I have been testing codes from online tutorials.

Edited by deceptikon: Fixed code tags

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by WaltP
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.