Archive for the ‘技事巧事’ Category
UDP服务器端与客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #define PORT_SERVER 9898 int main() { int socket_fd; int nrecv; int size; struct sockaddr_in ad_s; char buffer[1024]; if((socket_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) { fprintf(stderr,"socket error\n"); exit(1); } bzero(&ad_s,sizeof(ad_s)); ad_s.sin_family=AF_INET; ad_s.sin_addr.s_addr=htonl(INADDR_ANY); ad_s.sin_port=htons(PORT_SERVER); if(bind(socket_fd,(struct sockaddr *)&ad_s,sizeof(ad_s))) { fprintf(stderr,"bind error\n"); exit(1); } while(1) { bzero(buffer,1024); size=sizeof(struct sockaddr); nrecv=recvfrom(socket_fd,buffer,1024,0,(struct sockaddr *)&ad_s,&size); buffer[nrecv]='\0'; printf("server receive %s\n",buffer); } close(socket_fd); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT_CLIENT 9898 int main(int argv,char *argc[]) { struct hostent *host; if(argv<2) { perror("parameter error\n"); exit(1); } if((host=gethostbyname(argc[1]))==NULL) { fprintf(stderr,"gethostname error\n"); exit(1); } int socket_fd; struct sockaddr_in ad_s; char buffer[1024]; if((socket_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) { fprintf(stderr,"socket error\n"); exit(1); } bzero(&ad_s,sizeof(ad_s)); ad_s.sin_family=AF_INET; ad_s.sin_port=htons(PORT_CLIENT); ad_s.sin_addr=*((struct in_addr*)host->h_addr); while(1) { printf("input something:\n"); fgets(buffer,1024,stdin); if(sendto(socket_fd,buffer,strlen(buffer),0,(struct sockaddr *)&ad_s,sizeof(ad_s))==-1) { fprintf(stderr,"sendto error\n"); exit(1); } bzero(buffer,1024); } close(socket_fd); } |
TCP服务器端与客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT_SERVER 9898 int main() { int socket_fd,fd; int nread; int size; char buffer[1024]; struct sockaddr_in ad_s,ad_c; if((socket_fd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"socket error\n"); exit(1); } bzero(&ad_s,sizeof(ad_s)); ad_s.sin_family=AF_INET; ad_s.sin_addr.s_addr=htonl(INADDR_ANY); ad_s.sin_port=htons(PORT_SERVER); if(bind(socket_fd,(struct sockaddr *)&ad_s,sizeof(ad_s))==-1) { fprintf(stderr,"bind error\n"); exit(1); } if(listen(socket_fd,5)==-1) { fprintf(stderr,"listen error\n"); exit(1); } while(1) { size=sizeof(struct sockaddr_in); bzero(buffer,1024); if((fd=accept(socket_fd,(struct sockaddr *)&ad_c,&size))==-1) { fprintf(stderr,"accept error\n"); exit(1); } printf("server connect from %s\n",inet_ntoa(ad_c.sin_addr.s_addr)); if((nread=read(fd,buffer,1024))==-1) { fprintf(stderr,"read error\n"); exit(1); } buffer[nread]='\0'; printf("server receive %s\n",buffer); close(fd); } close(socket_fd); exit(0); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT_CLIENT 9898 int main(int argv,char *argc[]) { struct hostent *host; if(argv<2) { perror("parameter error\n"); exit(1); } if((host=gethostbyname(argc[1]))==NULL) { fprintf(stderr,"gethostname error\n"); exit(1); } int sock_fd; struct sockaddr_in ad_s; char buffer[1024]; if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"socket error\n"); exit(1); } bzero(&ad_s,sizeof(ad_s)); ad_s.sin_family=AF_INET; ad_s.sin_port=htons(PORT_CLIENT); ad_s.sin_addr=*((struct in_addr*)host->h_addr); if(connect(sock_fd,(struct sockaddr *)&ad_s,sizeof(ad_s))==-1) { fprintf(stderr,"connect error\n"); exit(1); } printf("input something...\n"); bzero(buffer,1024); fgets(buffer,1024,stdin); write(sock_fd,buffer,strlen(buffer)); close(sock_fd); exit(0); } |
留个记号
这么长时间也没有做下真正的记号,最好的复习应该就是做笔记了。今天做个小笔记吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | assume cs:code,ds:data data segment db 'welcome to masm!' db 02h,24h,71h data ends code segment start: mov ax,data mov ds,ax mov ax,0b872h ;显示位置 mov es,ax mov bp,10h ;颜色02h的位置 mov cx,3 s: push cx mov di,0 mov cx,16 s1: mov dl,ds:[di] ;将字符 mov es:[si],dl ;复制到低字节 mov dh,ds:[bp] ;将颜色 mov es:[si+1],dh;复制到高字节 inc di ;下一字符 add si,2;加2,颜色为奇地址 loop s1 add si,80h ;160个字节-32字节跳转到下一行 add bp,1 pop cx loop s mov ax,4c00h int 21h code ends end start |
最后忘记写程序退出返回,导致查了很多遍都没有找到问题所在,以后得记清楚了。
坚定不移的信念是成功的钥匙!