473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to build a chat server in c

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

Mar 11 '06 #1
1 6984
c_*****@rediffmail.com 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) ks***@mib.org <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.
Mar 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jaspreet | last post by:
Hi All, I want to make a Chat Server and Client program in C++ using Turbo/Borland C++ on a windows/Dos platform. if you have any info, suggestions or resources for Network programming in...
2
by: Paul | last post by:
Hello, I nead to create chat server but i cannot find any sample can you show me how i can craete multirhread chat server. ?? Regards PM
6
by: the-programmer | last post by:
I need to know if i want to build a server what shall i do and what devices should i buy I need to know everything
4
by: Ronald Green | last post by:
Hi, I have this theoretical problem: Say that I need a chat server that also has a database where it saves all conversations. However, I need several servers for availability and load balancing....
1
by: arjunjag | last post by:
Hi All i am a developing a chat server using C in Linux.Now i am just a beginner in Networking so i need help from someone in the project so please help me and reply me fast thanking you all...
1
by: Pradeep Chhabria | last post by:
I have made a UDP multi client chat server.Now i wanna see that how a bit error or any packet loss can affect the chatting.....So what should i do?What methods should i use?
1
by: Viji nellaiappan | last post by:
How to implement chat server in PHP for users in a local area network ? Please give me basic ideas to start this task..I am using XAMPP software in WindowsXP machine.. Thanks
7
by: shathaaaa | last post by:
hi every body here i try to run program written in java (client server chat Application ) but when i try to compile it i get this error " RMIChatServerImpl.java.java:6: calss,interface,or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.