Linux Network Programming-read data on the server cyclically

Source: Internet
Author: User
For more information about Linux network programming, see "read server data cyclically"-Linux general technology-Linux programming and kernel information. # Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define MAXBUF 10
************************ ********************
* Filename: simple-readall.c
* Purpose: demonstrate the most basic network programming. It cyclically reads the content sent from the server until it is read.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
Linux enthusiasts Linux knowledge disseminators sohowhose developers are best at C Language
* Date time: 2007-01-23 20:16:54
* Note: Anyone can copy the code and use these documents, including your commercial use.
* But follow the GPL
* Hope: more and more people are expected to contribute to the development of science and technology.
* Technology stands on the shoulders of giants and makes progress faster! Thank you for your contributions to the open source team!
**************************************** *****************************/
Int main (int argc, char ** argv)
{
Int sockfd, ret;
Struct sockaddr_in dest, mine;
Char buffer [MAXBUF + 1];

If (argc! = 5 ){
Printf
("Parameter format error! Correct usage: \ n \ t % s peer IP address peer port local IP address local port \ n \ t for example: \ t % s 127.0.0.1 80 \ n this program is used to receive messages of up to MAXBUF bytes from a port on the server with an IP address on a fixed port of the Local Machine ",
Argv [0], argv [0]);
Exit (0 );
}
/* Create a socket for tcp communication */
If (sockfd = socket (AF_INET, SOCK_STREAM, 0) <0 ){
Perror ("Socket ");
Exit (errno );
}

/* Initialize the address and port information of the server (PEER */
Bzero (& dest, sizeof (dest ));
Dest. sin_family = AF_INET;
Dest. sin_port = htons (atoi (argv [2]);
If (inet_aton (argv [1], (struct in_addr *) & dest. sin_addr.s_addr) = 0 ){
Perror (argv [1]);
Exit (errno );
}

/* Initialize your own address and port information */
Bzero (& mine, sizeof (mine ));
Mine. sin_family = AF_INET;
Mine. sin_port = htons (atoi (argv [4]);
If (inet_aton (argv [3], (struct in_addr *) & mine. sin_addr.s_addr) = 0 ){
Perror (argv [3]);
Exit (errno );
}

/* Bind your IP address and port to the socket */
If (bind (sockfd, (struct sockaddr *) & mine, sizeof (struct sockaddr) =-1 ){
Perror (argv [3]);
Exit (errno );
}

/* Connect to the server */
If (connect (sockfd, (struct sockaddr *) & dest, sizeof (dest ))! = 0 ){
Perror ("Connect ");
Exit (errno );
}

/* Receives the messages sent by the other party. A maximum of MAXBUF bytes can be received each time until all the messages sent by the other Party are received */
Do {
Bzero (buffer, MAXBUF + 1 );
Ret = recv (sockfd, buffer, MAXBUF, 0 );
Printf ("read % d bytes, they are: '% s' \ n", ret, buffer );
} While (ret = MAXBUF );

/* Close the connection */
Close (sockfd );
Return 0;
}

Run the following command to compile the program:
Gcc-Wall simple-readall.c

Run the following command to run the program:
./A. out 127.0.0.1 22 127.0.0.1 3000

The program running result is as follows:

Read 10 bytes, which are: 'ssh-2.0-op'
Read 10 bytes, which are: 'enssh _ 4.3p'
Read 10 bytes, they are: '2 Debian-5'
Read 8 bytes, they are: 'ubuntu1'
'

Note: If the program does not exit for a long time during running, set the first line of the program:
# Define MAXBUF 10
Change it slightly, for example, to the following:
# Define MAXBUF 9
Compile and run the program again.
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.