C++ socket demo

//client Linux 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

using namespace std;

int main()
{
    cout << "this is client" <<endl;

    int client = socket(AF_INET, SOCK_STREAM, 0);

    if(client == -1)
    {
        cout << " socket err " <<endl;
        return 0;
    }

    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(7637);
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if(connect(client, (struct sockaddr_in*) &serverAddr, sizeof(serverAddr)) < 0)
    {
        cout << "connect err" <<endl;
        return 0;
    }

    cout << "connect ...." <<endl;
    char data[255];
    char buf[255];

    while (true)
    {
        cin >> data;
        send(client, data, strlen(data), 0);   
        if(strcmp(data, "exit") == 0 || strcmp(data, "EXIT") == 0)
        {
            cout << "client disconnect ..." <<endl;
            break;
        } 

        memset(buf, 0 ,sizeof(buf));
        int len = recv(client, buf, sizeof(buf), 0);
        buf[len] = '\0';

        cout << buf << endl;
    }
    

    close(client);

    return 0;
}
//server linux 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

using namespace std;


int main()
{
    cout << "this is server" <<endl;
    int listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listenfd == -1)
    {
        cout << "socker err " <<endl;
        return 0;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(7637);
    addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(listenfd, (struct sockaddr*) &addr,sizeof(addr)) == -1)
    {
        cout << "bind err" <<endl;
        return 0;
    }


    if(listen(listenfd , 5) == -1)
    {
        cout <<"listen err" <<endl;
    }

    int conn;
    char clientIP[INET_ADDRSTRLEN] = "";
    struct sockaddr_in clientAddr;
    socklen_t clinetAddrLen = sizeof(clientAddr);
    while (true)
    {
        cout << "listen ...." <<endl;
        conn = accept(listenfd, (struct sockaddr*)&clientAddr, &clinetAddrLen);
        if(conn < 0)
        {
            cout <<"conn accept err " <<endl;
            continue;
        }

        inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);
        cout << " connect " << clientIP << ":" << ntohs(clientAddr.sin_port) << endl;

        char buf[255];
        while (true)
        {
            memset(buf, 0 , sizeof(buf));
            int len = recv(conn, buf, sizeof(buf), 0);
            buf[len] = '\0';
            if(strcmp(buf,"exit") == 0 || strcmp(buf,"EXIT") == 0)
            {
                cout << "disconnect " << clientIP << ":" <<ntohs(clientAddr.sin_port) << endl;
                break;
            }

            cout << buf << endl;

            send(conn, buf, len,0)
        }
        
        close(conn);


    }
    
    close(listenfd);
    return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值