Thread: Issue with TCP socket communication in C program.

  1. #1
    Registered User
    Join Date
    Oct 2023
    Posts
    1

    Issue with TCP socket communication in C program.

    Good afternoon. I have written a basic C program to exchange text between sockets. However, I have a issue where the program hangs at the point where I send a message from the client socket. I ran the program in GDB and saw no errors, the program just hung.
    My code is included below.

    Code:
    #include <stdio.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdbool.h>
    #define MAX 80
    #define PORT 3636
    #define SA struct sockaddr
    
    
    void server_func(int connfd)
    {
        char buff[MAX];
        int n;
        memset(buff, 0, MAX);
        read(connfd, buff, sizeof(buff));
        printf("From client: %s\t To client : ", buff);
        const char *response = "Response from server";
        write(connfd, response, strlen(response) + 1);
    }
    
    
    void client_func(int sockfd)
    {
        char buff[MAX];
        int n;
        memset(buff, 0, MAX);
        printf("Enter the string : ");
        fgets(buff, MAX, stdin);
        write(sockfd, buff, strlen(buff) + 1);
        memset(buff, 0, MAX);
        read(sockfd, buff, sizeof(buff));
        printf("From Server : %s \n", buff);
    }
    
    
    int client()
    {
        int sock_holder;
        int confd;
        // printf("Client called\n");
        printf("Hello  Client");
        struct sockaddr_in svraddcli, cliadd;
        sock_holder = socket(AF_INET, SOCK_STREAM, 0);
        if (sock_holder == -1)
        {
            perror("Client Socket creation failed\n");
        }
        else
            printf("Client Socket creation worked\n");
        memset(&svraddcli, 0, sizeof(svraddcli));
        svraddcli.sin_family = AF_INET;
        svraddcli.sin_addr.s_addr = inet_addr("127.0.0.1");
        svraddcli.sin_port = htons(PORT);
    
    
        if (connect(sock_holder, (SA *)&svraddcli, sizeof(svraddcli)) != 0)
        {
    
    
            printf("Connection with server failed \n");
        }
        else
            printf("Connected to server\n");
    
    
        client_func(confd);
        // close(sock_holder);
        return 0;
    }
    
    
    int main()
    {
        int sock_holder;
        int confd;
        int len;
        struct sockaddr_in svradd, cliadd;
        sock_holder = socket(AF_INET, SOCK_STREAM, 0);
    
    
        printf("Hello  Server");
        if (sock_holder == -1)
        {
            printf("Socket creation failed\n");
        }
        else
            printf("Socket creation worked\n");
    
    
        memset(&svradd, 0, sizeof(svradd));
        svradd.sin_family = AF_INET;
        svradd.sin_addr.s_addr = htonl(INADDR_ANY);
        svradd.sin_port = htons(PORT);
    
    
        if ((bind(sock_holder, (SA *)&svradd, sizeof(svradd))) != 0)
        {
    
    
            printf("Socket bind failed \n");
            exit(0);
        }
        else
            printf("Socket binding worked \n");
    
    
        if ((listen(sock_holder, 5)) != 0)
        {
    
    
            printf("Listen failed\n");
        }
        else
            printf("Server listening \n");
    
    
        client();
        len = sizeof(cliadd);
        confd = accept(sock_holder, (SA *)&cliadd, &len);
        if (confd < 0)
        {
            printf("server accept failed\n");
            exit(0);
        }
        else
            printf("server has accepted client\n");
        client_func(confd);
        server_func(confd);
    
    
        // close(sock_holder);
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    No matter what compiler and IDE you are using, please turn on the warnings and turn up to the highest level!
    Code:
    bar.c: In function ‘server_func’:
    bar.c:19:9: warning: unused variable ‘n’ [-Wunused-variable]
       19 |     int n;
          |         ^
    bar.c: In function ‘client_func’:
    bar.c:31:9: warning: unused variable ‘n’ [-Wunused-variable]
       31 |     int n;
          |         ^
    bar.c: In function ‘client’:
    bar.c:48:35: warning: unused variable ‘cliadd’ [-Wunused-variable]
       48 |     struct sockaddr_in svraddcli, cliadd;
          |                                   ^~~~~~
    bar.c: In function ‘main’:
    bar.c:125:48: warning: pointer targets in passing argument 3 of ‘accept’ differ in signedness [-Wpointer-sign]
      125 |     confd = accept(sock_holder, (SA *)&cliadd, &len);
          |                                                ^~~~
          |                                                |
          |                                                int *
    In file included from /usr/include/netinet/in.h:23,
                     from /usr/include/arpa/inet.h:22,
                     from bar.c:2:
    /usr/include/x86_64-linux-gnu/sys/socket.h:307:42: note: expected ‘socklen_t * restrict’ {aka ‘unsigned int * restrict’} but argument is of type ‘int *’
      307 |                    socklen_t *__restrict __addr_len);
          |                    ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
    bar.c: In function ‘client’:
    bar.c:72:5: warning: ‘confd’ is used uninitialized [-Wuninitialized]
       72 |     client_func(confd);
          |     ^~~~~~~~~~~~~~~~~~
    bar.c:45:9: note: ‘confd’ was declared here
       45 |     int confd;
          |         ^~~~~
    I don't do socket programming, so I defer other suggestions to others.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Normally the client and the server would need to be separate processes, or at least separate threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Pro tip..
    Code:
    printf("Socket creation failed\n");
    If the socket failed.. You probably want to exit the program.
    Code:
    fputs("Socket creation failed\n", stderr);
    exit(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw socket communication with web browser
    By Wido in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-07-2019, 12:13 AM
  2. Socket Program Challenging issue, please help
    By ragursr in forum C Programming
    Replies: 5
    Last Post: 02-05-2018, 11:04 PM
  3. raw socket to dgram communication
    By cole701 in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-02-2010, 03:46 PM
  4. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  5. Socket communication hanging
    By zee in forum C Programming
    Replies: 15
    Last Post: 08-05-2004, 11:52 PM

Tags for this Thread