Assignment 3 A Client/Server Application: Chatroom.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Review: –What functionality is supported by IP? –What IP does not do? –How many classes of IP addresses? –Explain fields in an IP header? –How subnet works?
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Introduction to Project 1 Web Client and Server Jan 2006.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Remote Shell CS230 Project #4 Assigned : Due date :
1 COMP445 Fall 2006 Lab assignment 1. 2 STEP1: Get the name of the second party STEP2: Get phone number from white pages CALLERRECEIVER STEP1: Plug the.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Socket Programming.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
Assignment 3 A Client/Server Application: Chatroom
Socket Programming in C
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
Socket Programming in C
TCP Sockets Programming
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Presentation transcript:

Assignment 3 A Client/Server Application: Chatroom

Socket  socket creates an endpoint for communication.  Two useful headers for socket programming:  #include

Description  Server:  supports at most 10 clients one time  Client:  will be asked for a hostname for the server e.g. “rc01xcs213.managed.mst.edu”  Will be asked for a username (alias)

Chatroom Behavior  Basic: client types a message and presses enter; server will receive the message and broadcasts to all clients  Special Commands:  If a client types /exit, /quit or /part, client exits; server prints a message that client(alias) has left  If a client presses Ctrl+C, a nice error message is printed and the user is asked to type /exit, /quit or /part  If Ctrl+C is pressed on the Server side, the server tells the clients that it will shut down in 10 seconds. Then the clients tries to exit (disconnect) gracefully and the server shuts down.

Server Side Implementation

Data Structures Server address:  sockaddr_in host={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.  An important aspect about SERVER_PORT is that all ports bellow 1024 are reserved. You can set a port above 1024 and below  Make sure that the port number is not used by other users. htons(): convert host to network short Client address: sockaddr_in peer = {AF_INET}

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 or return -1 on error. A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. socket(int domain, int type, int protocol)  domain -> communications domain in which a socket is to be created: AF_INET or AF_UNIX. (AF_INET is used for our application)  type -> type of socket to be created. (Stream or Datagram)  protocol -> 0 : use an unspecified default protocol  Example:  soc = socket(AF_INET, SOCK_STREAM, 0);

bind socket bind() – associate a socket with a port, returns -1 on error 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*)&host, sizeof(host))

listen for new connections  listen() –  listen for connection requests and declare a queue size for the incoming simultaneous connection requests. int listen(int socket, int backlog)  Backlog -> sets a limit for the number of simultaneous connection requests who are waiting to be connected. Example:  listen(soc, 4) -  only 4 simultaneous connection requests can be handled. However, there is no limit for the number of connections that can be made which arrive sporadically.

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.  Example:  netSock = accept(soc, (sockaddr*)&peer, (socklen_t*)&peerlen); create a separate thread for each client connection Each client is handled using a seperate thread. pthread_create(&myThread, NULL, ClientHandler, &var);

steps in Server so far … socket() bind() listen() accept()  Assign an unique ID (1 to n) to each client using a shared array. Use Mutex lock/unlock while accessing this array. pthread_create() for each client:  handle read/write operations in thread body

ClientHandler Declare arrays for  read buffer, write buffer, userName Each Client will send a message containing its username(alias) first Write to the client using write buffer a welcome message:  e.g: strncpy(writeBuf, "Welcome ", 8) Print to all clients that a new user (Client) has entered the chat room The data that clients send is stored in read buffer Print read buffer data on server’s terminal as well as the client terminals (except for thisClient) If a special message (/exit, /quit or /part) is received from a client  then print a goodbye message and remove this client from the client array

Client Side Implementation

Data Stuctures & functions Client structure:  members – clientName  clientId // ‘0’ initially Server’s address Info:  struct sockaddr_in host = {AF_INET, htons(SERVER_PORT)}; Buffer – an array used for writing data void* EchoHandler(void * soc) – thread handler function void signalhandler(int sig) – if Ctrl-C is pressed for client, it won’t let it exit, rather print message asking to type “/exit” or “/part” or “/quit”

Connection to 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 or rc01xcs213.managed.mst.edu) as parameter.  e.g: hp = gethostbyname(argv[1]) Print an error message if return value is NULL (hostname does not exist)  bcopy(s1, s2) copies n bytes from the area pointed to by s1 to the area pointed to by s2.  Example: bcopy(hp->h_addr_list[0], (char*)&peer.sin_addr, hp->h_length)  Where host address is saved by gethostbyname() in hp. Then, enter the client’s username (alias) as required

create a socket and connect to server  socket() - create a client socket  Connect() - connect to server, return -1 on error  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 depends on the address family of the socket.  address_len  Specifies the length of the sockaddr structure pointed to by the address argument. Create separate threads to handle read and write -A thread to accept user input and check for exit condition and write to the server -A thread for reading the messages from the server and printing it on user’s terminal -The thread handler will take care of different errors and special messages to be printed.

Ctrl+C sigHandler()  signal(SIGINT, signalhandler)  Ctrl-C will raise SIGINT  By default, SIGINT immediately terminates the process  In this assignment, you define your own SigHandler as below:  Signalhandler(int sig) { If Ctrl-C is pressed by the client, it should print a nice error message and ask the user to type /exit, /quit or /part instead. }

Thread Handling  Create Thread  Method - pthread_create  It accepts a thread variable, a thread attribute, a start routine function, and an optional argument. To use default thread attributes, pass NULL as the second argument.  Example if ( pthread_create( &thread, NULL, fromServer, &client_info)) { perror( "Show Error"); exit( 1 ); }

Thread Handling  Exit Thread  Method - pthread_exit  This method is called after a thread has completed its work and is no longer required to exist.  Example pthread_exit(thread);

 Example if ( pthread_create( &read_thrd, NULL, fromServer, &client_info)) { perror( "Show Error"); exit( 1 ); } if ( pthread_create( &write_thrd, NULL, fromUser, &client_info) { perror( "Show Error"); exit( 1 ); } pthread_join(read_thrd, NULL); pthread_join(write_thrd, NULL); pthread_exit(read_thrd); pthread_exit(write_thrd);. Thread Handling

End of Session