Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Programming.

Similar presentations


Presentation on theme: "Socket Programming."— Presentation transcript:

1 Socket Programming

2 Introduction Sockets are a protocol independent method of creating a
connection between processes. Sockets can be either connection based (TCP: Transmission Control Protocol) or connectionless (UDP: User Datagram Protocol) Connection based sockets communicate client-server: the server waits for a connection from the client Connectionless sockets are peer-to-peer

3 What is SOCKET? server client
One endpoint of a two way communication link between two program running on the Network. Connection request server port client A socket is bound to a port number Port Numbers are 16 bit unsigned binary nos. Each binary range 1 to 65,535 ( o is reserved)

4 Socket API’s socket: creates a socket of a given domain, type, protocol (buy a phone) bind: assigns a name to the socket (get a telephone number) listen: specifies the number of pending connections that can be queued for a server socket. (call waiting allowance) accept: server accepts a connection request from a client (answer phone) connect: client requests a connection request to a server (call) send, sendto: write to connection (speak) recv, recvfrom: read from connection (listen) shutdown: end the call

5 Connection Based Communication
Server performs the following actions socket: create the socket bind: give the address of the socket on the server listen: specifies the maximum number of connection requests that can be pending for this process accept: establish the connection with a specific client send,recv: stream-based equivalents of read and write (repeated) shutdown: end reading or writing close: release kernel data structures

6 TCP client Client performs the following actions
socket: create the socket connect: connect to a server send,recv: (repeated) shutdown close

7 Receive Server Client Socket Socket Bind Connect Listen Accept Close
Data request from client Receive Send Process Request Send Receive Data reply from server Close Close TCP-based Sockets

8 Header Files #include<stdio.h> #include<stdlib.h>
#include<sys/socket.h> => socket(), accept(), connect(), bind(), send() - for generic socket header file.

9 Step for establishing socket on TCP Client
Create a socket with the socket() 2. Specify the internet address family and port no 3. Check for valid address 4. Then take the size of socket address 5. Connect the socket to the address 6. Communicate using recv() 7. Print msg that is stored in buffer 8. close the socket.

10 TCP server for sending msg to client
Create TCP socket using socket(). Specify the internet address family and port no 3. Tack the IP address 4. Assign a port number to the socket with bind(). 5. Tell the system to allow connection to be made to that port, using listen(). 6. Repeatedly do the following: Call accept() to go a new socket for each client connection. Communicate with the client via that new socket using send() and recv(). Close the client connection using close().


Download ppt "Socket Programming."

Similar presentations


Ads by Google