Code:
/*
* Console Chat V0.1,
* written by eXeCuTeR <executerx[at]gmail[dot]com>, 9/7/08
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 10020
#define PACKETSIZE 100
#define MAXCONNECTIONS 10
#define USERNAMELENGTH 20
#define STRING 50
typedef struct
{
char *ipaddress;
char name[USERNAMELENGTH];
} userInfo;
char *usernamebyip(userInfo *users, struct sockaddr_in *client);
void getcurrenttime(char *str, char *newstr);
void cutNewLine(char *str);
int main(void)
{
// variable declarations
int sockfd, newfd, loopbetweenDesc, userIndexer = 0;
struct sockaddr_in server, client;
userInfo users[MAXCONNECTIONS + 10];
fd_set mainSet, current;
const char *nickReq = "Enter your nickname: ";
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) // creating a TCP socket
{
perror("socket");
exit(1);
}
// configuration
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(PORT);
memset(&server.sin_zero, '\0', 8);
if(bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) // binding to the given port
{
perror("bind");
exit(1);
}
if(listen(sockfd, MAXCONNECTIONS) == -1) // listening only to MAXCONNECTIONS connections
{
perror("listen");
exit(1);
}
time_t now = time(0);
printf("CC Server is running, %s", ctime(&now));
loopbetweenDesc = sockfd;
// initializing the lists
FD_ZERO(&mainSet);
FD_ZERO(¤t);
FD_SET(sockfd, &mainSet); // adding the server to our streams list
while(1)
{
int recvNum = 0, i = 0;
current = mainSet; // current will be changed (and might not) when calling select, so we are using main to hold all of ourr streams
// so every loop we can try and go over all of our streams
if(select(loopbetweenDesc + 1, ¤t, NULL, NULL, NULL) == -1)
{
perror("select");
exit(1);
}
for(; i <= loopbetweenDesc; i++) // looping between our connections
{
if(FD_ISSET(i, ¤t)) // if a connection is ready
{
time_t current_time = time(0);
char *time = malloc(10);
getcurrenttime(ctime(¤t_time), time);
char talker[PACKETSIZE/2], buffer[PACKETSIZE], data[PACKETSIZE + (PACKETSIZE/2)];
bzero(&buffer, PACKETSIZE); // client's message
// note: data (which holds talker + buffer) is not bzeroed because strcat will automatically add the null-terminator
if(sockfd == i) // if our server is ready to accept a new connection
{
size_t size = sizeof(client);
if( (newfd = accept(sockfd, (struct sockaddr *)&client, &size)) == -1)
perror("accept");
else
{
FD_SET(newfd, &mainSet); // add the new connection to our list
FD_SET(newfd, ¤t); // we want to greet him for entering the server
bzero(users[userIndexer].name, USERNAMELENGTH);
users[userIndexer].ipaddress = inet_ntoa(client.sin_addr);
do
{
if(send(newfd, (char *)nickReq, strlen(nickReq) + 1, 0) == -1)
perror("send");
else
{
int tempBytesNick = 0;
if( (tempBytesNick = recv(newfd, users[userIndexer].name, USERNAMELENGTH, 0)) == -1)
perror("recv");
}
} while(strlen(users[userIndexer].name) <= 2);
cutNewLine(users[userIndexer].name);
if(newfd > loopbetweenDesc) // if it is bigger, swap because we do not want to miss this connection later in our loop
loopbetweenDesc = newfd;
char userJoinedMsg[PACKETSIZE];
bzero(&userJoinedMsg, PACKETSIZE);
sprintf(userJoinedMsg, "\n%s <%s, %s> has joined the server.\n", users[userIndexer].name, inet_ntoa(client.sin_addr), time);
printf("%s", userJoinedMsg); // informing the server
int h = 0;
for(; h <= loopbetweenDesc; h++) // informing the others that i has joined
{
if(FD_ISSET(h, &mainSet) && h != sockfd)
{
if(newfd == h)
{
char greeting[STRING/2];
sprintf(greeting, "Hello %s and welcome to the server.\n", users[userIndexer].name);
if(send(h, greeting, strlen(greeting), 0) == -1)
perror("send");
}
else
{
if(send(h, userJoinedMsg, strlen(userJoinedMsg), 0) == -1)
perror("send");
}
}
}
userIndexer++;
}
}
else
{
if( (recvNum = recv(i, buffer, PACKETSIZE, 0)) == -1)
perror("recv");
else if(recvNum == 0) // if client has closed the connection
{
int k =0;
shutdown(i, 2);
FD_CLR(i, &mainSet);
FD_CLR(i, ¤t); // we want to inform them that i left
char userLeftMsg[PACKETSIZE];
bzero(&userLeftMsg, PACKETSIZE);
size_t peer_size_left = sizeof(struct sockaddr);
getpeername(i, (struct sockaddr *)&client, &peer_size_left);
sprintf(userLeftMsg, "%s <%s, %s> has left the server.\n", usernamebyip((userInfo *)&users, &client), inet_ntoa(client.sin_addr), time);
printf("%s", userLeftMsg); // informing the server
for(; k <= loopbetweenDesc; k++) // informing the others that i has left
if(FD_ISSET(k, ¤t) && k != sockfd)
if(send(k, userLeftMsg, strlen(userLeftMsg), 0) == -1)
perror("send");
}
else // if recv was successful
{
bzero(&talker, PACKETSIZE/2); // holds __IP__ says
size_t peer_size = sizeof(struct sockaddr);
getpeername(i, (struct sockaddr *)&client, &peer_size);
sprintf(talker, "%s says <%s>: ", usernamebyip((userInfo *)&users, &client), time);
bzero(&data, PACKETSIZE + (PACKETSIZE / 2));
strcat(data, talker);
strcat(data, buffer); // appending talker and buffer to data
printf("%s", data);
int u = 0;
for(; u <= loopbetweenDesc; u++)
{
if(FD_ISSET(u, &mainSet) && u != sockfd)
{
if(u == i) // the sender (i) does not need to receive his own message
continue;
else
{
if(send(u, data, strlen(data), 0) == -1)
perror("send");
}
}
}
}
}
free(time);
}
}
}
return 0;
}
char *usernamebyip(userInfo *users, struct sockaddr_in *client)
{
char *seekAddress = inet_ntoa(client->sin_addr);
int i = 0;
for(; i < MAXCONNECTIONS * 2; i++)
if(strcmp(users[i].ipaddress, seekAddress) == 0)
return users[i].name;
return "Unknown";
}
void getcurrenttime(char *str, char *newstr)
{
int index = 0, i = 11;
for(; i < 19; i++)
newstr[index++] = str[i];
newstr[index] = '\0';
}
void cutNewLine(char *str)
{
int i = 0;
for(; i < strlen(str); i++)
{
if(str[i] == '\r')
{
str[i] = '\0';
break;
}
}
}