Assignment 4: Client Server Chatroom - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Assignment 4: Client Server Chatroom

Description:

Assignment 4: Client Server Chatroom. SOCKET. socket creates an ... sigaction(SIGINT, &act, NULL); Ctrl-C signal handler. SigHandler(int sig, ... (SIGINT, ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 27
Provided by: nd2n
Category:

less

Transcript and Presenter's Notes

Title: Assignment 4: Client Server Chatroom


1
Assignment 4 Client Server Chatroom
2
SOCKET
  • socket creates an endpoint for communication.
  • Two useful headers for socket programming
  • include ltsignal.hgt
  • include ltsys/socket.hgt

3
  • Server Side Implementation

4
Data Stuctures
  • Client structure
  • members clientName
  • clientId // 0 initially
  • sockaddr_in self AF_INET,htons(SERVER_PORT)
  • In the Internet address family, the SOCKADDR_IN
    structure is used by Windows Sockets to specify a
    local or remote endpoint address to which to
    connect a socket.
  • struct sigaction instance used to set interrupt
    action
  • Thread for clients

5
Create New Socket
  • socket() - 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.
  • socket(int domain, int type, int protocol)
  • domain - communications domain in which a socket
    is to be created.
  • type - type of socket to be created.
  • protocol - particular protocol to be used with
    the socket.
  • Protocol 0 gt use an unspecified default
    protocol appropriate for the requested socket
    type.
  • Example
  • soc socket(AF_INET, SOCK_STREAM, 0)

6
bind socket
  • bind() - bind a name to a socket
  • int bind(int socket, const struct sockaddr
    address,       socklen_t address_len)
  • socket -file descriptor of the socket to be
    bound. address -points to a sockaddr structure
    containing the address to be bound to the socket.
  • address_len - length of the sockaddr structure
    pointed to by the address argument.
  • Example
  • Bind(soc, (sockaddr)self, sizeof(self))

7
listen for new connections
  • listen() - listen for socket connections and
    limit the queue of incoming connections.
  • int listen(int socket, int backlog)
  • Backlog provides a hint to the implementation
    which the implementation shall use to limit the
    number of outstanding connections in the socket's
    listen queue.
  • Example
  • listen(soc, 1)

8
Accept a new connection
  • accept() - accepts a new connection on a socket
  • int accept(int socket, struct sockaddr restrict
    address, socklen_t restrict address_len)
  • Address - Either a null pointer, or a pointer to
    a sockaddr structure where the address of the
    connecting socket shall be returned
  • address_len - Points to a socklen_t structure
    which on input specifies the length of the
    supplied sockaddr structure, and on output
    specifies the length of the stored address.
  • var accept(soc, (sockaddr)peer,
    (socklen_t)peerlen)

9
create new threads for each connection
  • Each client is handled using a seperate thread.
  • pthread_create(myThread, NULL, ClientHandler,
    var)

10
Error Control
  • bind
  • accept
  • listen
  • thread create
  • read and write operations
  • etc..

11
set interrupt action
  • If the SA_SIGINFO flag is cleared in the
    sa_flags field of the sigaction structure, the
    sa_handler field identifies the action to be
    associated with the specified signal.
  • sa_sigaction Pointer to a signal-catching
    function.
  • int sigaction(int signum, const struct sigaction
    act, struct sigaction oldact)
  • The sigaction system call is used to change the
    action taken by a process on receipt of a
    specific signal.
  • signum specifies the signal and can be any valid
    signal except SIGKILL and SIGSTOP.
  • If act is non-null, the new action for signal
    signum is installed from act.
  • If oldact is non-null, the previous action is
    saved in oldact.

12
Example
  • Struct sigaction act
  • act.sa_flags SA_SIGINFO
  • act.sa_sigaction SigHandler
  • sigaction(SIGINT, act, NULL)

13
Ctrl-C signal handler
  • SigHandler(int sig, siginfo_t si, void b)
  • Print message in server that it is shutting down
  • strcat to write buffer the message that server
    is shutting down and call the function to write
    all clients using this buffer
  • sleep for 10 sec
  • exit

14
Writing to all clients
  • allClientwrite()
  • Mutex lock the thread for client
  • For all clients except thisClient and clientId0
  • Write the message stored in write buffer
  • Unlock mutex
  • End

15
ClientHandler
  • Declare arrays as read buffer, write buffer,
    thisName
  • Mutex lock on client thread
  • Set client IDs from 1 to n
  • Unlock mutex
  • Write to the client using write buffer a welcome
    message
  • Eg strncpy(writeBuf, "Welcome ", 8)

16
  • Print to all clients that thisClient has entered
    the room
  • The data that clients send is stored in read
    buffer
  • Print read buffer data to all clients except
    thisClient
  • If readbuffer data is special messages like
    /exit etc
  • then print goodbye message to all clients
  • make clientId0
  • Exit
  • However, make sure you implement all the special
    exit messages mentioned in the assignment
    (/exit,/part,/quit)

17
Client Side Implementation
18
Data Stuctures functions
  • struct sockaddr_in peer AF_INET,
    htons(SERVER_PORT)
  • Buffer an array used for writing data
  • void EchoHandler(void soc) thread handler
    function
  • struct sigaction act - used to set interrupt
    action
  • void siginthandler(int signum) if ctrl-c is
    pressed for client, it wont let it exit, rather
    print message asking to type /exit or /part
    etc..

19
Connect the client to the server
  • Prompt to enter the hostname to which user wants
    to connect.
  • Use gethostbyname to save the hostname
  • gethostbyname() is used to get its IP address
    and store it in a struct in_addr
  • Takes a string (like www.yahoo.com or
    rc01xcs213.managed.mst.edu) as parameter.
  • Print an error message if return value is NULL
    (unable to connect)
  • Input the clients name

20
Call signal()
  • The signal() function chooses one of three ways
    in which receipt of the signal number sig is to
    be subsequently handled.
  • void (signal(int sig, void (func)(int)))(int)
  • If the value of func is SIG_DFL, default
    handling for that signal will occur. If the value
    of func is SIG_IGN, the signal will be ignored.
    Otherwise, func must point to a function to be
    called when that signal occurs. Such a function
    is called a signal handler.
  • signal(SIGINT, siginthandler)
  • siginthandler deals with ctrl-c. On pressing
    ctrl-c on a client, it should prompt a message
    instead of exiting.

21
Copy the host address into peer socket
  • The bcopy() function shall copy n bytes from the
    area pointed to by s1 to the area pointed to by
    s2.
  • bcopy(hp-gth_addr_list0, (char)peer.sin_addr,
    hp-gth_length)
  • Where host is saved by gethostname in hp.

22
create a socket and connect to server
  • socket() function to create a client socket
  • Connect() to connect to the server
  • int connect(int socket, const struct sockaddr
    address,       socklen_t address_len)
  • socket Specifies the file descriptor associated
    with the socket. address Points to a sockaddr
    structure containing the peer address. The length
    and format of the address depend on the address
    family of the socket. address_len Specifies the
    length of the sockaddr structure pointed to by
    the address argument.
  • Error control

23
Thread for Client
  • Create threads to handle read and write the
    client
  • The thread handler function will take care of
    different errors and special messages to be
    printed.
  • Example
  • pthread_create(myThread, NULL, EchoHandler,
    socket)

24
Operations of a Client
  • write
  • write(soc, buffer, strlen(buffer))
  • read -
  • read(soc, buffer, strlen(buffer))
  • In case it is writing /exit etc, sleep for
    sometime and exit
  • Ctrl-C handler
  • Print an error message, but dont exit client on
    Ctrl-C

25
Other Functions
  • Read buffer and check for different quitting
    conditions
  • If the server shutting down message is read from
    buffer, print server is shutting and exit
  • If the client wants to quit, print goodbye
    message and exit

26
THANKS ?
Write a Comment
User Comments (0)
About PowerShow.com