linguanghuan
linguanghuan · 共 7 篇文章
内存对齐初探
test.c #include <stdio.h> struct stu{ char sex; int length; char name[10]; }; int main() { char name[10]; struct stu mystu; printf("size of char:%d\n", sizeof(char)); printf("size of int: %d\n", sizeof(int)); printf("size of char[10]:%d\n", sizeof(name)); printf("size of stu:%d\n", sizeof(mystu)); …
2015-04-28 11:09 · C/C++ · 评论(0)
标准输入复制到标准输出
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #define BUFFSIZE 4096 int main(void) { int n; char buf[BUFFSIZE]; while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) if (write(STDOUT_FILENO, buf, n) != n) printf("write error\n"); exit(1); if (n < 0) printf("write error\n"); exit(0); } 运行…
2015-04-23 22:39 · Linux · 评论(0)
myls.c 读目录
#include <dirent.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *dirp; if (argc != 2) { printf("usage: ls directory_name\n"); exit(1); } if ((dp = opendir(argv[1])) == NULL) { printf("can't open %s\n", argv[1]); exit(1); } while ((dirp = readdir(…
2015-04-23 21:11 · Linux · 评论(0)
Socket epoll
#include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <netinet/in.h> #include <string.h> #include <sys/epoll.h> #define EPOLLSIZE 100 #define MAXEVENTS 100 #define LINESIZE 1024 int main() { int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd == -1) {…
Linux Socket select IO多路复用
#include <sys/socket.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <netinet/in.h> #define BACKLOG 5 #define MAXLINE 1024 int clients[BACKLOG]; int main() { int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd == -1) { printf("socket error[%d]:%s…
Linux Socket 多进程服务器
server.c #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netinet/in.h> #include <signal.h> #define MAXLINE 1024 // 1: static void sig_child(int signo) { pid_t pid; int status; while( (pid=waitpid(-1, &status,WNOHANG)) > 0) //2: W NO HA…
Linux Socket 基础
编写服务端程序server.c #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netinet/in.h> #define MAXLINE 1024 int main() { int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd == -1) { printf("socket error[%d]:%s\n", errno, strerror(err…