From 608352b3662083ba6fcf3e4ec798340f64fafe5f Mon Sep 17 00:00:00 2001 From: Saturneic Date: Wed, 16 Jan 2019 19:32:15 +0800 Subject: [PATCH] Added. --- client.cpp | 9 +++++--- net.cpp | 8 +++---- net.h | 37 ++++++++++++++++++++++++-------- server.h | 62 ++++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 86 insertions(+), 30 deletions(-) diff --git a/client.cpp b/client.cpp index 7af00bd..18659b1 100644 --- a/client.cpp +++ b/client.cpp @@ -14,10 +14,13 @@ int main(int argc, char *argv[]) { try { - Socket client("127.0.0.1",9048,false,false); + Server client("127.0.0.1",9049,"127.0.0.1",9048); while (1) { - client.PacketSend("Hello"); - usleep(50000); + raw_data trdt; + trdt.setData("Hello"); + client.SignedRawdata(&trdt, "RSTR"); + client.SentRawdata(&trdt); + usleep(5000); } } catch (char const *str) { diff --git a/net.cpp b/net.cpp index b03f557..195dcb6 100644 --- a/net.cpp +++ b/net.cpp @@ -62,13 +62,13 @@ int main(void){ CPart::addArg(&args, 6); CPart::addArg(&args, 8.2); // 输入过程 - struct compute_result cpur = {"Test",&args,&args,&fargs,&fargs}; + /* struct compute_result cpur = {"Test",&args,&args,&fargs,&fargs}; packet pkt = srvr.CPURS2Packet(cpur); - raw_data rwd = srvr.Packet2Rawdata(pkt); + raw_data rwd = srvr.Packet2Rawdata(pkt);*/ // 输出过程 - srvr.Rawdata2Packet(rwd); - + //srvr.Rawdata2Packet(rwd); + srvr.Deamon(); while(1){ sleep(100); } diff --git a/net.h b/net.h index c996c4b..9660908 100644 --- a/net.h +++ b/net.h @@ -74,6 +74,12 @@ public: socklen_t size(void){ return len; } + void SetPort(int port){ + address.sin_port = htons(port); + } + void SetIP(string ip_addr){ + address.sin_addr.s_addr = inet_addr(ip_addr.data()); + } void setSize(void){ len = sizeof(address); } @@ -88,6 +94,8 @@ public: Addr addr; int nsfd,sfd,port; bool server,tcp,ipv4,set_fcntl = false; +// 缓冲区 + char buff[BUFSIZ]; void (*func)(class Socket &,int ,Addr); Socket(string ip_addr, int port, bool server = false, bool tcp = true, bool ipv4 = true){ if(ipv4) @@ -154,6 +162,19 @@ public: sendto(sfd, buff.data(), buff.size(), 0, addr.obj(), addr.size()); } +// 重新设置发送模式的端口 + void SetSendPort(int port){ + if(!server){ + addr.SetPort(port); + } + } +// 重新设置发送模式的IP地址 + void SetSendIP(string ip_addr){ + if(!server){ + addr.SetIP(ip_addr); + } + } + // 发送一段二进制信息 void PacketSendRAW(char *buff, unsigned long size){ if(!tcp) @@ -163,7 +184,6 @@ public: // 接受储存字符串信息的UDP包 int PacketRecv(Addr &t_addr, string &str){ if(!tcp){ - char buff[BUFSIZ]; ssize_t tlen; // 非阻塞输入 if(set_fcntl){ @@ -203,39 +223,38 @@ public: } // 接受储存二进制信息的UDP包 - ssize_t PacketRecvRAW(Addr &t_addr, char *p_rdt){ + ssize_t PacketRecvRAW(Addr &t_addr, char **p_rdt){ if(!tcp){ - char buff[BUFSIZ]; ssize_t tlen; // 非阻塞输入 if(set_fcntl){ tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); // 读取错误 if(tlen == -1 && errno != EAGAIN){ - p_rdt = nullptr; + *p_rdt = nullptr; return -1; } // 缓冲区没有信息 else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){ - p_rdt = nullptr; + *p_rdt = nullptr; return 0; } // 成功读取信息 else{ - p_rdt = (char *)malloc(tlen); - memcpy(p_rdt, buff, tlen); + *p_rdt = (char *)malloc(tlen); + memcpy(*p_rdt, buff, tlen); return tlen; } } else{ tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); if(~tlen){ - p_rdt = (char *)malloc(tlen); + *p_rdt = (char *)malloc(tlen); memcpy(p_rdt, buff, tlen); return tlen; } else{ - p_rdt = nullptr; + *p_rdt = nullptr; return -1; } diff --git a/server.h b/server.h index e9b33cf..bf96b6f 100644 --- a/server.h +++ b/server.h @@ -41,6 +41,13 @@ struct raw_data{ uint32_t info; char *msg = NULL; unsigned long msg_size = 0; + + void setData(string str){ + data = (char *)malloc(str.size()+1); + size = str.size()+1; + memcpy(data, str.data(),str.size()); + data[str.size()+1] = '\0'; + } }; //设置服务器守护程序的时钟 @@ -51,28 +58,44 @@ public: vector cpurs_in; vector packets_in; vector rawstr_in; - Socket socket; + Socket socket, send_socket; int packet_max = 30; - Server(string ip_addr):socket(ip_addr,9048,true,false){ + Server(string ip_addr, int port = 9048, string send_ip_addr = "127.0.0.1",int send_port = 9049):socket(ip_addr,port,true,false),send_socket(send_ip_addr,send_port,false,false){ socket.UDPSetFCNTL(); } + +// 重新设置服务器的发送端口 + void SetSendPort(int port){ + send_socket.SetSendPort(port); + } + +// 重新设置服务器的发送IP地址 + void SetSendIP(string ip_addr){ + send_socket.SetSendIP(ip_addr); + } + void Deamon(void){ //cout<<"Server Deamon Checked."< 0){ + cout<<"Get."< 0); + }while (tlen && prm-- > 0); setServerClock(this, 2); } // 将计算结果包转化为结构数据包 @@ -211,11 +234,11 @@ public: } // 为原始二进制串打上信息标签 - void RawdataAddInfo(struct raw_data *trdt,char info[]){ + void SignedRawdata(struct raw_data *trdt,string info){ // 填充标签信息 memcpy(&trdt->head, "NETC", sizeof(uint32_t)); memcpy(&trdt->tail, "CTEN", sizeof(uint32_t)); - memcpy(&trdt->head, info, sizeof(uint32_t)); + memcpy(&trdt->info, info.data(), sizeof(uint32_t)); // 整合信息 char *msg = (char *)malloc(sizeof(uint32_t) * 3 + trdt->size); trdt->msg_size = sizeof(uint32_t) * 3 + trdt->size; @@ -224,14 +247,15 @@ public: idx += sizeof(uint32_t); memcpy(idx, &trdt->info, sizeof(uint32_t)); idx += sizeof(uint32_t); - memcpy(idx, &trdt->data, trdt->size); + memcpy(idx, trdt->data, trdt->size); idx += trdt->size; memcpy(idx, &trdt->tail, sizeof(uint32_t)); + trdt->msg = msg; } // 发送已经打上标签的原始二进制串 int SentRawdata(struct raw_data *trdt){ - socket.PacketSendRAW(trdt->msg, trdt->msg_size); + send_socket.PacketSendRAW(trdt->msg, trdt->msg_size); return 0; } @@ -241,14 +265,24 @@ public: char *idx = p_rdt; memcpy(&head, "NETC", sizeof(uint32_t)); memcpy(&tail, "CTEN", sizeof(uint32_t)); - if(memcmp(idx, &head, sizeof(uint32_t))){ + if(!memcmp(idx, &head, sizeof(uint32_t))){ idx += size-sizeof(uint32_t); - if(memcmp(idx, &tail, sizeof(uint32_t))) return true; + if(!memcmp(idx, &tail, sizeof(uint32_t))) return true; else return false; } else return false; } +// 处理一个已打上标签的原始二进制串,获得其中储存的信息 + raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size){ + raw_data trdt; + trdt.data = (char *)malloc(size-3*sizeof(uint32_t)); + memcpy(&trdt.info, p_rdt+sizeof(uint32_t), sizeof(uint32_t)); + memcpy(trdt.data, p_rdt+sizeof(uint32_t)*2, size-3*sizeof(uint32_t)); + printf("Data:%s\n",trdt.data); + return trdt; + } + }; #endif /* server_h */