c++ linux socket编程 c++网络编程

声明:大部分代码来自这篇博客http://www.cnblogs.com/diligenceday/p/6241021.html, 感谢博主

 思路:

思路很重要呦~~~

socket详细信息,思路:http://www.cnblogs.com/renfanzi/p/5713054.html

基础知识 

 

一个简单的socket服务端和客户端

 

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <typeinfo>

#define PORT 7000
#define QUEUE 20
int conn;

using namespace std;

int main() {
    //实例化socket对象:
    // AF_INET为地址簇,默认IPV4;
    // SOCK_STREAM为类型,默认为tcp
    // 0为协议,默认自动选择合适协议
    int ss = socket(AF_INET, SOCK_STREAM, 0);
    // 创建一个struct
    struct sockaddr_in server_sockaddr;
    //结构体加入参数:端口和地址
    server_sockaddr.sin_family = AF_INET;
    server_sockaddr.sin_port = htons(PORT);
    server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    //bind()
    if (bind(ss, (struct sockaddr *) &server_sockaddr, sizeof(server_sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }
    //listen()
    if (listen(ss, QUEUE) == -1) {
        perror("listen");
        exit(1);
    }

    struct sockaddr_in client_addr;
    socklen_t length = sizeof(client_addr);
    
    //accept()
    //成功返回非负描述字,出错返回-1
    conn = accept(ss, (struct sockaddr *) &client_addr, &length);
    
    if (conn < 0) {
        perror("connect");
        exit(1);
    }

    char buffer[1024];
    while (1) {
        memset(buffer, 0, sizeof(buffer));
        
        //read()
        int len = recv(conn, buffer, sizeof(buffer), 0);
        if (strcmp(buffer, "exit\n") == 0) break;
        printf("buffer: %s", buffer);
        //send()
        //必须要有返回数据, 这样才算一个完整的请求
        send(conn, buffer, len, 0);
    }
    close(conn);
    close(ss);
    return 0;
}
simple server

 

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>

#define MYPORT  7000
#define BUFFER_SIZE 1024

int main() {
    ///定义sockfd
    int sock_cli = socket(AF_INET, SOCK_STREAM, 0);

    ///定义sockaddr_in
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(MYPORT);  ///服务器端口
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  ///服务器ip

    //连接服务器,成功返回0,错误返回-1
    if (connect(sock_cli, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
        perror("connect");
        exit(1);
    }

    char sendbuf[BUFFER_SIZE];
    char recvbuf[BUFFER_SIZE];
    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) {
        send(sock_cli, sendbuf, strlen(sendbuf), 0); ///发送
        if (strcmp(sendbuf, "exit\n") == 0)
            break;
        recv(sock_cli, recvbuf, sizeof(recvbuf), 0); ///接收
        fputs(recvbuf, stdout);

        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
    }

    close(sock_cli);
    return 0;
}
simple client

 

还没写完呢,下一步,1对多,多线程,异步,开源库,框架,等等...

 

posted @ 2019-01-10 11:22  我当道士那儿些年  阅读(1869)  评论(0编辑  收藏  举报