码迷,mamicode.com
首页 > 系统相关 > 详细

Linux最简单的网络通信

时间:2014-12-16 10:06:41      阅读:235      评论:0      收藏:0      [点我收藏+]

标签: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
[arbboter@arser]$ g++ server.cpp -o server
[arbboter@arser]$ ./server
waitting for connected...
recived connection from : 192.168.2.200
waitting for connected...
recived connection from : 192.168.2.200
waitting for connected...
recived connection from : 192.168.2.200
waitting for connected...
recived connection from : 192.168.2.200
waitting for connected...

# client output
[arbboter@arser]$ g++ client.cpp -o client
[arbboter@arser]$ ./client
recived from server : Your magic number is : 5
[arbboter@arser]$ ./client
recived from server : Your magic number is : 43
[arbboter@arser]$ ./client
recived from server : Your magic number is : 88
[arbboter@arser]$ ./client
recived from server : Your magic number is : 28
[arbboter@arser]$ 


Linux最简单的网络通信

标签:linux   网络编程   socket   通信   

原文地址:http://blog.csdn.net/arbboter/article/details/41955885

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!