Getting Started with Linux socket programming--hello World

Source: Internet
Author: User
Tags htons

The following code is the entry code for Linux socket programming. It is divided into server and client source.

The main process of server-side code is to bind the IP address and port number to establish the socket, waiting for the client to initiate the access. After accepting the client request, send the string "Hello World" to the client, close the socket, and end the program.

The main process of client code is to initiate a request to the socket corresponding to the server, read the data sent by the server, and print it out.

The code has been commented in detail and more details are not mentioned.

Server.cpp

#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/socket.h>#include<netinet/inch.h>#include<string.h>intMain () {//Creating Sockets    /** Af_inet is a address family that's used to designate * the type of addresses that your socket can communicate     * WITH (on this case, Internet Protocol v4 addresses).  * When your create a socket, you had to specify it address family, * and then you can only use addresses of the that type     With the socket.  * The Linux kernel, for example, supports in other address families * such as UNIX (Af_unix) sockets and IPX (AF_IPX), and also communications with IRDA * and Bluetooth
* (Af_irda and Af_bluetooth, but it's doubtful you'll use these at such a). * For the most part, sticking with af_inet for sockets programming over * A network is the safest option. * There is also af_inet6 for Internet Protocol V6 addresses*/ /** Sock_stream * provides sequenced, reliable, two-way, connection-* based byte streams. An Out-of-band data transmission * mechanism is supported. */ //IPPROTO_TCP using the TCP protocol intServ_sock =socket (AF_INET,SOCK_STREAM,IPPROTO_TCP); //To bind a socket and IP port number structsockaddr_in serv_addr; //Initialize the structure body serv_addrmemset (&AMP;SERV_ADDR,0,sizeof(SERV_ADDR)); //Use IPv4 addressserv_addr.sin_family=af_inet; //Set IP addressSERV_ADDR.SIN_ADDR.S_ADDR=INET_ADDR ("127.0.0.1"); //set the port numberServ_addr.sin_port =htons (2896); //The protocol port number and IP address of the socket are stored in the binding structure of the socket and struct bodyBind (Serv_sock, (structsockaddr*) &serv_addr,sizeof(SERV_ADDR)); //Enter the listening state, waiting for the user to initiate the request//enters passive listening state, the socket is asleep until the client initiates the request to wake up againListen (Serv_sock, -); //the client requests the corresponding socket structure structsockaddr_in client_addr; //the size of the client request socket structure Bodysocklen_t client_addr_size =sizeof(CLIENT_ADDR); //request to accept the client intClient_sock =accept (Serv_sock, (structSOCKADDR *) &client_addr,&client_addr_size); Charstr[]="Hello World"; //sending data to the client//writing data to a client socketWrite (Client_sock,str,sizeof(str)); //Close SocketClose (Client_sock); Close (Serv_sock); return 0;}

Client.cpp

////Created by fit on 16-5-19.//#include<stdio.h>#include<cstring>#include<stdlib.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/socket.h>intMain () {/** This constant has the value 0.
* It ' s actually an automatic choice depending on socket type and family. * If you use it, and if the socket type is sock_stream and the family are af_inet, * then the protocol would automatical Ly is TCP (exactly the same as if you ' d used ipproto_tcp). * Buf if ipproto_ip together with Af_inet and Sock_raw, you'll have an error, * because the kernel cannot ch Oose a protocol automatically in this case. */ intSock =socket (AF_INET,SOCK_STREAM,IPPROTO_IP); structsockaddr_in serv_addr; memset (&AMP;SERV_ADDR,0,sizeof(SERV_ADDR)); Serv_addr.sin_family=af_inet; Serv_addr.sin_addr.s_addr= Inet_addr ("127.0.0.1"); Serv_addr.sin_port= Htons (2896); //A request is made to the client via the Connect function, and the server's socket information is stored in the struct SERV_ADDRConnect (sock, (structsockaddr*) &serv_addr,sizeof(SERV_ADDR)); Charbuffer[ +]; //reading data from a socket via readRead (Sock,buffer,sizeof(buffer)-1); printf ("message from server:%s\n", buffer); //Close SocketClose (sock); return 0;}

Reference:

    1. Stack_overflow
    2. Man7
    3. C Language Chinese Network

  

Getting Started with Linux socket programming--hello World

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.