Chat server

Started by
1 comment, last by Kylotan 15 years, 8 months ago
Hi, I'm trying to create a simple chat server with sockets on a Linux. I've done most of it but there are still a few, annoying problems: 1:If a client connects, the second client can't connect until the first client has said something. 2:A client can't say two things directly after eachother. If a client said three things after eachother the first thing get said normal. The last two only get read by the server when another client said something and the sentences get pasted to eachother. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#include <fcntl.h>

using namespace std;

int CreateSocket()
{
 int returnsocket=socket(AF_INET,SOCK_STREAM,0);
 if (returnsocket==-1)
  perror("Create socket");
 return returnsocket;
}

void BindSocket(int *getsocket,int port)
{
  struct sockaddr_in address;
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(port);
  bind(*getsocket,(struct sockaddr *)&address,sizeof(address));

}

int AcceptConnection(int mastersocket,int port)
{
 int addrlen,new_socket;
 struct sockaddr_in address;
 address.sin_family = AF_INET;
 address.sin_addr.s_addr = INADDR_ANY;
 address.sin_port = htons(port);
 addrlen = sizeof(struct sockaddr_in);
 new_socket = accept(mastersocket, (struct sockaddr *)&address,(socklen_t*) &addrlen);
 if (new_socket<0)
  perror("Accept connection");
 return new_socket;
}

void SendMessage(int socket,char *message)
{
 send(socket,message,strlen(message),0);
}

void AllowMultipleConnections(int master_socket)
{
 int opt=1;

 if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))<0)
 {
  perror("setsockopt");
  exit(EXIT_FAILURE);
 }
 fcntl(master_socket, F_SETFL, O_NONBLOCK);
}

