Linux TCP/IP-simple sending and receiving (1)

Source: Internet
Author: User

Now, in the network society, I don't know how to use TCP/IP for Linux. I feel empty. So you must study this stuff carefully.

Tutorial objective: to implement client sending and server receiving

============= Server ==============================

# Include <stdio. h> # include <stdlib. h> # include <errno. h> # include <string. h> # include <netdb. h> # include <sys/types. h> # include <netinet/in. h> # include <sys/socket. h> # Define Port 3333int main (INT argc, char * argv []) {int sockfd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size; char Buf [100]; sockfd = socket (af_inet, sock_stream, 0); // create a socket If (sockfd =-1) {fprintf (stderr, "socket Error: % S \ n ", strerror (errno); exit (1);} bzero (& server_addr, sizeof (struct sockaddr_in )); // set the first n Bytes of the byte string s to zero and include '\ 0' server _ ADDR. sin_family = af_inet; bytes = htonl (inaddr_any); // any IP address can be connected. I am an open drop server_addr.sin_port = htons (port); int on = 1; setsockopt (sockfd, sol_socket, so_reuseaddr, & on, sizeof (on); // you can remove this function and try if (BIND (sockfd, (struct sockaddr *) (& server_addr ), sizeof (struct sockaddr_in) =-1 )/ /Bind {fprintf (stderr, "BIND error: % s \ n", strerror (errno); exit (1) ;}if (Listen (sockfd, 20) =-1) // listen for 20 more {fprintf (stderr, "Listen error: % s \ n", strerror (errno); exit (1 );} int new_fd; pid_t child; int r_size; int w_size; while (1) {printf ("Wait !!!!! \ N "); memset (BUF, 0,100); sin_size = sizeof (struct sockaddr_in); If (new_fd = accept (sockfd, (struct sockaddr *) (& client_addr ), & sin_size) =-1) // receives the message to check whether someone is connected to {fprintf (stderr, "Accept error: % s \ n", strerror (errno )); exit (1);} fprintf (stderr, "server get connection from % s \ n", inet_ntoa (client_addr.sin_addr); If (r_size = read (new_fd, Buf, 100) =-1) // read data {fprintf (stderr, "read error: % s \ n", strerror (errno); exit (1 );} printf ("received information % d: % s \ n", r_size, Buf);} Close (sockfd); exit (0 );}

============= Client =============================

# Include <stdlib. h> # include <stdio. h> # include <errno. h> # include <string. h> # include <netdb. h> # include <sys/types. h> # include <netinet/in. h> # include <sys/socket. h> # define PORT 3333int main (int argc, char * argv []) {int sockfd; char buf [100]; struct sockaddr_in server_addr; struct hostent * host; sockfd = socket (AF_INET, SOCK_STREAM, 0); // create a Socket if (sockfd =-1) {fprintf (stderr, "socket error: % s \ n ", strerror (errno); exit (1);} bzero (& server_addr, sizeof (server_addr )); // set the first n Bytes of the byte string s to zero and include '\ 0' server _ addr. sin_family = AF_INET; server_addr.sin_port = htons (PORT); region = inet_addr ("192.168.1.155"); // ip address is the server ipint con_flag to be connected; con_flag = connect (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr); // Connect if (con_flag =-1) {fprintf (stderr, "Connect Error: % s \ a \ n ", strerror (errno); exit (1);} printf (" Please input some words: "); fgets (buf, 100, stdin ); if (write (sockfd, buf, sizeof (buf) <0) // write data to the socket interface {fprintf (stderr, "write Error: % s \ a \ n ", strerror (errno); exit (1) ;}close (sockfd); // disable exit (0 );}

The experiment results are as follows:

Root @ WYZ-vlinux :~ /Test/SC #./tcp_server
Server GET connection from 192.168.1.155
Server received Hello mama

Server GET connection from 192.168.1.155
Server deployed ed I love Linux

Root @ WYZ-vlinux :~ /Test #./tcp_client
Please input some words: Hello mama
Root @ WYZ-vlinux :~ /Test #./tcp_client
Please input some words: I love Linux
Root @ WYZ-vlinux :~ /Test #

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.