首页 > 代码库 > socket 3 草稿.
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 草稿.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。