Sunteți pe pagina 1din 11

BITS Pilani

K K Birla Goa Campus


Lab 3
Lab 3
BITS Pilani, K K Birla Goa Campus
2
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 Berkele !ocket
BITS Pilani, K K Birla Goa Campus
"
Two types of Internet Sockets
!tream sockets
!#CK$!%&'A(
!#CK$!%&'A(
Connection oriented
&el on %CP to pro)ide reliable two*wa
connected communication
+atagram sockets
!#CK$+G&A(
!#CK$+G&A(
&el on ,+P
Connection is unreliable
BITS Pilani, K K Birla Goa Campus
-
Stream sockets
connection-oriented (TCP
BITS Pilani, K K Birla Goa Campus
.
!ata"ram sockets-
connection#ess socket ($!P
BITS Pilani, K K Birla Goa Campus
/
TCP-based sockets
BITS Pilani, K K Birla Goa Campus
0
Socket Ca##s
int socket(int domain% int type% int protoco#&
!ocket() 1 &eturns a file descriptor(socket I+) if
successful, *2 ot3erwise4
int bind(int sockfd% str'ct sockaddr (my)addr% int
addr#en&
Bind() 1 Associate a socket id wit3 an address to
w3ic3 ot3er processes can connect4
int #isten(int sockfd% int back#o"&
5isten() 1 &eturn 6 on success, or 72 if failure
BITS Pilani, K K Birla Goa Campus
Socket Ca##s
int connect(int sockfd% str'ct sockaddr (ser*)addr% int
addr#en&
Connect() 1 connect to a remote 3ost
int accept(int sockfd% str'ct sockaddr (addr% int (addr#en&
Accept() 1 gets t3e pending connection on t3e port ou
are listen()ing on4
int send(int sockfd% const *oid (ms"% int #en% int f#a"s&
!end() 1 !end a message4 &eturns t3e number of
btes sent or *2 if failure4
BITS Pilani, K K Birla Goa Campus
Socket Ca##s
int rec*(int sockfd% *oid (b'f% int #en% 'nsi"ned int f#a"s&
rec)() 1 &ecei)e up to len btes in buf4 &eturns t3e
number of btes recei)ed or *2 on failure4
int sh'tdown(int sockfd% int how&
s3utdown() 1 disable sending or recei)ing based on t3e
)alue 3ow4
int c#ose(int sockfd
Close() 1 Close connection corresponding to t3e socket
descriptor and frees t3e socket descriptor4
BITS Pilani, K K Birla Goa Campus
+eference
Bee89s Guide to :etwork Programming ,sing Internet !ockets
3ttp1;;bee84us;guide;bgnet;output;3tml;multipage;inde<43tml
!ockets %utorial
3ttp1;;www4linu<3owtos4org;C$C==;socket43tm
BITS Pilani, K K Birla Goa Campus
Prob#em to So#*e
a) >AP to implement %CP based ec3o ser)er4
b) >AP to implement a simple ?%P ser)er;client w3ic3 allows
t3e following operation
a) 5ist t3e files in t3e ser)er (eg1 ls)
b) get a file from t3e ser)er (get @filenameA)
c) delete t3e file in t3e ser)er (del @filenameA)
c) >AP to implement B%%P client w3ic3 will displa t3e
webpage content in t3e terminal for t3e user input
url 1 @portnumberA
!tudents s3ould complete a and b before comming to lab-(22;62;2-)4

S-ar putea să vă placă și