lab3
TRANSCRIPT
BITS PilaniK K Birla Goa Campus
Lab 3Lab 3
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
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
BITS Pilani, K K Birla Goa Campus4
Stream sockets – connection-oriented (TCP)
BITS Pilani, K K Birla Goa Campus5
Datagram sockets- connectionless socket (UDP)
BITS Pilani, K K Birla Goa Campus6
TCP-based sockets
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
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.
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.
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
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).