Linux C Socket Programming principle and simple example

Source: Internet
Author: User
Tags strcmp server port htons

Partly transferred from: http://goodcandle.cnblogs.com/archive/2005/12/10/294652.aspx

1. What is TCP/IP,UDP?

2. where is the socket?

3. What is a socket?

4. There are many frameworks, why are you still starting from the socket?

5. Linux C Socket Simple Example

1. What is TCP/IP ,UDP ?

  TCP/IP (transmission Control protocol/internet Protocol) is a protocol/inter-network protocol, an industry-standard set of protocols designed for wide area networks (WANs).
UDP (user Data Protocol, Subscriber Datagram Protocol) is the protocol that corresponds to TCP. It is a part of the TCP/IP protocol family.
The following diagram illustrates the relationship of these protocols.

2.Socket where is it?

3.Socket What is it?

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, thesocket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

  Façade mode, in its own words, is the system to provide a single interface to the outside world does not need to understand the internal implementation.

4. There are many frameworks, why are you still starting from the socket?

There are many cross-platform network programming frameworks, such as the ssh,c/c++ boost in Java.

There are many distributed frameworks, such as Hadoop.

My task is to make a C/C + + program distributed, the requirements of the environment, the basic belongs to pure computing, the results are very small. So the socket is selected.

It is important that sockets are the basis for distributed, cloud computing, and network programming, and that learning about sockets facilitates understanding of other frameworks.

Is the basic process of socket programming:

5.Linux C Socket Simple instance with detailed comment

The program is a simple "back-up", the client sends the console input information to the server side, the server returns information as is.

Server-side:

#include <sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<fcntl.h>#include<sys/shm.h>#defineMyPort 8887#defineQUEUE 20#defineBuffer_size 1024intMain () {///define SOCKFD    intSERVER_SOCKFD = socket (Af_inet,sock_stream,0); ///define SOCKADDR_IN    structsockaddr_in server_sockaddr; Server_sockaddr.sin_family=af_inet; Server_sockaddr.sin_port=htons (MyPort); Server_sockaddr.sin_addr.s_addr=htonl (Inaddr_any); ///BIND, successfully returned 0, error returned-1    if(Bind (SERVER_SOCKFD, (structSOCKADDR *) &server_sockaddr,sizeof(SERVER_SOCKADDR)) ==-1) {perror ("Bind"); Exit (1); }    ///Listen, successfully returned 0, error returned-1    if(Listen (server_sockfd,queue) = =-1) {perror ("Listen"); Exit (1); }    ///Client Sockets    CharBuffer[buffer_size]; structsockaddr_in client_addr; socklen_t length=sizeof(CLIENT_ADDR); ///successfully returned non-negative descriptor, error 1    intconn = Accept (SERVER_SOCKFD, (structsockaddr*) &client_addr, &length); if(conn<0) {perror ("Connect"); Exit (1); }     while(1) {memset (buffer,0,sizeof(buffer)); intLen = recv (conn, buffer,sizeof(buffer),0); if(strcmp (Buffer,"exit\n")==0)             Break;        Fputs (buffer, stdout); Send (conn, buffer, Len,0);    } close (conn);    Close (SERVER_SOCKFD); return 0;}
#include <sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<fcntl.h>#include<sys/shm.h>#defineMyPort 8887#defineBuffer_size 1024intMain () {///define SOCKFD    intSOCK_CLI = socket (Af_inet,sock_stream,0); ///define SOCKADDR_IN    structsockaddr_in servaddr; memset (&AMP;SERVADDR,0,sizeof(SERVADDR)); Servaddr.sin_family=af_inet; Servaddr.sin_port= Htons (MyPort);///Server PortSERVADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");///Server IP///connection server, successfully returned 0, error returned-1    if(Connect (SOCK_CLI,structSOCKADDR *) &servaddr,sizeof(SERVADDR)) <0) {perror ("Connect"); Exit (1); }    CharSendbuf[buffer_size]; CharRecvbuf[buffer_size];  while(Fgets (SendBuf,sizeof(SENDBUF), stdin)! =NULL) {Send (SOCK_CLI, SendBuf, strlen (sendbuf),0);///Send        if(strcmp (SendBuf,"exit\n")==0)             Break; Recv (SOCK_CLI, Recvbuf,sizeof(RECVBUF),0);///Receivefputs (Recvbuf, stdout); memset (SendBuf,0,sizeof(SENDBUF)); memset (Recvbuf,0,sizeof(RECVBUF));    } close (SOCK_CLI); return 0;}

Perform:

Client

Server-side

Ext.: http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html

Linux C Socket Programming principle and simple example

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.