1 / 12

Project 2: Socket Programming

Project 2: Socket Programming. Overview. Sockets Working with sockets Client-Server example Project 1 Hints. Socket. What is socket? An abstraction through which an application may send and receive data Different types Stream sockets  we will use for our project Datagram sockets.

mairi
Télécharger la présentation

Project 2: Socket Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Project 2: Socket Programming

  2. Overview • Sockets • Working with sockets • Client-Server example • Project 1 • Hints

  3. Socket • What is socket? • An abstraction through which an application may send and receive data • Different types • Stream sockets  we will use for our project • Datagram sockets

  4. Berkeley Sockets The socket primitives for TCP.

  5. Working with Sockets • Sender creates a new socket • Attach a local address to the socket: binding operation • Receiver listens, announces willingness to accept socket connection with a queue size announcement • Block the caller/receiver until a connection establishment attempt arrives • Sender and Receiver are connected • Send data (by sender) and receive data (by receiver) • When done sending and receiving data explicitly close the connection

  6. A Client-Server Example • TCP Client • Create a TCP socket • sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); • Establish a connection to the server • connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr) • Communicate with server • send(sock, msg, stringLen, 0) • recv(sock, buffer, RCVBUFSIZE - 1, 0) • Close the connection • close(sock)

  7. A Client-Server Example (cont.) • TCP Server • Create a TCP socket • servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) • Assign a port number to the socket • bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr) • Tell the system to allow connections for that port • listen(servSock, MAXPENDING) • Accept new client connection • clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntLen) • Communicate (send, recv) using clntSock • Close client connection (close clntSock )

  8. Project 2 • Requirements • Implement a chat program that incorporates both client and server functionalities Server client client client

  9. Illustration • ssh-server% chat • connect 192.168.1.3 10000 // connect to the server in 192.168.1.3 in port 10000 • accept connection from 192.168.1.4 // accept a connection from the client in 192.168.1.4 • receive HELLO from 192.168.1.4 // get data message from the client in 192.168.1.4 • receive HELLO from 192.168.1.3 // get data message from the server in 192.168.1.3 • send 192.168.1.3 HELLO // send message to the server in 192.168.1.3 • send 192.168.1.4 HELLO // send message to the client in 192.168.1.4

  10. Notes for Our Project • Need for “multiple” requests • Server needs to receive • New client connection attempts • Connected clients’ data • User’s input (standard input) • Client needs to receive • Server’s data • User’s input (standard input)

  11. Notes for Our Project (cont.) • Solution for “multiple” requests • Using “select” -- a single process handles multiple requests • select() works by blocking until something happens on a file descriptor (e.g., a socket) • Usage (http://www.lowtek.com/sockets/select.html) • Fill up a fd_set structure with the file descriptors you want to know when data comes in on. • Call select() and block until something happens • Once select() returns, check to see if any of your file descriptors was the reason you woke up, then do the following operation • Repeat this process

  12. Submission Details • Submit the electronic copy • Set up appointment with the instructor/TA to demonstrate the functionality

More Related