Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to build a chat server in c

Reply
Thread Tools

how to build a chat server in c

 
 
c_eagle@rediffmail.com
Guest
Posts: n/a
 
      03-11-2006
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
hosts 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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> 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
> hosts through the server.

[snip]

Standard C doesn't support sockets or fork(). Try a newsgroup
specific to your system, perhaps comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Could I build a video chat site with ruby on rails? Tony Mr Ruby 1 08-01-2010 04:55 PM
Build a simple Textual Chat with Java - 2 Clients and 1 Server: Help Me !! Larion Java 1 03-26-2006 06:48 PM
Chat Server and Chat Client for INTRANET diptanu@gmail.com Java 0 01-30-2006 06:56 PM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM



Advertisments