Web Client Programming with Perl-Chapter 4: The Socket Library- P1
Số trang: 26
Loại file: pdf
Dung lượng: 63.38 KB
Lượt xem: 9
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu web client programming with perl-chapter 4: the socket library- p1, công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Web Client Programming with Perl-Chapter 4: The Socket Library- P1 Chapter 4: The Socket Library- P1The socket library is a low-level programmers interface that allows clientsto set up a TCP/IP connection and communicate directly to servers. Serversuse sockets to listen for incoming connections, and clients use sockets toinitiate transactions on the port that the server is listening on.Do you really need to know about sockets? Possibly not. In Chapter 5, TheLWP Library, we cover LWP, a library that includes a simple framework forconnecting to and communicating over the Web, making knowledge of theunderlying network communication superfluous. If you plan to use LWP youcan probably skip this chapter for now (and maybe forever).Compared to using something like LWP, working with sockets is a tediousundertaking. While it gives you the power to say whatever you want throughyour network connection, you need to be really careful about what you say;if its not fully compliant with the HTTP specs, the web server wontunderstand you! Perhaps your web client works with one web server but notanother. Or maybe your web client works most of the time, but not in specialcases. Writing a fully compliant application could become a real headache.A programmers library like LWP will figure out which headers to use, theparameters with each header, and special cases like dealing with HTTPversion differences and URL redirections. With the socket library, you do allof this on your own. To some degree, writing a raw client with the socketlibrary is like reinventing the wheel.However, some people may be forced to use sockets because LWP isunavailable, or because they just prefer to do things by hand (the way somepeople prefer to make spaghetti sauce from scratch). This chapter covers thesocket calls that you can use to establish HTTP connections independentlyof LWP. At the end of the chapter are some extended examples usingsockets that you can model your own programs on.A Typical Conversation over SocketsThe basic idea behind sockets (as with all TCP-based client/server services)is that the server sits and waits for connections over the network to the portin question. When a client connects to that port, the server accepts theconnection and then converses with the client using whatever protocol theyagree on (e.g., HTTP, NNTP, SMTP, etc.).Initially, the server uses the socket( ) system call to create the socket, and thebind( ) call to assign the socket to a particular port on the host. The serverthen uses the listen( ) and accept( ) routines to establish communication onthat port.On the other end, the client also uses the socket( ) system call to create asocket, and then the connect( ) call to initiate a connection associated withthat socket on a specified remote host and port.The server uses the accept( ) call to intercept the incoming connection andinitiate communication with the client. Now the client and server can eachuse sysread( ) and syswrite( ) calls to speak HTTP, until the transaction isover.Instead of using sysread( ) and syswrite( ), you can also just read from andwrite to the socket as you would any other file handle (e.g., print ;).Finally, either the client or server uses the close( ) or shutdown( ) routine toend the connection.Figure 4-1 shows the flow of a sockets transaction. Figure 4-1. Socket callsUsing the Socket CallsThe socket library is part of the standard Perl distribution. Include the socketmodule like this:use Socket;Table 4-1 lists the socket calls available using the socket library in Perl. Table 4-1: Socket CallsFunction Usage Purpose Both clientsocket( ) Create a generic I/O buffer in the operating system and serverconnect( Establish a network connection and associate it Client only) with the I/O buffer created by socket( ) Both clientsysread( ) Read data from the network connection and serversyswrite( Both client Write data to the network connection) and server Both clientclose( ) Terminate communication and server Associate a socket buffer with a port on thebind( ) Server only machinelisten( ) Server only Wait for incoming connection from a clientaccept( ) Server only Accept the incoming connection from clientConceptually, think of a socket as a pipe between the client and server.Data written to one end of the pipe appears on the other end of the pipe. Tocreate a pipe, call socket( ). To write data into one end of the pipe, callsyswrite( ). To read on the other end of the pipe, call sysread( ). Finally, todispose of the pipe and cease communication between the client and server,call close( ).Since this book is primarily about client programming, well talk about thesocket calls used by clients first, followed by the calls that are only used onthe server end. Although were only writing client programs, we cover bothclient and server functions, for the sake of showing how the library fitstogether.Initializing the SocketBoth the client and server use the socket( ) function to create a genericpipe or I/O buffer in the operating system. The socket( ) call takes severalarguments, specifying which file handle to associate with the socket, whatthe network protocol is, and whether the socket should be stream-oriented orrecord-oriented. For HTTP transactions, sockets are stream-orientedconnections running TCP over IP, so HTTP-based applications mustassociate these characteristics with a newly created socket.For example, in the following line ...
Nội dung trích xuất từ tài liệu:
Web Client Programming with Perl-Chapter 4: The Socket Library- P1 Chapter 4: The Socket Library- P1The socket library is a low-level programmers interface that allows clientsto set up a TCP/IP connection and communicate directly to servers. Serversuse sockets to listen for incoming connections, and clients use sockets toinitiate transactions on the port that the server is listening on.Do you really need to know about sockets? Possibly not. In Chapter 5, TheLWP Library, we cover LWP, a library that includes a simple framework forconnecting to and communicating over the Web, making knowledge of theunderlying network communication superfluous. If you plan to use LWP youcan probably skip this chapter for now (and maybe forever).Compared to using something like LWP, working with sockets is a tediousundertaking. While it gives you the power to say whatever you want throughyour network connection, you need to be really careful about what you say;if its not fully compliant with the HTTP specs, the web server wontunderstand you! Perhaps your web client works with one web server but notanother. Or maybe your web client works most of the time, but not in specialcases. Writing a fully compliant application could become a real headache.A programmers library like LWP will figure out which headers to use, theparameters with each header, and special cases like dealing with HTTPversion differences and URL redirections. With the socket library, you do allof this on your own. To some degree, writing a raw client with the socketlibrary is like reinventing the wheel.However, some people may be forced to use sockets because LWP isunavailable, or because they just prefer to do things by hand (the way somepeople prefer to make spaghetti sauce from scratch). This chapter covers thesocket calls that you can use to establish HTTP connections independentlyof LWP. At the end of the chapter are some extended examples usingsockets that you can model your own programs on.A Typical Conversation over SocketsThe basic idea behind sockets (as with all TCP-based client/server services)is that the server sits and waits for connections over the network to the portin question. When a client connects to that port, the server accepts theconnection and then converses with the client using whatever protocol theyagree on (e.g., HTTP, NNTP, SMTP, etc.).Initially, the server uses the socket( ) system call to create the socket, and thebind( ) call to assign the socket to a particular port on the host. The serverthen uses the listen( ) and accept( ) routines to establish communication onthat port.On the other end, the client also uses the socket( ) system call to create asocket, and then the connect( ) call to initiate a connection associated withthat socket on a specified remote host and port.The server uses the accept( ) call to intercept the incoming connection andinitiate communication with the client. Now the client and server can eachuse sysread( ) and syswrite( ) calls to speak HTTP, until the transaction isover.Instead of using sysread( ) and syswrite( ), you can also just read from andwrite to the socket as you would any other file handle (e.g., print ;).Finally, either the client or server uses the close( ) or shutdown( ) routine toend the connection.Figure 4-1 shows the flow of a sockets transaction. Figure 4-1. Socket callsUsing the Socket CallsThe socket library is part of the standard Perl distribution. Include the socketmodule like this:use Socket;Table 4-1 lists the socket calls available using the socket library in Perl. Table 4-1: Socket CallsFunction Usage Purpose Both clientsocket( ) Create a generic I/O buffer in the operating system and serverconnect( Establish a network connection and associate it Client only) with the I/O buffer created by socket( ) Both clientsysread( ) Read data from the network connection and serversyswrite( Both client Write data to the network connection) and server Both clientclose( ) Terminate communication and server Associate a socket buffer with a port on thebind( ) Server only machinelisten( ) Server only Wait for incoming connection from a clientaccept( ) Server only Accept the incoming connection from clientConceptually, think of a socket as a pipe between the client and server.Data written to one end of the pipe appears on the other end of the pipe. Tocreate a pipe, call socket( ). To write data into one end of the pipe, callsyswrite( ). To read on the other end of the pipe, call sysread( ). Finally, todispose of the pipe and cease communication between the client and server,call close( ).Since this book is primarily about client programming, well talk about thesocket calls used by clients first, followed by the calls that are only used onthe server end. Although were only writing client programs, we cover bothclient and server functions, for the sake of showing how the library fitstogether.Initializing the SocketBoth the client and server use the socket( ) function to create a genericpipe or I/O buffer in the operating system. The socket( ) call takes severalarguments, specifying which file handle to associate with the socket, whatthe network protocol is, and whether the socket should be stream-oriented orrecord-oriented. For HTTP transactions, sockets are stream-orientedconnections running TCP over IP, so HTTP-based applications mustassociate these characteristics with a newly created socket.For example, in the following line ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 432 1 0
-
24 trang 358 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 319 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 308 0 0 -
74 trang 303 0 0
-
96 trang 297 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 284 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0