所需E币: 0
时间: 2023-12-25 11:06
大小: 3.48KB
LinuxSocket网络编程框架主要由3大模块组成:BSDSocketAPIsSocketAbstractionLayerVFSLayerTCP/IP协议在设计和实现上并没有客户端和服务器的概念,在通信过程中所有机器都是对等的。但由于资源(视频、新闻、软件等)都被数据提供者所垄断,所以几乎所有的网络应用程序都很自然地用了客户端/服务器模型,即所有客户端都通过访问服务器来获取所需的资源。BS和CS服务器架构(1)CS架构介绍(clientserver,客户端服务器架构)(2)BS架构介绍(broswerserver,浏览器服务器架构)TCP协议(1)建立连接需要三次握手(2)建立连接的条件:服务器listen时客户端主动发起connect(3)关闭连接需要四次握手(4)服务器或者客户端都可以主动发起关闭packagecom.example.emos.wx.controller.form;importio.swagger.annotations.ApiModel;importlombok.Data;importjavax.validation.constraints.NotBlank;importjavax.validation.constraints.Pattern;@Data@ApiModelpublicclassRegisterForm{ @NotBlank(message="注册码不能为空") @Pattern(regexp="^[0-9]{6}$",message="注册码必须是6位数字") privateStringregisterCode; @NotBlank(message="微信临时授权不能为空") privateStringcode; @NotBlank(message="昵称不能为空") privateStringnickname; @NotBlank(message="头像不能为空") privateStringphoto;}在UserController.java中创建login()方法。@PostMapping("/login")@ApiOperation("登陆系统")publicRlogin(@Valid@RequestBodyLoginFormform){intid=userService.login(form.getCode()); Stringtoken=jwtUtil.createToken(id); Set<String>permsSet=userService.searchUserPermissions(id); saveCacheToken(token,id); returnR.ok("登陆成功").put("token",token).put("permission",permsSet);}在CheckinServiceImpl类中,实现抽象方法……publicclassCheckinServiceImplimplementsCheckinService{……publicvoidcreateFaceModel(intuserId,Stringpath){ HttpRequestrequest=HttpUtil.createPost(createFaceModelUrl); request.form("photo",FileUtil.file(path)); HttpResponseresponse=request.execute(); Stringbody=response.body(); if("无法识别出人脸".equals(body)||"照片中存在多张人脸".equals(body)){ thrownewEmosException(body); }else{ TbFaceModelentity=newTbFaceModel(); entity.setUserId(userId); entity.setFaceModel(body); faceModelDao.insert(entity); } }}在CheckinServiceImpl.java类中,实现三个抽象方法。publicclassCheckinServiceImplimplementsCheckinService{……@Override publicHashMapsearchTodayCheckin(intuserId){ HashMapmap=checkinDao.searchTodayCheckin(userId); returnmap; } @Override publiclongsearchCheckinDays(intuserId){ longdays=checkinDao.searchCheckinDays(userId); returndays; } @Override publicArrayList<HashMap>searchWeekCheckin(HashMapparam){ ArrayList<HashMap>checkinList=checkinDao.searchWeekCheckin(param); ArrayList<String>holidaysList=holidaysDao.searchHolidaysInRange(param); ArrayList<String>workdayList=workdayDao.searchWorkdayInRange(param); DateTimestartDate=DateUtil.parseDate(param.get("startDate").toString()); DateTimeendDate=DateUtil.parseDate(param.get("endDate").toString()); DateRangerange=DateUtil.range(startDate,endDate,DateField.DAY_OF_MONTH); ArrayListlist=newArrayList(); range.forEach(one->{ Stringdate=one.toString("yyyy-MM-dd"); //查看今天是不是假期或者工作日 Stringtype="工作日"; if(one.isWeekend()){ type="节假日"; } if(holidaysList!=null&&holidaysList.contains(date)){ type="节假日"; }elseif(workdayList!=null&&workdayList.contains(date)){ type="工作日"; } Stringstatus=""; if(type.equals("工作日")&&DateUtil.compare(one,DateUtil.date())<=0){ status="缺勤";booleanflag=false; for(HashMap<String,String>map:checkinList){ if(map.containsValue(date)){ status=map.get("status");flag=true; break; } }DateTimeendTime=DateUtil.parse(DateUtil.today()+""+constants.attendanceEndTime);Stringtoday=DateUtil.today();if(date.equals(today)&&DateUtil.date().isBefore(endTime)&&flag==false){ status=""; } } HashMapmap=newHashMap(); map.put("date",date); map.put("status",status); map.put("type",type); map.put("day",one.dayOfWeekEnum().toChinese("周")); list.add(map); }); returnlist; }}在EmosWxApiApplicationTests.java类中提供了contextLoads()测试用例方法,我们把生成大量系统消息记录的代码写在其中,程序运行的时候这些消息记录就会写入到MongoDB里面。@SpringBootTestclassEmosWxApiApplicationTests{ @Autowired privateMessageServicemessageService; @Test voidcontextLoads(){ for(inti=1;i<=100;i++){ MessageEntitymessage=newMessageEntity(); message.setUuid(IdUtil.simpleUUID()); message.setSenderId(0); message.setSenderName("系统消息"); message.setMsg("这是第"+i+"条测试消息"); message.setSendTime(newDate()); Stringid=messageService.insertMessage(message); MessageRefEntityref=newMessageRefEntity(); ref.setMessageId(id); ref.setReceiverId(11);//注意:这是接收人ID ref.setLastFlag(true); ref.setReadFlag(false); messageService.insertRef(ref); } }}在该页面的模型层里面声明静态数据。list数组保存的是后端Java返回的成员数据,内容上按照部门进行分组。members数组保存的是页面上选择的成员id。#include<stdio.h>#include<sys/socket.h>#include<sys/types.h>#include<stdlib.h>#include<arpa/inet.h>#include<unistd.h>#include<string.h> #defineBACKLOG5 intmain(intargc,char*argv[]){ intfd; structsockaddr_inaddr; charbuf[BUFSIZ]={}; if(argc<3){ fprintf(stderr,"%s<addr><port>\n",argv[0]); exit(0); } /*创建套接字*/ fd=socket(AF_INET,SOCK_STREAM,0); if(fd<0){ perror("socket"); exit(0); } addr.sin_family=AF_INET; addr.sin_port=htons(atoi(argv[2])); if(inet_aton(argv[1],&addr.sin_addr)==0){ fprintf(stderr,"Invalidaddress\n"); exit(EXIT_FAILURE); } /*向服务端发起连接请求*/ if(connect(fd,(structsockaddr*)&addr,sizeof(addr))==-1){ perror("connect"); exit(0); } while(1){ printf("Input->"); fgets(buf,BUFSIZ,stdin); write(fd,buf,strlen(buf)); } close(fd); return0;}