how to build a chat server in c
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how to build a chat server in c




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-12-06 10: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






[ Post a follow-up to this message ]



    Re: how to build a chat server in c  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-13-06 07: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







[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-13-06 07: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






[ Post a follow-up to this message ]



    Re: how to build a chat server in c  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-13-06 10: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







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:40 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register