The simplest network communication for Linux

Source: Internet
Author: User
Tags htons

Server-Side
/* Socket Server * 2014-12-15 CopyRight (c) arbboter */#include <unistd.h> #include <stdio.h> #include <stdl ib.h> #include <time.h> #include <sys/socket.h> #include <sys/types.h> #include <netdb.h>#    Include <string.h> #include <arpa/inet.h>int main () {int sockfd_server;    int sockfd_client;    struct sockaddr_in addr_server;    struct sockaddr_in addr_client;    socklen_t Addr_len = 0;    int Client_max = 10;    int server_port = 33892;        Srand (Time (NULL));    Create Socket Sockfd_server = socket (af_inet, sock_stream, 0);        if (Sockfd_server = =-1) {printf ("init socket failed\n");    return-1;    }//Set address memset (&addr_server, 0, sizeof (addr_server));    addr_server.sin_family = af_inet;    ADDR_SERVER.SIN_ADDR.S_ADDR = htonl (Inaddr_any);        Addr_server.sin_port = htons (Server_port); Socket bind with Address if (bind (sockfd_server, struct sockaddr*) &addr_server, sizeof (structSOCKADDR)) = =-1) {printf ("bind socket failed\n");    return-1; }//server socket start list, waitting client to connect//This client_max refers to the simultaneous connection number if (Listen (Sockfd_server, Client_        max) = =-1) {printf ("Start listen socket failed\n");    return-1;    } char szmsg[128] = {0};        while (1) {addr_len = sizeof (struct sockaddr_in);                printf ("Waitting for connected...\n");         Waitting for Connected sockfd_client = Accept (Sockfd_server, (struct sockaddr*) &addr_client, &addr_len);            if (sockfd_client = =-1) {printf ("Accept socket failed\n");        return-1; }//Get client IP and send a magic number to the client,//Then close this connection Prin        TF ("recived connection from:%s\n", Inet_ntoa (ADDR_CLIENT.SIN_ADDR));        sprintf (szmsg, "Your magic number is:%d", rand ()%100);        Write (Sockfd_client, szmsg, strlen (szmsg)); CloseSockfd_client);    } close (Sockfd_server); return 0;}

Client
/* Socket client * 2014-12-15 CopyRight (c) arbboter */#include <unistd.h> #include <stdio.h> #include <stdl ib.h> #include <sys/socket.h> #include <sys/types.h> #include <netdb.h> #include <string.h>    #include <arpa/inet.h>int main () {int sockfd_server;    struct sockaddr_in addr_server;    socklen_t Addr_len = 0;    int server_port = 33891;    char* server_addr = "192.168.2.200";    Create Socket Sockfd_server = socket (af_inet, sock_stream, 0);        if (Sockfd_server = =-1) {printf ("init socket failed\n");    return-1;    }//Set server address memset (&addr_server, 0, sizeof (addr_server));    addr_server.sin_family = af_inet;    ADDR_SERVER.SIN_ADDR.S_ADDR = inet_addr (server_addr);;        Addr_server.sin_port = htons (Server_port);        Connect server if (connect (sockfd_server, (struct sockaddr *) &addr_server,sizeof (struct sockaddr)) ==-1) {        printf ("Connect server failed\n"); return-1;   } char szbuf[512] = {0};        int nread = 0;    recived data from server and print it nread = Read (Sockfd_server, szbuf, sizeof (SZBUF));            printf ("recived from server:%s\n", szbuf);    Close (Sockfd_server); return 0;}


Run Results


The simplest network communication for Linux

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.