Hi there people
I am trying to develop a chat server kind of a thing with a server and multiple
clients, so that if the client sends a message to the server the server displays it.
Now the thing is that even though this may sound very simple Im a newbie at Linux and
hence am stuck.
The program is giving unexpected results, I am enclosing the code, which is very small
about 200 lines 130 for server and the rest for the client, the program uses sockets
for communication.
Please do try and help and bear with the coding technique cause it is one of a newbie
Thanks in advance
Aditya
Client
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<stdlib.h>
# define PORT 5556
# define MAX 512
# define SERVERHOST "127.0.0.1"
void write_to_server(int fileid)
{
int nbytes;
char buffer[MAX];
fprintf(stdout,"\nEnter the message");
fscanf(stdin,"%s",buffer);
nbytes=write(fileid,buffer,MAX);
if(nbytes<0) {
perror("Error in write");
exit(EXIT_FAILURE);
}
}
int main(void)
{
int sock;
struct sockaddr_in serv_addr;
char choice;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0) {
perror("Error in write");
exit(EXIT_FAILURE);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(SERVERHOST);
serv_addr.sin_port = htons(PORT);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("client: error in creating socket\n");
exit(EXIT_FAILURE);
}
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("error in connecting to server\n");
exit(EXIT_FAILURE);
}
do{
write_to_server(sock);
fprintf(stdout,"\n\tAnmother message?\t");
fscanf(stdin,"%c",&choice);
} while(choice!='n');
close(sock);
exit(EXIT_SUCCESS);
}
/*Client ends here*/
/*Server starts here */
/*This is the server code*/
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<stdlib.h>
#include<sys/time.h>
#include<fcntl.h>
# define PORT 5556
# define MAX 512
void init_flags(int []);
int read_from_client(int fileid)
{
char buffer[MAX];
int nbytes;
/* fprintf(stdout,"reading from client");*/
nbytes=read(fileid,buffer,MAX);
if(nbytes<0) {
perror("Read error");
exit(EXIT_FAILURE);
}
else if(nbytes==0)
return -1;
else {
fprintf(stdout,"Server got message:%s",buffer);
return 1;
}
}
void init_flags(int flags[])
{
int i;
for(i=0;i<MAX;i++)
flags[i]=0;
}
int main(void)
{
int sock;
int u;
fd_set active_fd_set,read_fd_set,write_fd_set;
int i;
struct sockaddr_in clientname,servername;
size_t size;
int flags[MAX];
struct timeval time;
time.tv_sec =0;
time.tv_usec=0; /*Time set*/
if((sock=socket(AF_INET,SOCK_STREAM,0))<0) {
perror("unable ot make socket :server");
exit(EXIT_FAILURE);
}
bzero((char *)&servername,sizeof(servername));
servername.sin_family=AF_INET;
servername.sin_addr.s_addr=htonl(INADDR_ANY);
servername.sin_port=htons(PORT);
if(bind(sock,(struct sockaddr *)&servername,sizeof(servername))<0) {
perror("error in binding server");
exit(EXIT_FAILURE);
}
if(listen(sock,5)<0) {
perror("Error in listening");
exit(EXIT_FAILURE);
}
FD_ZERO(&active_fd_set);
FD_SET(sock,&active_fd_set);
/* init_flags(flags);*/
while(1) {
read_fd_set=active_fd_set;
if(select(FD_SETSIZE,&read_fd_set,NULL,NULL,&time)<0) {
perror("acceError in select");
exit(EXIT_FAILURE);
}
for(i=0;i<FD_SETSIZE;++i) {
if(FD_ISSET(i,&read_fd_set)) {
if(i==sock /*&&flags[i]==0*/) {
int newsock;
fprintf(stdout,"%d ==%d",sock,i);
size=sizeof(clientname);
fprintf(stdout,"In .....twetwet.");
newsock=accept(sock,(struct sockaddr *)&clientname,&size);
/*flags[i]=1;*/
printf("Adding new client....");
if(newsock<0) {
perror("Error in accept");
exit(EXIT_FAILURE);
}
FD_SET(newsock,&active_fd_set);
}
else {
if(read_from_client(i)) {
/* close(i);*/
FD_CLR(i,&active_fd_set);
fprintf(stdout,"Aditya");
}
}
}
}
}
}