socket实现的http连接,可以通信

最近想研究一下linux下http服务器的问题,今天验证了一下socket连接在不主动断的情况下是一直连着的。

在linux下用socket写的服务器和客户端程序,可以相互通信,没有经过缜密的设计,只能在第一次连接时生效;

之后打算研究一下服务器的线程框架,让服务器能提供稳定服务。

// prot.h

#define PORT 3333
// server.cpp
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <string>
#include "port.h"

using std::cout;
using std::endl;
using std::cin;
using std::string;

int main()
{
    struct sockaddr_in sin;
    struct sockaddr_in cin;
    int serfd;
    int clifd;

    serfd=socket(AF_INET,SOCK_STREAM,0);
    if(serfd<0)
    {
        cout<<"error when socket()"<<endl;
        exit(1);
    }
    sin.sin_family=AF_INET;
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_port=htons(PORT);
    int Setit=1;
    setsockopt(serfd,SOL_SOCKET,SO_REUSEADDR,
       (const char*)&Setit,sizeof(Setit)); 
    if(bind(serfd, (struct sockaddr*)&sin, sizeof(sin))==-1)
    {
        cout<<"error when bind()"<<endl;
        exit(1);
    }
    listen(serfd,10);
    cout<<"waiting for accepting connection from client"<<endl;
    int size=sizeof(cin);
    while(1)
    {
        clifd=accept(serfd, (struct sockaddr*)&cin, (socklen_t*)&size);
        if(clifd>0)
        {
            int son=fork();
            if(son==0)      // son
            {
                string str;
                while(std::cin>>str)
                {
                    send(clifd, str.c_str(), str.length(), 0);
                }
            }
            else
            {
                string str;
                char get_buf[333];
                while(1)
                {
                    bzero(get_buf, sizeof(get_buf));
                    if(recv(clifd,get_buf,333,0))
                    {
                        str=get_buf;
                        cout<<"get:"<<str<<endl;
                    }
                }
            }
        }
    }
    close(clifd);
    close(serfd);
}
// client.cpp
#include <iostream>
#include <sys/socket.h>
#include <netdb.h>
#include <string>
#include "port.h"

using std::cout;
using std::endl;
using std::string;
using std::cin;

char* host_name="127.0.0.1";
int port=PORT;

int main()
{
    int serfd;
    struct sockaddr_in sin;
    char * message_str="hello,network";

    sin.sin_family=AF_INET;
    sin.sin_addr.s_addr=htonl(INADDR_ANY);
    sin.sin_port=htons(port);
    if((serfd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
        cout<<"error socket()"<<endl;
        exit(0);
    }
    if(connect(serfd,(sockaddr*)&sin,sizeof(sin))<0)
    {
        cout<<"error connect()"<<endl;
        exit(0);
    }
    int son=fork();
    if(son==0)  // son
    {
        string str;
        while(cin>>str)
        {
            send(serfd, str.c_str(), str.length(), 0);
        }
    }
    else        // father
    {
        string str;
        char get_buf[333];
        while(1)
        {
            bzero(get_buf, sizeof(get_buf));
            if(recv(serfd, get_buf, 333, 0))
            {
                str=get_buf;
                cout<<"get:"<<str<<endl;
            }
        }
    }
    close(serfd);
}

原文地址:https://www.cnblogs.com/zhidian314/p/3488041.html