server.c File Reference

This program acts like a server in the LAN Messenger. More...

#include "server.h"

Go to the source code of this file.

Functions

void Close (int fd)
 This function is used for closing a file.
void close_properly (int signal)
 This function is called when we press Ctrl+C key or on any error condition.
ssize_t Write (int fd, const void *buf, size_t count)
 This function is used for writing into the file/socket.
int Socket (int domain, int type, int protocol)
 This function creates an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets.
void Bind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
 This function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned.
void Listen (int sockfd, int backlog)
 This function shall mark a connection-mode socket, specified by the socket argument, as accepting connections.
int Accept (int sockfd, struct sockaddr *addr, socklen_t *addrlen)
 This function hall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket.
ssize_t Read (int fd, void *buf, size_t count)
 This function reads the data from file/socket into a buffer.
int isNickExists (char *nick)
 This function checks if the client who is attempting to connect with a nick name already exists or not.
void insert_row (char *query)
 This function inserts a row into a mysql database.
void write_all (char *data)
 This function sends the message send by one client to all other connected clients.
void add_widget_with_label (GtkContainer *box, gchar *caption, GtkWidget *widget)
 This function adds the widget to the Box with corresponding label passed.
void end_program (GtkWidget *widget1, gpointer data)
 This function calls when we close the window.
void onExit (GtkWidget *widget1, gpointer data)
 This function calls when we close the window.
void mysql_connect_create_table ()
 This function connects to the database and creates a table (if not present) for logging the chats of clients.
void connect_server (GtkWidget *widget1, gpointer data)
 This function stars the server with the given port for listening.It is called when we press "Connect" button Widget.
void * start_server (void *arg)
 This function starts the server.
int main (int argc, char *argv[])
 This is where program execution starts.


Detailed Description

This program acts like a server in the LAN Messenger.

It listens to the user specified port and accepts all client connections and then it would list all the messages being sent by the clients, log them into a database and list of all connected clients.

Author:
Ratna Kumar K.V.R

Definition in file server.c.


Function Documentation

int Accept ( int  sockfd,
struct sockaddr *  addr,
socklen_t *  addrlen 
)

This function hall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket.

Parameters:
sockfd Specifies a socket that was created with socket(), has been bound to an address with bind(), and has issued a successful call to listen().
addr Either a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket shall be returned.
addrlen Points to a socklen_t structure which specifies the length of the supplied sockaddr structure.
Returns:
the non-negative file descriptor of the accepted socket. Otherwise, -1 shall be returned and errno set to indicate the error.

Definition at line 126 of file server.c.

Referenced by start_server().

void add_widget_with_label ( GtkContainer *  box,
gchar *  caption,
GtkWidget *  widget 
)

This function adds the widget to the Box with corresponding label passed.

Parameters:
box Box into which widget has to be packed
caption label to be given.
widget widget to be packed.

Definition at line 242 of file server.c.

Referenced by main().

void Bind ( int  sockfd,
const struct sockaddr *  my_addr,
socklen_t  addrlen 
)

This function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned.

Parameters:
sockfd Specifies the file descriptor of the socket to be bound.
my_addr Points to a sockaddr structure containing the address to be bound to the socket.
addrlen Specifies the length of the sockaddr structure pointed to by the address argument.

Definition at line 92 of file server.c.

References err_msg, and success.

Referenced by connect_server().

void Close ( int  fd  ) 

This function is used for closing a file.

It returns an error message if it cannot close the file/socket.

Parameters:
fd file descriptor of the file/socket to be closed.

Definition at line 16 of file server.c.

Referenced by close_properly(), end_program(), and start_server().

void close_properly ( int  signal  ) 

This function is called when we press Ctrl+C key or on any error condition.

It does proper closing of all file descriptors and database connections.

Parameters:
signal (SIGINT)

Definition at line 30 of file server.c.

References client, Close(), connection1, listen_file_descriptor, and maxi.

Referenced by insert_row(), and start_server().

void connect_server ( GtkWidget *  widget1,
gpointer  data 
)

This function stars the server with the given port for listening.It is called when we press "Connect" button Widget.

It gives appropriate message if it cannot listen to the port.

Parameters:
widget1 Connect button
data pointer

Definition at line 318 of file server.c.

References Bind(), entry_port, err_msg, Listen(), listen_file_descriptor, Socket(), and success.

Referenced by main().

void end_program ( GtkWidget *  widget1,
gpointer  data 
)

This function calls when we close the window.

