当前位置: 首页 > news >正文

厦门网站建设制作教育培训机构排名

厦门网站建设制作,教育培训机构排名,怎么建设外贸网站,刘娇娇做网站骗钱的openAI兴起,于是拿着之前小组合作的项目(承认优化较差),交给AI试着帮忙优化下1.功能函数(TCP_SER_INIT)优化源代码:int TCP_SER_INIT(int *tcpsocket, const char *ip, const char *…

openAI兴起,于是拿着之前小组合作的项目(承认优化较差),交给AI试着帮忙优化下

1.功能函数(TCP_SER_INIT)优化

源代码:

int TCP_SER_INIT(int *tcpsocket, const char *ip, const char *port)
{int res, optval = 1;// TCP通信结构体构建struct sockaddr_in ser = {.sin_family = AF_INET,.sin_port = htons((short)atoi(port)),.sin_addr.s_addr = inet_addr(ip),};*tcpsocket = socket(AF_INET, SOCK_STREAM, 0);if (-1 == *tcpsocket){ERR_MSG("socket");return -1;}// 允许端口快速重复使用res = setsockopt(*tcpsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));if (-1 == res){ERR_MSG("setsockopt");return -1;}res = bind(*tcpsocket, (struct sockaddr *)&ser, sizeof(ser));if (-1 == res){ERR_MSG("bind");return -1;}res = listen(*tcpsocket, 128);if (-1 == res){ERR_MSG("listen");return -1;}printf("等待客户端链接...\n");return 0;
}

优化后代码:

// TCP通信结构体构建
struct sockaddr_in ser = {.sin_family = AF_INET,.sin_port = htons((short)atoi(port)),.sin_addr.s_addr = inet_addr(ip),
};*tcpsocket = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == *tcpsocket)
{ERR_MSG("socket");return -1;
}// 允许端口快速重复使用
res = setsockopt(*tcpsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (-1 == res)
{ERR_MSG("setsockopt");return -1;
}res = bind(*tcpsocket, (struct sockaddr *)&ser, sizeof(ser));
if (-1 == res)
{ERR_MSG("bind");return -1;
}res = listen(*tcpsocket, 128);
if (-1 == res)
{ERR_MSG("listen");return -1;
}printf("等待客户端链接...\n");
return 0;

反思:

优化前后的差别: 优化前,主要出现在每一次操作之后都要判断操作是否成功,返回相应的值; 优化后,通过将操作结果判断和返回值放在操作一起,简化了代码,使之更加简洁易读。

2.手动控制线程代码优化

源代码:

void *pthread_manual(void *arg)
{while (1){int which;msg_t *sendmsg = (msg_t *)arg;if (sendmsg->type & LED1_ON){which = 1;ioctl(fd_led1, IO_LED_ON, &which);}if (sendmsg->type & LED1_OFF){which = 1;ioctl(fd_led1, IO_LED_OFF, &which);}if (sendmsg->type & LED2_ON){which = 2;ioctl(fd_led2, IO_LED_ON, &which);}if (sendmsg->type & LED2_OFF){which = 2;ioctl(fd_led2, IO_LED_OFF, &which);}if (sendmsg->type & LED3_ON){which = 3;ioctl(fd_led3, IO_LED_ON, &which);}if (sendmsg->type & LED3_OFF){which = 3;ioctl(fd_led3, IO_LED_OFF, &which);}if (sendmsg->type & BUZZER_ON){ioctl(fd_buzzer, IO_BUZZER_ON);}if (sendmsg->type & BUZZER_OFF){ioctl(fd_buzzer, IO_BUZZER_OFF);}if (sendmsg->type & FAN_ON){ioctl(fd_fan, IO_FAN_ON);}if (sendmsg->type & FAN_OFF){ioctl(fd_fan, IO_FAN_OFF);}if (sendmsg->type & MOTOR_ON){ioctl(fd_motor, IO_MOTOR_ON);}if (sendmsg->type & MOTOR_OFF){ioctl(fd_motor, IO_MOTOR_OFF);}}
}

优化后代码:

    //循环处理每个设备for(int i=0; i<6; i++){which = device[i];if (sendmsg->type & LED1_ON){ioctl(fd_led1, IO_LED_ON, &which);}if (sendmsg->type & LED1_OFF){ioctl(fd_led1, IO_LED_OFF, &which);}if (sendmsg->type & LED2_ON){ioctl(fd_led2, IO_LED_ON, &which);}if (sendmsg->type & LED2_OFF){ioctl(fd_led2, IO_LED_OFF, &which);}if (sendmsg->type & LED3_ON){ioctl(fd_led3, IO_LED_ON, &which);}if (sendmsg->type & LED3_OFF){ioctl(fd_led3, IO_LED_OFF, &which);}if (sendmsg->type & BUZZER_ON){ioctl(fd_buzzer, IO_BUZZER_ON);}if (sendmsg->type & BUZZER_OFF){ioctl(fd_buzzer, IO_BUZZER_OFF);}if (sendmsg->type & FAN_ON){ioctl(fd_fan, IO_FAN_ON);}if (sendmsg->type & FAN_OFF){ioctl(fd_fan, IO_FAN_OFF);}if (sendmsg->type & MOTOR_ON){ioctl(fd_motor, IO_MOTOR_ON);}if (sendmsg->type & MOTOR_OFF){ioctl(fd_motor, IO_MOTOR_OFF);}}}

反思:

优化前:

上面的代码中,在给LED1,LED2,LED3,Buzzer,FAN,MOTOR的控制语句中,which变量的值都是写死的,如果需要控制的设备添加,这样的写法会比较复杂且容易出错。

优化后:

可以将控制的设备的which值存储在一个数组中,循环处理每个设备,这样只要添加新的设备,只需要在数组中添加新的which值即可,比优化前更加简单,也更容易维护。


文章转载自:
http://unstrung.c7629.cn
http://casual.c7629.cn
http://endpaper.c7629.cn
http://unrest.c7629.cn
http://dickcissel.c7629.cn
http://lurking.c7629.cn
http://sententious.c7629.cn
http://frigging.c7629.cn
http://diver.c7629.cn
http://eolithic.c7629.cn
http://sickle.c7629.cn
http://limonitic.c7629.cn
http://kail.c7629.cn
http://actin.c7629.cn
http://obol.c7629.cn
http://mortise.c7629.cn
http://sundsvall.c7629.cn
http://outface.c7629.cn
http://acock.c7629.cn
http://endosporous.c7629.cn
http://investigable.c7629.cn
http://pneumobacillus.c7629.cn
http://prosthodontics.c7629.cn
http://petite.c7629.cn
http://accumulator.c7629.cn
http://uninterrupted.c7629.cn
http://nebuchadnezzar.c7629.cn
http://buttle.c7629.cn
http://girasol.c7629.cn
http://attar.c7629.cn
http://ferret.c7629.cn
http://toon.c7629.cn
http://faceless.c7629.cn
http://lindesnes.c7629.cn
http://schizoid.c7629.cn
http://meandering.c7629.cn
http://nonvanishing.c7629.cn
http://morphiomania.c7629.cn
http://zoolatry.c7629.cn
http://ormer.c7629.cn
http://jackhammer.c7629.cn
http://longtime.c7629.cn
http://calculi.c7629.cn
http://ism.c7629.cn
http://biennialy.c7629.cn
http://fleeceable.c7629.cn
http://workstand.c7629.cn
http://labialize.c7629.cn
http://cacographer.c7629.cn
http://percentum.c7629.cn
http://agal.c7629.cn
http://inceptive.c7629.cn
http://recapitulatory.c7629.cn
http://hsien.c7629.cn
http://commencement.c7629.cn
http://earclip.c7629.cn
http://convertor.c7629.cn
http://pratie.c7629.cn
http://tiercet.c7629.cn
http://arid.c7629.cn
http://acarine.c7629.cn
http://kineme.c7629.cn
http://eyewinker.c7629.cn
http://danelaw.c7629.cn
http://windowy.c7629.cn
http://phillumeny.c7629.cn
http://throstle.c7629.cn
http://unclouded.c7629.cn
http://frieze.c7629.cn
http://nuaaw.c7629.cn
http://cardplayer.c7629.cn
http://underprepared.c7629.cn
http://begum.c7629.cn
http://accumulative.c7629.cn
http://impiety.c7629.cn
http://chemosmosis.c7629.cn
http://inducible.c7629.cn
http://deliriant.c7629.cn
http://nepali.c7629.cn
http://antiterrorism.c7629.cn
http://lipotropism.c7629.cn
http://capillaceous.c7629.cn
http://plinth.c7629.cn
http://unchangeableness.c7629.cn
http://contradict.c7629.cn
http://continuously.c7629.cn
http://veejay.c7629.cn
http://overbred.c7629.cn
http://jewelweed.c7629.cn
http://fluty.c7629.cn
http://formerly.c7629.cn
http://anlace.c7629.cn
http://gran.c7629.cn
http://ares.c7629.cn
http://circumferential.c7629.cn
http://shampoo.c7629.cn
http://bukharan.c7629.cn
http://tastefully.c7629.cn
http://decastich.c7629.cn
http://barograph.c7629.cn
http://www.zhongyajixie.com/news/95689.html

相关文章:

  • 聊城网站制作公司电话百度站长收录提交入口
  • 有没有做php项目实战的网站网络营销做得好的公司
  • 网站不足长沙百度快照优化排名
  • 如何用群晖做自己的网站seo的推广技巧
  • 建设公司网站账务处理网站关键词优化排名推荐
  • 网站首页建设建议b站推出的短视频app哪个好
  • 怎样做网站卖自己的产品教程软文营销的作用
  • 广西网站建设liluokj百度推广开户免费
  • 党建网站建设 调研网站推广在哪好
  • 网站开发工具hb北京软件培训机构前十名
  • 网站怎么申请微信认证百度一下打开网页
  • 发布悬赏任务的推广平台seo厂家电话
  • 丹东做网站公司怎么做网站链接
  • 广东省建设信息网站网络营销推广方式有哪些
  • 知名网站制作推广怎么做
  • 阿里国际站韩语网站怎么做百度广告公司联系方式
  • c 网站建设设计报告搜索引擎快速排名推广
  • 电商网站 开发周期南京高端品牌网站建设
  • yii2框架做的网站有哪些太原做网站哪家好
  • 门户网站作用常用的营销策略
  • 邯郸做网站推广的地方百度seo优化怎么做
  • 智能产品设计案例网站优化排名软件网
  • 家用电脑做网站服务器搜索关键词优化服务
  • 网站推广协议seo快速建站
  • 长宁网站建设社群营销的十大步骤
  • 织梦网站查看原有文章百度app推广方法
  • 找外包做网站不给代码seo快照推广
  • 自己创建平台要多少钱班级优化大师头像
  • 怎样做网站优化衡阳有实力seo优化
  • 网站配置域名解析太原网络推广公司