Unix Programming - how to build a chat server in c

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > how to build a chat server in c





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to build a chat server in c
c_eagle@rediffmail.com

2006-03-12, 5:51 pm

hi ...i am building a chat server in c using sockets.i have used fork
to do
non-blocking read and write between the client and the server.but i
dont know how to accept multiple clients and relay messages between two

clients through the server.should i use select()? i have given below
the
client program.the server program is almost same except the usual
bind() , listen() calls.if someone has got such a chat program,then
please send the sourcecode.

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


#define PORT 3468 // the port client will be connecting to
#define MAXDATASIZE 256 // max number of bytes we can get at once


int main(int argc, char *argv[])
{
int sockfd, i,byt=0,pid;
char buf[MAXDATASIZE];
//struct hostent *he;
struct sockaddr_in their_addr; // connector's address
information
/* if (argc != 3)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}*/
/* if ((he=gethostbyname(argv[1])) == NULL)
{ // get the host info
herror("gethostbyname");
exit(1);
}*/
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
memset( (char*) &their_addr, 0, sizeof(their_addr) ); // zero
the rest
of the struct
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order

if(inet_pton(AF_INET,argv[1],&their_addr.sin_addr)<=0)
perror("Error in argument");
// their_addr.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(sockfd, (struct sockaddr
*)&their_addr,sizeof(struct
sockaddr)) == -1)
{
perror("connect");
exit(1);
}
pid=fork();
if(pid==0)
{
while(1)
{
fgets(buf,255,stdin);
write(sockfd,buf,strlen(buf));
}
}
else if(pid>0)
{
while(1)
{
byt=read(sockfd,buf,255);
if(byt<=0)break;
printf("%s",buf);
}
}


close(sockfd);
return 0;



}


bye.June

David Schwartz

2006-03-13, 2:48 am


<c_eagle@rediffmail.com> wrote in message
news:1142180182.516920.321710@i39g2000cwa.googlegroups.com...

> hi ...i am building a chat server in c using sockets.i have used fork
> to do
> non-blocking read and write between the client and the server.but i
> dont know how to accept multiple clients and relay messages between two
>
> clients through the server.should i use select()? i have given below
> the
> client program.the server program is almost same except the usual
> bind() , listen() calls.if someone has got such a chat program,then
> please send the sourcecode.


You are trying to build a bridge when you don't even know what a span
should look like. You really need to take several dozen steps backward.

DS


c_eagle@rediffmail.com

2006-03-13, 2:48 am

dav...will u be a little specific?....am i missing something?......this
code is working fine....so,why should i go backward?...i am asking for
suggestions for developing it further...june

David Schwartz

2006-03-13, 5:54 pm


<c_eagle@rediffmail.com> wrote in message
news:1142231259.533281.65780@p10g2000cwp.googlegroups.com...

> dav...will u be a little specific?....am i missing something?......this
> code is working fine....so,why should i go backward?...i am asking for
> suggestions for developing it further...june


The code you have is way too linear and disorganized to serve as a base
for a chat server. Take a look at the source code to 'ircd', for example. Do
you have a specification for the protocol you are using? (As RFC1459 is for
IRC.)

DS


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com