springboot常用版本(springboot用哪个版本)
267
2022-10-30
UDP畅聊系统简单版本
带有数据存储的模型图
总体的框架
server中
udp_server.h
其中add_user函数为添加新上线用户信息
1 #pragma once
2 #include
udp_server.cpp
1 #include "udp_server.h"
2 #include "comm.h"
3 #include "data_pool.h"
4
5 udp_server::udp_server(const std::string &default_ip,const short &default_po rt)
6 :_ip(default_ip)
7 ,_port(default_port)
8 ,_sock(-1)
9 {}
10
11 udp_server::~udp_server()
12 {
13 if(_sock > 0)
14 close(_sock);
15 }
16
17 void udp_server::init()
18 {
19 _sock=socket(AF_INET,SOCK_DGRAM,0);
20 if(_sock <0)
21 {
22 print_log(strerror(errno),__FUNCTION__,__LINE__);
23 exit(1);
24 }
25 struct sockaddr_in local;
26 socklen_t len=sizeof(local);
27 local.sin_family=AF_INET;
28 local.sin_port=htons(_port);
29 if(_ip.c_str()=="any")
30 {
31 local.sin_addr.s_addr=htonl(INADDR_ANY);
32 }
33 else
34 local.sin_addr.s_addr=inet_addr(_ip.c_str());
35 if(bind(_sock,(struct sockaddr*)&local,len)<0)
36 {
37 print_log(strerror(errno),__FUNCTION__,__LINE__);
38 exit(2);
39 }
40 }
41
42 int udp_server::recv_data()
43 {
44 std::string _out;
45 struct sockaddr_in client;
46 socklen_t len=sizeof(client);
47 char buf[_SIZE_];
48 buf[0]='\0';
49 ssize_t _size=recvfrom(_sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&clie nt,&len);
50 if(_size < 0)
51 {
52 print_log(strerror(errno),__FUNCTION__,__LINE__);
53 }
54 else if(_size == 0)
55 {
56 const char *msg="client closed...";
57 print_log(msg,__FUNCTION__,__LINE__);
58 }
59 else
60 {
61 buf[_size]='\0';
62 _out=buf;
63 string key_ip=inet_ntoa(client.sin_addr);
64 print_log(key_ip.c_str(),__FUNCTION__,__LINE__);
65 add_user(key_ip,client);
66 _pool_data.put_data(_out);
67 }
68 return _size;
69 }
70
71 void udp_server::add_user(string &key_ip,struct sockaddr_in &client)
72 {
73 std::map
server_main.cpp中,两个线程处理函数分别处理接收与广播数据
1 #include "udp_server.h"
2
3 void Usage(const char* proc)
4 {
5 cout<<"Usage:"< client中进行发送消息和接收消息 udp_client.h 1 #pragma once
2
3 #include udp_client.cpp 1 #include "udp_client.h"
2 #include "comm.h"
3
4 udp_client::udp_client(const string &default_ip,const short &default_port)
5 :_ip(default_ip)
6 ,_port(default_port)
7 ,_sock(-1)
8 {}
9
10 udp_client::~udp_client()
11 {
12 if(_sock > 0)
13 close(_sock);
14 }
15
16
17 void udp_client::init()
18 {
19 _sock=socket(AF_INET,SOCK_DGRAM,0);
20 if(_sock <0)
21 {
22 print_log(strerror(errno),__FUNCTION__,__LINE__);
23 exit(1);
24 }
25 }
26
27 int udp_client::recv_data(string &_out)
28 {
29 struct sockaddr_in server;
30 socklen_t len=sizeof(server);
31 char buf[_SIZE_];
32 buf[0]='\0';
33 ssize_t _size=recvfrom(_sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&ser ver,&len);
34 if(_size < 0)
35 {
36 print_log(strerror(errno),__FUNCTION__,__LINE__);
37 }
38 else if(_size == 0)
39 {
40 const char *msg="server closed...";
41 print_log(msg,__FUNCTION__,__LINE__);
42 }
43 else
44 {
45 buf[_size]='\0';
46 _out=buf;
47 }
48 return _size;
49 }
50
51 int udp_client::send_data(string& _in)
52 {
53 struct sockaddr_in server;
54 server.sin_family=AF_INET;
55 server.sin_port=htons(_port);
56 server.sin_addr.s_addr=inet_addr(_ip.c_str());
57 socklen_t len=sizeof(server);
58 ssize_t _size=sendto(_sock,_in.c_str(),_in.size(),0,(struct sockaddr*)& server,len);
59 if(_size < 0)
60 {
61 print_log(strerror(errno),__FUNCTION__,__LINE__);
62 }
63 return _size;
64 } client_main.cpp 1 #include "udp_client.h"
2 #include "comm.h"
3
4 void Usage(const char* proc)
5 {
6 assert(proc);
7 cout<<"Usage:"< Makefile 1 ROOT_PATH=$(shell pwd)
2 SERVER=$(ROOT_PATH)/server
3 CLIENT=$(ROOT_PATH)/client
4 COMM=$(ROOT_PATH)/comm
5 DATA_POOL=$(ROOT_PATH)/data_pool
8 SERVER_BIN=udp_server
9 CLIENT_BIN=udp_client
10
11
12 INCLUDE=-I$(COMM) -I$(DATA_POOL)
13 INCLUDE+=-I$(DATA_POOL)
14 INCLUDE+=-I$(ROOT_PATH)/lib/include
15
16 CC=g++
17 FLAGS=
18 LDFLAGS=-lpthread #-static
19
20 SER_SRC=$(shell ls -R $(SERVER) |grep -E '*.cpp')
21 SER_SRC+=$(shell ls -R $(COMM) |grep -E '*.cpp')
22 SER_SRC+=$(shell ls -R $(DATA_POOL) |grep -E '*.cpp')
23 SER_OBJ=$(SER_SRC:.cpp=.o)
25 CLI_SRC=$(shell ls -R $(CLIENT) |grep -E '*.cpp')
26 CLI_SRC+=$(shell ls -R $(COMM) |grep -E '*.cpp')
27 CLI_OBJ=$(CLI_SRC:.cpp=.o)
28
29 .PHONY:all
30 all:$(SERVER_BIN) $(CLIENT_BIN)
31 $(SERVER_BIN):$(SER_OBJ)
32 @$(CC) -o $@ $^ $(LDFLAGS)
33 @echo "Linking [ $^ ] to [ $@ ] ...done"
34 $(CLIENT_BIN):$(CLI_OBJ)
35 @$(CC) -o $@ $^ $(LDFLAGS)
36 @echo "Linking [ $^ ] to [ $@ ] ...done"
37 %.o:$(COMM)/%.cpp
38 @$(CC) -c $< $(INCLUDE)
39 @echo "Compling [ $(shell basename $<) ] to [ $@ ] ...done"
40 %.o:$(DATA_POOL)/%.cpp
41 @$(CC) -c $< $(INCLUDE)
42 @echo "Compling [ $(shell basename $<) ] to [ $@ ] ...done"
43 %.o:$(SERVER)/%.cpp
44 @$(CC) -c $< $(INCLUDE)
45 @echo "Compling [ $(shell basename $<) ] to [ $@ ] ...done"
46 %.o:$(CLIENT)/%.cpp
47 @$(CC) -c $< $(INCLUDE)
48 @echo "Compling [ $(shell basename $<) ] to [ $@ ] ...done"
49
62 .PHONY:clean
63 clean:
64 @rm -f *.o $(SERVER_BIN) $(CLIENT_BIN)
65 @echo "cleaning done..."
66
67 .PHONY:debug
68 debug:
69 @echo $(SER_SRC)
70 @echo $(CLI_SRC)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~