TCP communication in Linux Network Programming

Source: Internet
Author: User
Tags htons

Client code

Client. cpp

# Include <stdlib. h> # include <sys/types. h> # include <netinet/in. h> # include <sys/socket. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdio. h> int main () {// create a socketint sock_fd = socket (af_inet, sock_stream, 0); // create the address struct sockaddr_in address; address. sin_family = af_inet; address. sin_port = htons (1991); // The port corresponds to the address of the server. sin_addr.s_addr = inet_addr ("127.0.0.1"); // start to connect to the server int result = connect (sock_fd, (struct sockaddr *) & Address, sizeof (Address )); if (result =-1) {perror ("Connect failed:"); exit (1);} Char CH = 'a '; // send a write (sock_fd, & Ch, 1); printf ("client says: % C \ n", CH) to the server ); // read one character read (sock_fd, & Ch, 1); printf ("Get char from server: % C \ n", CH ); // close (sock_fd); Return 0 ;}

Server

Server. cpp

# Include <stdlib. h> # include <sys/types. h> # include <netinet/in. h> # include <sys/socket. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdio. h> int main () {// use the sock_stream identifier to establish a TCP connection int sockfd = socket (af_inet, sock_stream, 0); struct sockaddr_in server_addr; server_addr.sin_family = af_inet; server_addr.sin_port = htons (1991); // you need to convert the byte order // you need to convert the 10th hexadecimal address/** name: inet_addr prototype: uint32_t inet_addr (const char * Name) Description: This function converts the IPv4 internet host address name from the standard numbers-and-dots notation into binary data. if the input is not valid, inet_addr returns inaddr_none. this is an obsolete interface to inet_aton, described immediately abve. it is obsolete because inaddr_none is a valid address (255.255.255.255), and inet_aton provides a cleaner way to indicate error return. */Counter = inet_addr ("127.0.0.1");/* bind */BIND (sockfd, (struct sockaddr *) & server_addr, sizeof (server_addr);/** Name: listen prototype: int listen (INT socket, int N) Description: The listen function enables the Socket socket to accept connections, thus making it a server socket. the argument n specifies the length of the queue for pending connections. when the queue fills, new clients attempting to connect fail with econnrefused until the server CILS accept to accept a connection from the queue. the listen function returns 0 on success and-1 on failure. the following errno error conditions are defined for this function: */Listen (sockfd, 10); char ch; struct sockaddr_in client_addr; socklen_t Len = sizeof (client_addr ); // socklen_t Len = 0; while (1) {int client_sockfd; printf ("server waiting: \ n"); client_sockfd = accept (sockfd, (struct sockaddr *) & client_addr, & Len); // read only one character read (client_sockfd, & Ch, 1); printf ("Get char from client: % C \ n", CH ); + + CH; // send the character + 1 back to the client for write (client_sockfd, & Ch, 1); close (client_sockfd );} // printf ("% 0x", server_addr.sin_addr.s_addr); Return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.