lab3

11
BITS Pilani K K Birla Goa Campus Lab 3 Lab 3

Upload: savya-mittal

Post on 01-May-2017

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lab3

BITS PilaniK K Birla Goa Campus

Lab 3Lab 3

Page 2: lab3

BITS Pilani, K K Birla Goa Campus2

What is socket?

An interface between an application process

An Application Programming Interface (API) used for InterProcess Communications (IPC)

It can also be called as Berkeley Socket

Page 3: lab3

BITS Pilani, K K Birla Goa Campus3

Two types of Internet Sockets

Stream sockets SOCK_STREAMSOCK_STREAMConnection orientedRely on TCP to provide reliable two-way connected communication

Datagram sockets SOCK_DGRAMSOCK_DGRAMRely on UDPConnection is unreliable

Page 4: lab3

BITS Pilani, K K Birla Goa Campus4

Stream sockets – connection-oriented (TCP)

Page 5: lab3

BITS Pilani, K K Birla Goa Campus5

Datagram sockets- connectionless socket (UDP)

Page 6: lab3

BITS Pilani, K K Birla Goa Campus6

TCP-based sockets

Page 7: lab3

BITS Pilani, K K Birla Goa Campus7

Socket Calls

int socket(int domain, int type, int protocol);

Socket() : Returns a file descriptor(socket ID) if successful, -1 otherwise.

int bind(int sockfd, struct sockaddr *my_addr, int addrlen);

Bind() : Associate a socket id with an address to which other processes can connect.

int listen(int sockfd, int backlog);

Listen() : Return 0 on success, or –1 if failure

Page 8: lab3

BITS Pilani, K K Birla Goa Campus

Socket Calls

int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);

Connect() : connect to a remote host

int accept(int sockfd, struct sockaddr *addr, int *addrlen);

Accept() : gets the pending connection on the port you are listen()ing on.

int send(int sockfd, const void *msg, int len, int flags);

Send() : Send a message. Returns the number of bytes sent or -1 if failure.

Page 9: lab3

BITS Pilani, K K Birla Goa Campus

Socket Calls

int recv(int sockfd, void *buf, int len, unsigned int flags);

recv() : Receive up to len bytes in buf. Returns the number of bytes received or -1 on failure.

int shutdown(int sockfd, int how);

shutdown() : disable sending or receiving based on the value how.

int close(int sockfd)

Close() : Close connection corresponding to the socket descriptor and frees the socket descriptor.

Page 10: lab3

BITS Pilani, K K Birla Goa Campus

Reference

Beej's Guide to Network Programming Using Internet Sockets http://beej.us/guide/bgnet/output/html/multipage/index.html

Sockets Tutorial http://www.linuxhowtos.org/C_C++/socket.htm

Page 11: lab3

BITS Pilani, K K Birla Goa Campus

Problem to Solve

a) WAP to implement TCP based echo server.

b) WAP to implement a simple FTP server/client which allows the following operation

a) List the files in the server (eg: ls)b) get a file from the server (get <filename>)c) delete the file in the server (del <filename>)

c) WAP to implement HTTP client which will display the webpage content in the terminal for the user input url : <portnumber>

Students should complete a and b before comming to lab4(22/02/14).