Linux最簡單的網路通訊

來源:互聯網
上載者:User

標籤:linux   網路編程   socket   通訊   

伺服器端
/* socket server * 2014-12-15 CopyRight (c) arbboter */#include <unistd.h>#include <stdio.h>#include <stdlib.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(struct sockaddr)) == -1)    {        printf("bind socket failed\n");        return -1;    }    // server socket start list, waitting client to connect    // 這個client_max是指同時串連數    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        printf("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));        close(sockfd_client);    }    close(sockfd_server);    return 0;}

用戶端
/* socket client * 2014-12-15 CopyRight (c) arbboter */#include <unistd.h>#include <stdio.h>#include <stdlib.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;}


運行結果
# server output[[email protected]]$ g++ server.cpp -o server[[email protected]]$ ./serverwaitting for connected...recived connection from : 192.168.2.200waitting for connected...recived connection from : 192.168.2.200waitting for connected...recived connection from : 192.168.2.200waitting for connected...recived connection from : 192.168.2.200waitting for connected...# client output[[email protected]]$ g++ client.cpp -o client[[email protected]]$ ./clientrecived from server : Your magic number is : 5[[email protected]]$ ./clientrecived from server : Your magic number is : 43[[email protected]]$ ./clientrecived from server : Your magic number is : 88[[email protected]]$ ./clientrecived from server : Your magic number is : 28[[email protected]]$ 


Linux最簡單的網路通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.