TCP/ IP sockets in c

196 1.6K 0
TCP/ IP sockets in c

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Đây là bộ sách tiếng anh cho dân công nghệ thông tin chuyên về bảo mật,lập trình.Thích hợp cho những ai đam mê về công nghệ thông tin,tìm hiểu về bảo mật và lập trình.

[...]... socket using socket() 2 Establish a connection to the server using connect() 3 Communicate using send and recv() 4 Close the connection with close() TCPEchoClient4 .c is an implementation of a TCP echo client for IPv4 TCPEchoClient4 .c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include #include #include #include #include #include #include #include #include ... the connect() would fail at the client 3 Iteratively handle incoming connections: lines 39–59 Accept an incoming connection: lines 40–47 20 Chapter 2: Basic TCP Sockets As discussed above, a TCP socket on which listen() has been called is used differently than the one we saw in the client application Instead of sending and receiving on the socket, the server application calls accept(), which blocks... on the new sockets The server gets a socket for an incoming client connection by calling accept() int accept(int socket, struct sockaddr *clientAddress, socklen_t *addressLength) This function dequeues the next connection on the queue for socket If the queue is empty, accept() blocks until a connection request arrives When successful, accept() fills in the sockaddr structure pointed to by clientAddress,... TCP Client The distinction between client and server is important because each uses the sockets interface differently at certain steps in the communication We first focus on the client Its job is to initiate communication with a server that is passively waiting to be contacted 11 12 Chapter 2: Basic TCP Sockets The typical TCP client’s communication involves four basic steps: 1 Create a TCP socket using... receives data on the client connection and sends the same data back over to the client; it repeats this until the client closes its end of the connection, at which point no more data will be forthcoming TCPEchoServer4 .c 1 2 3 4 5 6 7 8 9 10 11 12 #include #include #include #include #include #include #include #include ... sockaddr _in clntAddr; // Client address // Set length of client address structure (in- out parameter) socklen_t clntAddrLen = sizeof(clntAddr); // Wait for a client to connect int clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntAddrLen); if (clntSock < 0) DieWithSystemMessage("accept() failed"); // clntSock is connected to a client! char clntName[INET_ADDRSTRLEN]; // String to contain... connections from clients; this is done by calling listen() on the socket 2.7 Handling Incoming Connections 29 int listen(int socket, int queueLimit) The listen() function causes internal state changes to the given socket, so that incoming TCP connection requests will be processed and then queued for acceptance by the program (Section 7.4 in Chapter 7 has more details about the life cycle of a TCP connection.)... basic TCP server communication: 1 Create a TCP socket using socket() 2 Assign a port number to the socket with bind() 3 Tell the system to allow connections to be made to that port, using listen() 4 Repeatedly do the following: • Call accept() to get a new socket for each client connection • Communicate with the client via that new socket using send() and recv() • Close the client connection using close()... incoming connection is made to the listening socket’s port number At that point, accept() returns a descriptor for a new socket, which is already connected to the initiating remote socket The second argument points to a sockaddr _in structure, and the third argument is a pointer to the length of that structure Upon success, the sockaddr _in contains the Internet address and port of the client to which... a connection with a server, we call connect() on the socket int connect(int socket, const struct sockaddr *foreignAddress, socklen_t addressLength) The first argument, socket, is the descriptor created by socket() foreignAddress is declared to be a pointer to a sockaddr because the Sockets API is generic; for our purposes, it will always be a pointer to either a sockaddr _in or sockaddr _in6 containing

Ngày đăng: 19/03/2014, 13:42

Từ khóa liên quan

Mục lục

  • Cover Page

  • Copyright Page

    • Copyright

    • Preface

      • Preface

      • 1 Introduction

        • 1 Introduction

          • Networks, Packets, and Protocols

          • About Addresses

            • Writing Down IP Addresses

            • Dealing with Two Versions

            • Port Numbers

            • Special Addresses

            • About Names

            • Clients and Servers

            • What Is a Socket?

            • 2 Basic TCP Sockets

              • 2 Basic TCP Sockets

                • IPv4 TCP Client

                • IPv4 TCP Server

                • Creating and Destroying Sockets

                • Specifying Addresses

                  • Generic Addresses

                  • IPv4 Addresses

                  • IPv6 Addresses

                  • Generic Address Storage

                  • Binary/String Address Conversion

                  • Getting a Socket's Associated Addresses

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan