Information Technology Reference
In-Depth Information
Table 2.1.
Comparison of file and socket calls.
Operation
Files
Sockets
Open
open()
Client
socket()
bind()
connect()
Server
socket()
bind()
listen()
accept()
Close
close()
close()
shutdown()
Read
read()
read()
recv()
recvfrom()
Write
write()
write()
send()
sendto()
Seek
lseek()
Poll
select()
select()
Create
creat()
to see if they are ready for I/O or if certain events have occurred. UNIX systems
also provide asynchronous I/O which is used in conjunction with select() thereby
allowing a single process to eciently handle a large number of open files or sockets
simultaneously.
2.2.2
Socket Example
Our simple example provides the current system date to a client on demand and
prints it to the console. The protocol we use between client and server is based on
the exchange of C style strings between client and server. While this may sound
simple, it is not, due to fundamental differences between file I/O and network I/O
even though both share the same interface.
Using sockets, a read or write call may input or output fewer bytes than re-
quested due to underlying kernel buffer limits. However, a read operation from a
file is guaranteed to return the number of bytes requested providing that the num-
ber requested is less than or equal to that remaining before end-of-file. In the event
a read or write call returns fewer bytes than expected, the call must be invoked
again to receive or send the remaining bytes [96]. This can significantly complicate
socket development, particularly if the protocol developed contains many different
packet types of different sizes and advanced features such as slidingwindows and
 
Search WWH ::




Custom Search