127.0.0.1本地回环ip,用于本地测试,不会进行网络通信
TCP是面向连接的,服务器比较被动
需要服套接字监听 listen状态
正常通信默认会进行主机序列和网络序列的转换
TcpServer.cc
#pragma once#include<iostream>
#include<string>
#include<cstring>
#include<sys/types.h>
#include<sys/socket.h>
#include<cstdlib>
#include<wait.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
#include<signal.h>const int defaultfd=-1;
const std::string defaultip="0.0.0.0";
const int backlog=5;
class TcpServer;
class ThreadData
{
public:ThreadData(int fd,const std::string &ip,const uint16_t &p,TcpServer* tsvr):sockfd(fd),clientip(ip),clientport(p),tsvr_(tsvr){}
public:int sockfd;std::string clientip;uint16_t clientport;TcpServer* tsvr_;
};
class TcpServer
{
public:TcpServer(const uint16_t &port,const std::string &ip=defaultip):listensockfd_(defaultfd),port_(port),ip_(ip){}void InitServer(){listensockfd_=socket(AF_INET,SOCK_STREAM,0);if(listensockfd_<0){std::cout<<"create socket error"<<std::endl;}std::cout<<"create socket success"<<std::endl;struct sockaddr_in local;memset(&local,0,sizeof(local));local.sin_family=AF_INET;local.sin_port=htons(port_);inet_aton(ip_.c_str(),&(local.sin_addr));if(bind(listensockfd_,(struct sockaddr*)&local,sizeof(local))<0){std::cout<<"bind error"<<std::endl;}// TCP是面向连接的,服务器比较被动if(listen(listensockfd_,backlog)<0){std::cout<<"listen error"<<std::endl;}std::cout<<"listen success"<<std::endl;}static void* Routine(void* args){pthread_detach(pthread_self());ThreadData* td=static_cast<ThreadData*>(args);td->tsvr_->Service(td->sockfd,td->clientip,td->clientport);delete td;return nullptr;}void Start(){for(;;){//1.获取连接struct sockaddr_in client;socklen_t len=sizeof(client);// sock返的fd负责listen状态//accept返回的sockfd负责通信int sockfd=accept(listensockfd_,(struct sockaddr* )&client,&len);if(sockfd<0){std::cout<<"accept error"<<std::endl;}uint16_t clientport=ntohs(client.sin_port);char clientip[32];inet_ntop(AF_INET,&(client.sin_addr),clientip,sizeof(clientip));std::cout<<"get a net link... sockfd : "<< sockfd<<std::endl;sleep(1);// 根据新连接来通信// Service(sockfd, clientip,clientport);// close(sockfd);// 多进程版// pid_t id=fork();// if(id==0)// {// close(listensockfd_);// if(fork()>0) exit(0);// Service(sockfd, clientip,clientport); //孙子进程,system 领养// close(sockfd);// exit(0);// }// //father// close(sockfd);// // 关掉子进程 // pid_t rid=waitpid(id,nullptr,0);// (void)rid;// 多线程版本ThreadData*td=new ThreadData(sockfd,clientip,clientport,this);pthread_t tid;pthread_create(&tid,nullptr,Routine,nullptr);}}void Service(int sockfd,const std::string & clientip,const uint16_t &clientport){char buffer[4096];while(true){ssize_t n= read(sockfd,buffer,sizeof(buffer));if(n>0){buffer[n]=0;std::cout<<"client say# "<<buffer<<std::endl;std::string echo_string="tcpserver echo#";echo_string+=buffer;write(sockfd,echo_string.c_str(),echo_string.size());}else if(n==0){break;}else{std::cout<< "read error"<<std::endl;}}}~TcpServer(){}
private:int listensockfd_;uint16_t port_;std::string ip_;
};
TcpClient
#include<iostream>
#include<cstring>
#include<sys/types.h>
#include<sys/socket.h>
#include<cstdlib>
#include<arpa/inet.h>
#include<netinet/in.h>
#include <unistd.h>
void Usage(const std::string& proc)
{std::cout<<"\n\rUsage "<<proc<<" serveip serverport\n"<<std::endl;
}
int main(int argc,char*argv[])
{if(argc!=3){Usage(argv[0]);exit(1);}int sockfd=socket(AF_INET,SOCK_STREAM,0);std::string serverip=argv[1];uint16_t serverport=std::stoi(argv[2]);if(sockfd<0){std::cerr<<"sock error"<<std::endl;}struct sockaddr_in server;memset(&server,0,sizeof(server));server.sin_family=AF_INET;server.sin_port=htons(serverport);// tcp客户端要bind,// 客户端发起connect的时候,进行随机bindinet_pton(AF_INET,serverip.c_str(),&(server.sin_addr));int n=connect(sockfd,(struct sockaddr*)&server,sizeof(server));if(n<0){std::cerr<<"connect error..."<<std::endl;}std::string message;while(true){std::cout<<"Please Enter# ";std::getline(std::cin,message);write(sockfd,message.c_str(),message.size());char inbuffer[4096];int n=read(sockfd,inbuffer,sizeof(inbuffer));if(n>0){inbuffer[n]=0;std::cout<<inbuffer<<std::endl;}}close(sockfd);return 0;
}