clife 0 Unverified User

Hi,

I am familiar with C and C++, but dont use sockets regularly.I am facing problem in clinet-server program.

my purpose is :

  1. First client sends some info to server.
  2. Server should read info and inturn send some info to client
  3. Client reads info and again sends some info
  4. Client closes connection.
  5. But Server should read what client sent at last and run indefeinitely

My problem is the server is also getting closed after step 4 or may be because of some other problem .
But Server is running indefeinitely if i have only one write from client and only one read from server.

Please help me whats going wrong..

server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef  struct st_host_info{
        char hostName[50];
        char ipAddr[30];
        char macAddr[50];
        char infName[20];
}Host_Info_t;


#define NAME "HostInfo"

int main(int argc, char **argv)
{
    int sockid, msgsock, rval;
    struct sockaddr_un server, client;
    Host_Info_t buf;

    sockid = socket(PF_UNIX, SOCK_STREAM, 0); 
    if (sockid < 0) {
        perror("opening socket");
        exit(1);
    }   

    server.sun_family = AF_UNIX;
    strcpy(server.sun_path, NAME);

    if (bind(sockid, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) {
        perror("binding stream socket");
        exit(1);
    }   

    printf("Socket has name %s\n", server.sun_path);
    listen(sockid, 10);

    for (;;) {
     socklen_t len = sizeof(client);
        msgsock = accept(sockid, (struct sockaddr *)&client, &len);

        if (msgsock == -1)
            perror("accept");
        else do {
            printf("strlen(sun_path) = %zu\n", strlen(client.sun_path));

            bzero(&buf, sizeof(buf));
            if ((rval = read(msgsock, &buf, sizeof(buf))) < 0)
                perror("reading stream message");
            else if (rval == 0)
                printf("Ending connection\n");
            else {
                printf("-%s  ", buf.ipAddr);
                printf("-->%s  ", buf.macAddr);
                printf("-->%s  ", buf.infName);
                printf("-->%s  \n", buf.hostName);
        }
        printf("Rval Read  = %d\n", rval);

        char buff[30];
        strcpy(buff,"This is From Server");

        if((rval = write(msgsock,buff,sizeof (buff)))<0)
                perror("writting stream message");
        else if(rval == 0)
                printf("Ending connection\n");
        else
                printf("writting success\n");

        printf("Rval Write  = %d\n", rval);
        memset(buff,0,sizeof(buff));
        if ((rval = read(msgsock, buff, sizeof(buf))) < 0)
                perror(" read :");
        else
                printf(" from client: %s\n", buff);

        printf("Rval Read2  = %d\n", rval);
       } while (rval > 0);

        close(msgsock);
    }
    close(sockid);
    unlink(NAME);

    return 0;
}
client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef  struct st_host_info{
        char hostName[50];
        char ipAddr[30];
        char macAddr[50];
        char infName[20];
}Host_Info_t;

//#define DATA "Half a league, half a league . . ."
#define NAME "HostInfo"

int main(int argc, char **argv)
{
    int sock;
    struct sockaddr_un server;
    Host_Info_t host_info={"VM", "10.10.10.51","08:00:27:E8:DE:EC","eth0"};
    sock = socket(PF_UNIX, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("opening stream socket");
        exit(1);
    }

    server.sun_family = AF_UNIX;
    strcpy(server.sun_path, NAME);

    if (connect(sock, (struct sockaddr *) &server,
                sizeof(struct sockaddr_un)) < 0) {
        close(sock);
        perror("connecting stream socket");
        exit(1);
    }

    if (write(sock, &host_info, sizeof(host_info)) < 0)
        perror("writing on stream socket");
        char buff[30];

    if (read(sock, buff, sizeof(buff)) < 0)
        perror("writing on stream socket");
    else {
                printf(" buff  %s\n", buff);
        }
    strcpy(buff,"client received msg");
    if (write(sock, buff, sizeof(buff)) < 0)
        perror("writing on stream socket");

    close(sock);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.