. which quits the window and exits the program. It shutdowns and closes all the connections.

Parameters:
widget1 Widget to be closed
data pointer

Definition at line 257 of file server.c.

References client, Close(), connection1, listen_file_descriptor, and maxi.

Referenced by main().

void insert_row ( char *  query  ) 

This function inserts a row into a mysql database.

The row contains information like ID, timestamp, message of client, nickname of client, IP address of client. basically logging the chat into database.

Parameters:
query insert query.

Definition at line 184 of file server.c.

References close_properly(), and connection1.

Referenced by write_all().

int isNickExists ( char *  nick  ) 

This function checks if the client who is attempting to connect with a nick name already exists or not.

Parameters:
nick nickname of client attempting to connect
Returns:
1 if nick already exists, 0 if not exists.

Definition at line 161 of file server.c.

References client, and maxi.

Referenced by start_server().

void Listen ( int  sockfd,
int  backlog 
)

This function shall mark a connection-mode socket, specified by the socket argument, as accepting connections.

Parameters:
sockfd file descriptor on which it has to listen.
backlog used to limit the number of outstanding connections in the socket's listen queue.

Definition at line 107 of file server.c.

References err_msg, and success.

Referenced by connect_server().

int main ( int  argc,
char *  argv[] 
)

This is where program execution starts.

All arguments are passed to gtk for processing, which removes any arguments relevant to it. All other arguments are ignored. This function first gets a window for user to enter listening port. If it can listen succesfully then it displays the chat room, else it displays the dialog box with approriate message.

Parameters:
argc Number of arguments passed.
argv NULL terminated array of command line arguments.
Returns:
EXIT_SUCCESS on success, EXIT_FAILURE on failure

Definition at line 568 of file server.c.

References add_widget_with_label(), buffer, connect_server(), end_program(), entry_port, iter, list_store, mysql_connect_create_table(), onExit(), and start_server().

void mysql_connect_create_table (  ) 

This function connects to the database and creates a table (if not present) for logging the chats of clients.

Definition at line 290 of file server.c.

References connection1.

Referenced by main().

void onExit ( GtkWidget *  widget1,
gpointer  data 
)

This function calls when we close the window.

. which quits the window and exits the program. so it shutdowns and closes all the connections.

Parameters:
widget1 Widget to be closed
data pointer

Definition at line 282 of file server.c.

Referenced by main().

ssize_t Read ( int  fd,
void *  buf,
size_t  count 
)

This function reads the data from file/socket into a buffer.

Parameters:
fd file descriptor used for reading data
buf buffer into which the data read from file is stored
count Maximum number of bytes to be read
Returns:
number of bytes read ( < 0 indicates error)

Definition at line 145 of file server.c.

Referenced by start_server().

int Socket ( int  domain,
int  type,
int  protocol 
)

This function creates an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets.

Parameters:
domain Specifies the communications domain in which a socket is to be created.
type Specifies the type of socket to be created.
protocol Specifies a particular protocol to be used with the socket.
Returns:
non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.

Definition at line 74 of file server.c.

References err_msg, and success.

Referenced by connect_server().

void* start_server ( void *  arg  ) 

This function starts the server.

It accepts the client connections and sends the client messages to all other connected clients, logs the chat into database and lists the connected clients.

Parameters:
arg arguments to be sent

Definition at line 354 of file server.c.

References Accept(), buffer, BUFFER, client, Close(), close_properly(), client_data::fd, client_data::ip, ip, isNickExists(), iter, list_store, listen_file_descriptor, maxi, client_data::nick, nick, Read(), t_iter, Write(), and write_all().

Referenced by main().

ssize_t Write ( int  fd,
const void *  buf,
size_t  count 
)

This function is used for writing into the file/socket.

It prints error message and returns the status if it fails to write.

Parameters:
fd file descriptor of the file into which we write.
buf pointer to the buffer which stores the data to be written.
count number of bytes to be written.
Returns:
number of bytes written ( < 0 indicates failure)

Definition at line 55 of file server.c.

Referenced by start_server(), and write_all().

void write_all ( char *  data  ) 

This function sends the message send by one client to all other connected clients.

Parameters:
data message to be sent.

Definition at line 201 of file server.c.

References buffer, BUFFER, client, insert_row(), ip, transmit_unit::ip, iter, maxi, transmit_unit::msg, nick, transmit_unit::nick, transmit_unit::time, and Write().

Referenced by start_server().


Generated on Wed Apr 7 17:06:31 2010 for LAN-Messenger(Server) by  doxygen 1.5.8