1. Trang chủ
  2. » Công Nghệ Thông Tin

TCP/ IP sockets in c

196 1,6K 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 196
Dung lượng 1,28 MB

Nội dung

Đâ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