socket必备知识
github-30dayMakeCppServer
linux一切操作都是操作文件描述符
头文件--->
int sockfd = socket(AF_INET,SOCK_STREAM,0);
第一个参数:ip类型(AF_INET-->ipv4,AF_INET6--->ipv6)
第二个参数:TCP--->SOCK_STREAM
UDP--->SOCK_DGRAM
第三个参数:自动推导第二个参数类型
利用结构体sockaddr_in对ip,port,和family进行封装
头文件---
struct sock_addr_in ser;
bzero(&ser,sizeof(ser));//填充为0---头文件---
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
ser.sin_port = htons(10000);//host to newwork short(port为短整型int)
bind( sockfd, (sockaddr*)&ser, sizeof(ser));//绑定
listen(sockfd,maxnum);
maxnum ---> 服务器缓冲队列最大客户端数量
struct sockaddr_in cli;
socklen_t len = sizeof(cli);
bzero (&cli,sizeof(cli));//置空
int clifd = accept(sockfd,(sockaddr*)&cli,len);//客户端文件描述符
读写是针对 accept客户机文件描述符 的而不是 网络文件描述符
char buf[1024];
bzero(&buf,sizeof(buf));
ssize_t size = read(clifd,buf,sizeof(buf));//成功返回字节数,失败为-1
if(size > 0 ){
cout<<"客户机"<>buf;
write(clifd,buf,sizeof(buf));//服务器回传数据
}
else if(size == -1 ){
cout<<"socket出错"<
头文件--->
int sockfd = socket(AF_INET,SOCK_STREAM,0);
利用结构体sockaddr_in对ip,port,和family进行封装
头文件---
struct sock_addr_in cli;
bzero(&cli,sizeof(cli));//填充为0---头文件---
cli.sin_family = AF_INET;
cli.sin_addr.s_addr = inet_addr("127.0.0.1");
cli.sin_port = htons(10000);//host to newwork short(port为短整型int)
connect(sockfd,(sockaddr*)&cli,sizeof(cli));
char buf[1024];
bzero(&buf,sizeof(buf));
cin>>buf;
ssize_t size = write(sockfd,buf,sizeof(buf));
if(size == -1 ){
cout<<"写程序出错"< 0 ){
cout<<"服务器说:"<
#include
#include
#include
int main(){
//创建socket,网络文件描述符
int sockfd = socket(AF_INET,SOCK_STREAM,0);
//绑定ip和port【需要用结构体完成】
struct sockaddr_in ser;//创建结构体
bzero(&ser,sizeof(ser));
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
ser.sin_port = htons(10000);
//把储存在结构体里的ip和port和套接字进行绑定
bind(sockfd,(sockaddr*)&ser,sizeof(ser));
//监听端口[后者是同时进去的最大数量,如果大于就要进行等待]
listen(sockfd,100);
//接受
struct sockaddr_in cli;//创建结构体
socklen_t len = sizeof(cli);
bzero(&cli,sizeof(cli));
int clifd = accept(sockfd,(sockaddr*)&cli,&len);
/*
cout<<"new Client fd :" <
//读写数据
while(1){
char buf[1024];
bzero(&buf,sizeof(buf));
ssize_t size = read(clifd,buf,sizeof(buf));
if(size > 0 ){
cout<<"客户机"<<clifd<<"号说话了:"<<buf<<endl;
bzero(&buf,sizeof(buf));
cin>>buf;
write(clifd,buf,sizeof(buf));
}
else if(size == 0 ){
cout<<"客户机没有连接"<<endl;
close(clifd);
}
else if(size == -1 ){
cout<<"socket出错"<<endl;
close(clifd);
}
}
close(sockfd);
return 0;
}
#include
using namespace std;
#include
#include
#include
#include
int main(){
int sockfd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in cli;//创建结构体
bzero(&cli,sizeof(cli));
cli.sin_family = AF_INET;
cli.sin_addr.s_addr = inet_addr("127.0.0.1");
cli.sin_port = htons(10000);
connect(sockfd,(sockaddr*)&cli,sizeof(cli));
while(1){
//string str="";
char buf[1024];
bzero(&buf,sizeof(buf));
cin>>buf;
ssize_t size = write(sockfd,buf,sizeof(buf));
//cout<<"客户机说:"<
if(size == -1 ){
cout<<"写程序出错"<<endl;
}
bzero(&buf,sizeof(buf));
ssize_t read_size = read(sockfd,buf,sizeof(buf));
if(size > 0 ){
cout<<"服务器说:"<<buf<<endl;
}
else if(size == 0){
cout<<"服务器没有连接"<<endl;
break;
}
else if(size == -1){
cout<<"socket出错"<<endl;
close(sockfd);
}
}
close(sockfd);
return 0;
}