int main()
{
 int MasterSocket=CreateSocket();
 int ClientSocket[3]={0,0,0},Activity,NewSocket;
 fd_set readfds;
 AllowMultipleConnections(MasterSocket);
 BindSocket(&MasterSocket,7001);
 listen(MasterSocket,3);
 while(true)
 {
  FD_ZERO(&readfds);
  FD_SET(MasterSocket,&readfds);
  for(int a=0;a<3;a++)
  {
	if(ClientSocket[a]>0)
	 FD_SET(ClientSocket[a],&readfds);
  }
  Activity=select(6, &readfds, NULL, NULL, NULL);
  if(FD_ISSET(MasterSocket,&readfds))
  {
   NewSocket=AcceptConnection(MasterSocket,7001);
   for(int a=0;a<3;a++)
   {
	if(ClientSocket[a]==0)
	{
	 ClientSocket[a]=NewSocket;
	 char message[100];
	 sprintf(message,"Welcome to the socket server, user %i\n",a);
	 SendMessage(NewSocket,message);
	 cout<<"New user joined at:"<<a<<endl;
	// close(NewSocket);
	 break;
	}
   }
  }
  for(int a=0;a<3;a++)
  {
   if(ClientSocket[a]!=0)
   {
	int bufsize=1024;
	char *buffer=(char *)malloc(bufsize);
	int ret=recv(ClientSocket[a],buffer,bufsize,0);
	if(ret==0)
	{
	 cout<<"User "<<a<<" left."<<endl;
	 close(ClientSocket[a]);
	 ClientSocket[a]=0;
	}
	else if (ret == -1)
	{
	 perror("recv");
	}
	else
	{
	 cout<<"ReceiHi,

I'm trying to create a simple chat server with sockets on a Linux. I've done most of it but there are still a few, annoying problems:

1:If a client connects, the second client can't connect until the first client has said something.

2:A client can't say two things directly after eachother. If a client said three things after eachother the first thing get said normal. The last two only get read by the server when another client said something and the sentences get pasted to eachother.

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#include <fcntl.h>

using namespace std;

int CreateSocket()
{
 int returnsocket=socket(AF_INET,SOCK_STREAM,0);
 if (returnsocket==-1)
  perror("Create socket");
 return returnsocket;
}

void BindSocket(int *getsocket,int port)
{
  struct sockaddr_in address;
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(port);
  bind(*getsocket,(struct sockaddr *)&address,sizeof(address));

}

int AcceptConnection(int mastersocket,int port)
{
 int addrlen,new_socket;
 struct sockaddr_in address;
 address.sin_family = AF_INET;
 address.sin_addr.s_addr = INADDR_ANY;
 address.sin_port = htons(port);
 addrlen = sizeof(struct sockaddr_in);
 new_socket = accept(mastersocket, (struct sockaddr *)&address,(socklen_t*) &addrlen);
 if (new_socket<0)
  perror("Accept connection");
 return new_socket;
}

void SendMessage(int socket,char *message)
{
 send(socket,message,strlen(message),0);
}

void AllowMultipleConnections(int master_socket)
{
 int opt=1;

 if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))<0)
 {
  perror("setsockopt");
  exit(EXIT_FAILURE);
 }
 fcntl(master_socket, F_SETFL, O_NONBLOCK);
}

int main()
{
 int MasterSocket=CreateSocket();
 int ClientSocket[3]={0,0,0},Activity,NewSocket;
 fd_set readfds;
 AllowMultipleConnections(MasterSocket);
 BindSocket(&MasterSocket,7001);
 listen(MasterSocket,3);
 while(true)
 {
  FD_ZERO(&readfds);
  FD_SET(MasterSocket,&readfds);
  for(int a=0;a<3;a++)
  {
	if(ClientSocket[a]>0)
	 FD_SET(ClientSocket[a],&readfds);
  }
  Activity=select(6, &readfds, NULL, NULL, NULL);
  if(FD_ISSET(MasterSocket,&readfds))
  {
   NewSocket=AcceptConnection(MasterSocket,7001);
   for(int a=0;a<3;a++)
   {
	if(ClientSocket[a]==0)
	{
	 ClientSocket[a]=NewSocket;
	 char message[100];
	 sprintf(message,"Welcome to the socket server, user %i\n",a);
	 SendMessage(NewSocket,message);
	 cout<<"New user joined at:"<<a<<endl;
	// close(NewSocket);
	 break;
	}
   }
  }
  for(int a=0;a<3;a++)
  {
   if(ClientSocket[a]!=0)
   {
	int bufsize=1024;
	char *buffer=(char *)malloc(bufsize);
	int ret=recv(ClientSocket[a],buffer,bufsize,0);
	if(ret==0)
	{
	 cout<<"User "<<a<<" left."<<endl;
	 close(ClientSocket[a]);
	 ClientSocket[a]=0;
	}
	else if (ret == -1)
	{
	 perror("recv");
	}
	else
	{
	 cout<<"Received a message!"<<endl;
	 buffer[ret]=0;
	 cout<<buffer<<endl;
	 for(int b=0;b<3;b++)
	 {
	  if(ClientSocket!=0)
	  {
	   if(b!=a)
		SendMessage(ClientSocket,buffer);
	  }
	 }
	}
   }
  }
 }
 for(int a=0;a<3;a++)
 {
  if(ClientSocket[a]!=0)
   close(ClientSocket[a]);
 }
 close(MasterSocket);
 cin.get();
 return 0;
}



Anyone sees where I went wrong?

Greetings

Robinved a message!"<<endl;
	 buffer[ret]=0;
	 cout<<buffer<<endl;
	 for(int b=0;b<3;b++)
	 {
	  if(ClientSocket!=0)
	  {
	   if(b!=a)
		SendMessage(ClientSocket,buffer);
	  }
	 }
	}
   }
  }
 }
 for(int a=0;a<3;a++)
 {
  if(ClientSocket[a]!=0)
   close(ClientSocket[a]);
 }
 close(MasterSocket);
 cin.get();
 return 0;
}


Anyone sees where I went wrong? Greetings Robin
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
Advertisement
The Forum FAQ talks about why TCP is a stream, not packets, and what to do about it.
enum Bool { True, False, FileNotFound };
The first parameter to select is not something you can typically hard-code like that. And you're failing to check individual sockets after they connect - each one needs FD_ISSET to be called to see if it has data waiting.

This topic is closed to new replies.

Advertisement