socket 3 草稿.

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket 3 草稿.相关的知识,希望对你有一定的参考价值。

发送服务端.

//遗留问题.

1)address的拼写.

2)客户端,读完数据,会阻塞在read函数.

 

client

#include <iostream>
#include <sys/socket.h>//{socket_type.h}:socket,AF_INET,SOCK_STREAM,PF_INET
#include <netdb.h>//{<netinet/in.h>}:IPPROTO_TCP
#include <sys/errno.h>
#include <string.h>
#include <stdio.h>//perror
#include <fcntl.h>
#include <unistd.h>//close.

//遗留问题.address的拼写.

using namespace std;
int main()
{
    //socket
    //address
    //conect
    int sock_client;
    sock_client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sock_client<0)
    {
        cout<<"error socket()"<<endl;
        return -1;
    }

    //标准sockaddr是16字节,不同协议有不同结构.所以各种结构,都有要定义一个数据sin_zero,用0补满16字节.
    //char addr[4]={127,0,0,1};
    //struct in_addr sin_addr=*((in_addr*)&addr);

     //‘2  0  0  13  127  0  1  1  0  0  0  0  0  0  0  0‘

    struct sockaddr_in sock_addr_server;
    char man[16]={2,0,10,124,127,0,1,1,0,0,0,0,0,0,0,0};
    sock_addr_server=*((sockaddr_in*)&man);
    //sock_addr_server.sin_family=AF_INET;//2byte
    //sock_addr_server.sin_port=htons(13);//2byte
    //sock_addr_server.sin_addr=sin_addr;//4byte
    //bzero(&sock_addr_server.sin_zero,8);//用0填充.8byte;


    int conn_status= connect(sock_client,(sockaddr*)&sock_addr_server,sizeof(sock_addr_server));

    if(conn_status!=0)
    {
        perror("connect");
        //cout<<"error connect()"<<endl;
        return -1;
    }

    char buf[51];
    int recv_status;
    while(1)
    {
        recv_status= read(sock_client,buf,50);
        cout<<recv_status<<endl;
        if(recv_status==0)
        {
            break;
        }
        else if(recv_status>0)
        {
            buf[recv_status]=\0;
            cout<<buf<<endl;
        }
        else
        {
            perror("recv()");
            break;
        }
    }


    close(sock_client);
}

 

 

server:

#include <iostream>
#include <sys/socket.h>//{socket_type.h}:socket,AF_INET,SOCK_STREAM,PF_INET
#include <netdb.h>//{<netinet/in.h>}:IPPROTO_TCP
#include <sys/errno.h>
#include <string.h>
#include <stdio.h>//perror
#include <fcntl.h>
#include <unistd.h>//close.
#include <thread>

//遗留问题.address的拼写.

using namespace std;

int g_cmd=0;
void backfun(int sock_server);
int main()
{
    //socket
    //address
    //conect
    int sock_server;
    sock_server=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sock_server<0)
    {
        cout<<"error socket()"<<endl;
        return -1;
    }

    //标准sockaddr是16字节,不同协议有不同结构.所以各种结构,都有要定义一个数据sin_zero,用0补满16字节.

    struct sockaddr_in sock_addr_server;
    char man[16]= {2,0,10,124,127,0,1,1,0,0,0,0,0,0,0,0};
    sock_addr_server=*((sockaddr_in*)&man);

    int status_bind= bind(sock_server, (sockaddr*)&sock_addr_server, sizeof(sock_addr_server));

    if(status_bind==-1)
    {
        perror("bind()");
        return -1;
    }

    status_bind= listen(sock_server, 10);
    if(status_bind==-1)
    {
        perror("listen()");
        return -1;
    }

    thread bg=thread(backfun,sock_server);
    bg.detach();

    int cmd;
    while(1)
    {
        cin>>cmd;
        if(cmd==88)
        {
            g_cmd=88;
            break;
        }

    }

    close(sock_server);
}


void backfun(int sock_server)
{
    while(g_cmd!=88)
    {
        int sockfd_client= accept(sock_server,NULL,NULL);
        if(sockfd_client==-1)
        {
            perror("accept()");
            //return -1;
        }
        else
        {
            char msg[]="hi i am server.dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd12345678904444444444444444444444444444444444444444444444441\0";
            send(sockfd_client,msg,sizeof(msg),0);
        }
    }
}

 

以上是关于socket 3 草稿.的主要内容,如果未能解决你的问题,请参考以下文章

golang代码片段(摘抄)

P_PY(草稿)

tcp ip 草稿。

python socket

100行代码,轻松搞定文本编辑器中草稿箱

谷歌让NLP模型也能debug,只要给一张「草稿纸」就行