网站开发程序有哪些,网站怎么解析域名,网站全站开发,wordpress怎样给目录增加我要成为嵌入式高手之3月7日Linux高编第十七天#xff01;#xff01; ————————————————————————————
回顾
重要程序
1、UDP实现文件的发送
发端#xff1a;
#include head.hint main(void)
{int sockfd 0;struct sockaddr_i…我要成为嵌入式高手之3月7日Linux高编第十七天 ————————————————————————————
回顾
重要程序
1、UDP实现文件的发送
发端
#include head.hint main(void)
{int sockfd 0;struct sockaddr_in recvaddr;ssize_t nsize 0;int ret 0;int fd 0;char pathname[1024] {0};char tmpbuff[1024] {0};ssize_t nret;sockfd socket(AF_INET, SOCK_DGRAM, 0);if (-1 sockfd){perror(fail to socket);return -1;}recvaddr.sin_family AF_INET;recvaddr.sin_port htons(33333);recvaddr.sin_addr.s_addr inet_addr(192.168.1.167);memset(pathname, 0, sizeof(pathname));gets(pathname);nsize sendto(sockfd, pathname, strlen(pathname), 0, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (nsize -1){perror(fail to sendto);return -1;}fd open(pathname, O_RDONLY);if (fd -1){perror(fail to open);return -1;}printf(pathname: %s\n, pathname);while (1){usleep(10);nret read(fd, tmpbuff, sizeof(tmpbuff));nsize sendto(sockfd, tmpbuff, nret, 0, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (nsize -1){perror(fail to sendto tmpbuff);return -1;}if (nret 0){break;}}sprintf(tmpbuff, |__quit__|);nsize sendto(sockfd, tmpbuff, strlen(tmpbuff)1, 0, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (nsize -1){perror(fail to sendto tmpbuff);return -1;}printf(成功发送 %s 文件\n, pathname);close(fd);close(sockfd);return 0;
}收端
#include head.hint main(void)
{int ret 0;int sockfd 0;char tmpbuff[4096] {0};struct sockaddr_in recvaddr;ssize_t nsize 0;struct sockaddr_in sendaddr;socklen_t len sizeof(sendaddr);sockfd socket(AF_INET, SOCK_DGRAM, 0);if (sockfd -1){perror(fail to socket);return -1;}recvaddr.sin_family AF_INET;recvaddr.sin_port htons(33333);recvaddr.sin_addr.s_addr inet_addr(192.168.1.167);ret bind(sockfd, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (ret -1){perror(fail to bind);return -1;}nsize recvfrom(sockfd, tmpbuff, sizeof(tmpbuff), 0, (struct sockaddr *)sendaddr, len);if (nsize -1){perror(fail to recvfrom);return -1;}// printf(%s:%d接受到%ld个字节%s\n,inet_ntoa(sendaddr.sin_addr),ntohs(sendaddr.sin_port),nsize,tmpbuff);int fd 0;char ch[1024] {0};fd open(tmpbuff,O_WRONLY | O_CREAT | O_TRUNC, 0664);if (fd -1){perror(fail to fopen);return -1;}while(nsize ! 0){memset(ch, 0, sizeof(ch));nsize recvfrom(sockfd, ch, sizeof(ch), 0, (struct sockaddr *)sendaddr, len);if (nsize -1){perror(fail to recvfrom);return -1;}if (!strcmp(ch,1)){break;}write(fd,ch,nsize);printf(写入%ld字节\n,nsize);}printf(接受完成!\n);close(fd);close(sockfd);return 0;
}
2、UDP实现全双工的聊天
发送端
#include head.hchar name[32] {0};
int sockfd 0;
struct sockaddr_in recvaddr;
pthread_t tid_send;
pthread_t tid_recv;void *sendfun(void *arg)
{ssize_t nsize 0;struct msgbuf sendmsg;while(1){memset(sendmsg, 0, sizeof(sendmsg));sendmsg.type MSG_TYPE_CHAT;sprintf(sendmsg.name, %s, name);gets(sendmsg.text); if (!strcmp(sendmsg.text, .quit)){sendmsg.type MSG_TYPE_END;}nsize sendto(sockfd, sendmsg, sizeof(sendmsg), 0, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (nsize -1){perror(fail to sendto);return NULL;}if (sendmsg.type MSG_TYPE_END){break;}}pthread_cancel(tid_recv);return NULL;
}void *recvfun(void *arg)
{struct msgbuf recvmsg;ssize_t nsize 0;while(1){memset(recvmsg, 0, sizeof(recvmsg));nsize recvfrom(sockfd, recvmsg, sizeof(recvmsg), 0, NULL, NULL);if (-1 nsize){perror(fail to recvfrom);return NULL;}if (recvmsg.type MSG_TYPE_CHAT){printf(%s(%s:%d)%s\n, recvmsg.name, RECV_ADDR, RECV_PORT, recvmsg.text);}else if (recvmsg.type MSG_TYPE_END){break;}}pthread_cancel(tid_send);return NULL;
}int main(void)
{ssize_t nsize 0;struct msgbuf sendmsg;/*设置接收方信息*/recvaddr.sin_family AF_INET;recvaddr.sin_port htons(RECV_PORT);recvaddr.sin_addr.s_addr inet_addr(RECV_ADDR);/*输入昵称*/printf(请输入您的昵称:\n);gets(name);/*创建套接字*/sockfd socket(AF_INET, SOCK_DGRAM, 0);if (sockfd -1){perror(fail to sockfd);return -1;}/*设置要发送的信息*/memset(sendmsg, 0, sizeof(sendmsg));sendmsg.type MSG_TYPE_START;sprintf(sendmsg.name, %s, name);/*发送信息*/nsize sendto(sockfd, sendmsg, sizeof(sendmsg), 0, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (nsize -1){perror(fail to sendto);return -1;}pthread_create(tid_send, NULL, sendfun, NULL);pthread_create(tid_recv, NULL, recvfun, NULL);pthread_join(tid_send, NULL);pthread_join(tid_recv, NULL);close(sockfd);return 0;
}接收端
#include head.hchar name[32] {0};
int sockfd 0;
struct sockaddr_in recvaddr;
struct sockaddr_in sendaddr;
pthread_t tid_send;
pthread_t tid_recv;void *sendfun(void *arg)
{ssize_t nsize 0;struct msgbuf sendmsg;while(1){memset(sendmsg, 0, sizeof(sendmsg));sendmsg.type MSG_TYPE_CHAT;sprintf(sendmsg.name, %s, name);gets(sendmsg.text); if (!strcmp(sendmsg.text, .quit)){sendmsg.type MSG_TYPE_END;}nsize sendto(sockfd, sendmsg, sizeof(sendmsg), 0, (struct sockaddr *)sendaddr, sizeof(sendaddr));if (nsize -1){perror(fail to sendto);return NULL;}if (sendmsg.type MSG_TYPE_END){break;}}pthread_cancel(tid_recv);return NULL;
}void *recvfun(void *arg)
{struct msgbuf recvmsg;ssize_t nsize 0;while(1){memset(recvmsg, 0, sizeof(recvmsg));nsize recvfrom(sockfd, recvmsg, sizeof(recvmsg), 0, NULL, NULL);if (-1 nsize){perror(fail to recvfrom);return NULL;}if (recvmsg.type MSG_TYPE_CHAT){printf(%s(%s:%d)%s\n, recvmsg.name, inet_ntoa(sendaddr.sin_addr), ntohs(sendaddr.sin_port), recvmsg.text);}else if (recvmsg.type MSG_TYPE_END){break;}}pthread_cancel(tid_send);return NULL;
}int main(void)
{char tmpbuff[1024] {0};struct msgbuf recvmsg;int ret 0;socklen_t addrlen sizeof(sendaddr);/*设置接收方信息*/recvaddr.sin_family AF_INET;recvaddr.sin_port htons(RECV_PORT);recvaddr.sin_addr.s_addr inet_addr(RECV_ADDR);/*输入昵称*/printf(请输入您的昵称:\n);gets(name);/*创建套接字*/sockfd socket(AF_INET, SOCK_DGRAM, 0);if (sockfd -1){perror(fail to sockfd);return -1;}ret bind(sockfd, (struct sockaddr *)recvaddr, sizeof(recvaddr));if (-1 ret){perror(fail to bind);return -1;}/*设置要发送的信息*/memset(recvmsg, 0, sizeof(recvmsg));/*接收信息*/ret recvfrom(sockfd, recvmsg, sizeof(recvmsg), 0, (struct sockaddr *)sendaddr, addrlen);if (ret -1){perror(fail to recvfrom);return -1;}pthread_create(tid_send, NULL, sendfun, NULL);pthread_create(tid_recv, NULL, recvfun, NULL);pthread_join(tid_recv, NULL);pthread_join(tid_send, NULL);close(sockfd);return 0;
}head.h
#ifndef _HEAD_H
#define _HEAD_H#include sys/stat.h
#include sys/types.h
#include unistd.h
#include fcntl.h
#include stdio.h
#include string.h
#include dirent.h
#include time.h
#include pwd.h
#include stdlib.h
#include grp.h
#include sys/wait.h
#include pthread.h
#include semaphore.h
#include signal.h
#include sys/ipc.h
#include sys/msg.h
#include sys/shm.h
#include sys/sem.h
#include sys/socket.h
#include netinet/ip.h
#include netinet/in.h
#include arpa/inet.hstruct msgbuf
{int type; //消息类型char name[32]; //昵称char text[32]; //消息
};#define MSG_TYPE_START 100 //开始聊天消息类型
#define MSG_TYPE_END 200 //退出聊天消息类型
#define MSG_TYPE_CHAT 300 //聊天消息类型
#define RECV_ADDR 192.168.1.172 //接收端地址
#define RECV_PORT 50000 //接收端端口#endif学习笔记
TCP通信
TCP发端(客户端) socket - connect - send / recv- close
TCP收端(服务端) socket - bind - listen - accept - send / recv - close 1、connect
#include sys/types.h /* See NOTES */ #include sys/socket.h
int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
功能发起链接请求
参数 sockfd套接字的描述符 addr目的地址存放空间首地址 addrlenIP地址的大小
返回值成功0失败-1
2、send
#include sys/types.h #include sys/socket.h
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
功能发送数据
参数 sockfd套接字描述符 buf发送数据空间的首地址 len发送数据的长度 flags属性默认为0
返回值 成功返回实际发送字节数 失败返回-1
3、recv
#include sys/types.h #include sys/socket.h
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
功能接收数据
参数 sockfd套接字描述符 buf存放数据空间首地址 len最大接收数据的长度 flags属性默认0
返回值 成功返回实际接收字节数失败返回-1 若对方退出返回0
客户端
#include head.hint main(void)
{int ret 0;int sockfd 0;struct sockaddr_in serveaddr;char tmpbuff[1024] {0};ssize_t nsize 0;sockfd socket(AF_INET, SOCK_STREAM, 0);if (-1 sockfd){perror(fail to socket);return -1;}serveaddr.sin_family AF_INET;serveaddr.sin_port htons(50000);serveaddr.sin_addr.s_addr inet_addr(192.168.1.101);ret connect(sockfd, (struct sockaddr *)serveaddr, sizeof(serveaddr));if (ret -1){perror(fail to connect);return -1;}gets(tmpbuff);nsize send(sockfd, tmpbuff, strlen(tmpbuff), 0);if (nsize -1){perror(fail to send);return -1;}memset(tmpbuff, 0, sizeof(tmpbuff));nsize recv(sockfd, tmpbuff, sizeof(tmpbuff), 0);if (nsize -1){perror(fail to recv);return -1;}printf(RECV: %s\n, tmpbuff);close(sockfd);return 0;
}4、listen
#include sys/types.h /* See NOTES */ #include sys/socket.h
int listen(int sockfd, int backlog);
功能监听客户端发送的链接请求不会阻塞
参数 sockfd套接字描述符 backlog允许等待的尚未被处理的三次握手请求的最大个数
返回值成功0失败-1
5、accept
#include sys/types.h /* See NOTES */ #include sys/socket.h
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
功能 处理等待链接队列中的第一个链接请求若没有人发送链接请求会阻塞等待直到有连接请求
参数 sockfd套接字描述符 addr存放IP地址的空间首地址 addrlen存放IP地址大小空间首地址
返回值 成功返回一个新的文件描述符 失败返回-1
服务端
#include head.hint main(void)
{int sockfd 0;struct sockaddr_in serveaddr;int ret 0;int confd 0;ssize_t nsize 0;char tmpbuff[1024] {0};serveaddr.sin_family AF_INET;serveaddr.sin_port htons(50000);serveaddr.sin_addr.s_addr INADDR_ANY;sockfd socket(AF_INET, SOCK_STREAM, 0);if (sockfd -1){perror(fail to socket);return -1;}ret bind(sockfd, (struct sockaddr *)serveaddr, sizeof(serveaddr));if (ret -1){perror(fail to bind);return -1;}ret listen(sockfd, 10);if (-1 ret){perror(fail to listen);return -1;}confd accept(sockfd, NULL, NULL);if (-1 confd){perror(fail to accept);return -1;}nsize recv(confd, tmpbuff, sizeof(tmpbuff), 0);if (-1 nsize){perror(fail to recv);return -1;}printf(RECV: %s\n, tmpbuff);memset(tmpbuff, 0, sizeof(tmpbuff));gets(tmpbuff);nsize send(confd, tmpbuff, strlen(tmpbuff), 0);if (-1 nsize){perror(fail to send);return -1;}close(confd);close(sockfd);return 0;
} 文章转载自: http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn