Can you please give me hints or can you teach me how can i convert my simple client server program(in which only client can send any number of messages it want to server) to chat server client program ? I am doing this by trivial way, but still only server is receving but not client whichever thing server is sending. I have done all these things with the help of socket programming and in C language. thanks if you can help me.

Edited 4 Years Ago by nitin1

What does your design look like at the moment? (How are you handling multiple clients on the server? Threads, I/O multiplexing? You could apply the same solution on the client side to detect incoming messages and send things at "the same time" (not really ofcourse).

Edited 4 Years Ago by Gonbe

server .c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<string.h>

int main()
{
    int sockid,newsockid;
    int processid, client_add;
    char s[10];
    struct sockaddr_in myaddr,clientaddr;    


    sockid=socket(AF_INET,SOCK_STREAM,0);
    if(sockid<0)
    perror("socket");

    memset(&myaddr,0,sizeof myaddr);
    myaddr.sin_port=htons(8765);
    myaddr.sin_family=AF_INET;
    myaddr.sin_addr.s_addr=htonl(INADDR_ANY);

    printf("socket id is:%d\n",sockid);

    if((bind(sockid,(struct sockaddr*)&myaddr,sizeof myaddr))==-1)
    perror("bind");

    if(listen(sockid,10)==-1)
    perror("listen");

    client_add=sizeof clientaddr;
    printf("size of client_add is: %d\n",client_add);

    newsockid=accept(sockid,(struct sockaddr*)&clientaddr,&client_add);
    if(newsockid==-1)
    perror("accept");

     char * buffer=(char*)(malloc(sizeof(char)*1024));
     int buffsize=1024,i=0;
  int l=0;char c;char msg[200];
     while(i<10)
     {
                 recv(newsockid,buffer,buffsize,0);
                   l=0;

                 fprintf(stdout, "Client : %s", buffer);

                  printf("\n");
                 printf("server: ");
                 l=0;
                while((c=getchar())!='\n')
                {
            msg[l++]=c;
                }

                msg[l]='\0'; l++;
                send(newsockid,msg,l,0);
                   i++;            

     }


    printf("server program ended\n");
    close(sockid);
    return 0;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<string.h>

int main()
{

     printf("this is a client program\n");
     struct sockaddr_in myaddr,clientaddr;
     int newsockid;
     int len;
     int sockid,client_add;
     sockid=socket(AF_INET,SOCK_STREAM,0);

     if(sockid==-1)
     perror("socket");

     memset(&myaddr,0,sizeof myaddr);
     myaddr.sin_port=htons(8765);
     myaddr.sin_family=AF_INET;
     myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");

     len=sizeof myaddr;
      if((bind(sockid,(struct sockaddr*)&myaddr,sizeof myaddr))==-1)
    perror("bind");
     int p=connect(sockid,(struct sockaddr*)&myaddr,len);

     if(p==-1)
     perror("connect");

     char msg[200];
     int i=0;
     char c;int l=0; char *buffer=(char *)(malloc(sizeof(char)*200)); int buffsize;
     while(i<10)
     {l=0;
                printf("Client: ");
                while((c=getchar())!='\n')
                {
            msg[l++]=c;
                }

                msg[l]='\0'; l++;
                send(sockid,msg,l,0);

                fflush(stdin);
                newsockid=accept(sockid,(struct sockaddr*)&clientaddr,&client_add);
                recv(newsockid,buffer,buffsize,0);
                   l=0;

                 fprintf(stdout, "server: %s", buffer);

                  printf("\n");
                i++;
     }


     close(sockid);

     return 0;
}

thanks.

This article has been dead for over six months. Start a new discussion instead.