diff --git a/Linux_study/src/1 b/Linux_study/src/1 deleted file mode 100644 index 942a70f..0000000 --- a/Linux_study/src/1 +++ /dev/null @@ -1 +0,0 @@ -/proc/sys/vm/drop_caches diff --git a/Linux_study/src/a.out b/Linux_study/src/a.out deleted file mode 100755 index 559a10e..0000000 Binary files a/Linux_study/src/a.out and /dev/null differ diff --git a/Linux_study/src/append.txt b/Linux_study/src/append.txt deleted file mode 100755 index dd2fc4a..0000000 --- a/Linux_study/src/append.txt +++ /dev/null @@ -1 +0,0 @@ -hello iotek diff --git a/Linux_study/src/atexit.c b/Linux_study/src/atexit.c deleted file mode 100644 index face2f3..0000000 --- a/Linux_study/src/atexit.c +++ /dev/null @@ -1,51 +0,0 @@ -/************************************************************************* - > File Name: atexit.c - > Author: - > Mail: - > Created Time: 2017年09月25日 19时43分29秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include - -void term_fun1() -{ - printf("first term function\n"); -} -void term_fun2() -{ - printf("second term function\n"); -} -void term_fun3() -{ - printf("third term function\n"); -} -int main(int argc, char *argv[]) -{ - if(argc < 3) - { - fprintf(stderr, "usage : %s file[exit|return|_exit]", argv[1]); - } - //向内核登记终止函数 - atexit(term_fun1); - atexit(term_fun2); - atexit(term_fun3); - FILE *fp = fopen(argv[1], "w"); - fprintf(fp, "hello iotek");//标准c库函数 全缓存 - - if(!strcmp(argv[2], "exit")) - { - exit(0); - } - else if(!strcmp(argv[2], "return")) - { - return 0; - } - else if(!strcmp(argv[2], "_exit")); - { - _exit(0);//系统调用 - } -} diff --git a/Linux_study/src/cat.c b/Linux_study/src/cat.c deleted file mode 100644 index e93503f..0000000 --- a/Linux_study/src/cat.c +++ /dev/null @@ -1,60 +0,0 @@ -/************************************************************************* - > File Name: cat.c - > Author: - > Mail: - > Created Time: 2017年09月21日 19时05分53秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include -#define MAXLEN 4096 -char buffer[MAXLEN]; -/* - * cat功能的代码实现 - */ -void copy(int fd_in, int fd_out); -int main(int argc, char *argv[]) -{ - int fd_in = STDIN_FILENO; - int fd_out = STDOUT_FILENO; - for(int i = 1; i < argc; ++i) - { - fd_in = open(argv[i], O_RDONLY); - if(fd_in < 0) - { - perror("open error"); - continue; - } - copy(fd_in, fd_out); - close(fd_in); - } - if(argc ==1) - { - copy(fd_in, fd_out); - } - return 0; -} -void copy(int fd_in, int fd_out) -{ - int len = 0; - while((len = read(fd_in, buffer,MAXLEN)) != 0) - { - //printf("len = %d\n", len); - if(len < 0) - { - fprintf(stderr, "error: read error"); - exit(1); - } - else - { - if(write(fd_out, buffer, len) != len) - { - fprintf(stderr, "error: write error"); - exit(1); - } - } - } -} diff --git a/Linux_study/src/child b/Linux_study/src/child deleted file mode 100755 index 742db7b..0000000 Binary files a/Linux_study/src/child and /dev/null differ diff --git a/Linux_study/src/fcnyl.c b/Linux_study/src/fcnyl.c deleted file mode 100644 index 93f23e0..0000000 --- a/Linux_study/src/fcnyl.c +++ /dev/null @@ -1,108 +0,0 @@ -/************************************************************************* - > File Name: fcnyl.c - > Author: - > Mail: - > Created Time: 2017年09月22日 03时17分55秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include -#include - -#define BUF_LEN 4096 -void copy(int fd_in, int fd_out) -{ - char buffer[BUF_LEN]; - int len = 0; - while((len = read(fd_in, buffer ,BUF_LEN)) != 0) - { - //printf("len = %d\n", len); - if(len < 0) - { - fprintf(stderr, "error: read error"); - exit(1); - } - else - { - if(write(fd_out, buffer, len) != len) - { - fprintf(stderr, "error: write error"); - exit(1); - } - } - } -} - -/*void copy(int fd_in, int fd_out) -{ - char buffer[BUF_LEN]; - int size; - while((size = read(fd_in, buffer, BUF_LEN)) != 0) - { - printf("size = %d\n", size); - if(write(fd_out, buffer, size)!= size) - { - perror("write error"); - exit(1); - } - } - if(size < 0) - { - fprintf(stderr, "read error"); - exit(1); - } - close(fd_in); - close(fd_out); -}*/ -void set_fl(int fd, int flag) -{ - //获得原来的文件状态标志 - int val = fcntl(fd , F_GETFL); - //增加新的文件状态标志 - val |= flag;//按位或 - //重新设置文件状态标志 - if(fcntl(fd, F_SETFL, val)<0) - { - perror("fcntl error"); - } -} - -void clr_fl(int fd ,int flag) -{ - int val = fcntl(fd, F_GETFL); - //清除指定的文件状态标志 - val &=~flag; //按位与(置0) - if(fcntl(fd, F_SETFL, val)< 0) - { - perror("fcntl error2"); - } -} -int main(int argc, char *argv[]) -{ - - int fd_in; - - if(argc < 4) - { - fprintf(stderr, "usage: sourcefile and distfile\n"); - exit(1); - } - fd_in = open(argv[2], O_WRONLY); - int fd_in2 = open(argv[3], O_WRONLY|O_CREAT|O_TRUNC, 0777); - //fd_in = open(argv[2],O_WRONLY |O_APPEND);//原子操作,多个线程一起对文件进行读写不会相互覆盖 - if(fd_in < 0) - { - perror("open failed "); - exit(1); - } - int len = strlen(argv[1])*sizeof(char); - - write(fd_in, argv[1], len); - - copy(fd_in, STDOUT_FILENO); - close(fd_in); -} - diff --git a/Linux_study/src/hole_file.c b/Linux_study/src/hole_file.c deleted file mode 100644 index 79b2b5c..0000000 --- a/Linux_study/src/hole_file.c +++ /dev/null @@ -1,49 +0,0 @@ -/************************************************************************* - > File Name: hole_file.c - > Author: - > Mail: - > Created Time: 2017年09月20日 11时11分37秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include - -char *buf = "12345555533212l"; -int main(int argc, char *argv[]) -{ - if(argc < 2) - { - fprintf(stderr, "error: %s [file]", argv[0]); - exit(1); - } - int fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0777); - if(fd < 0) - { - fprintf(stderr, "-usage %s open failed", argv[1]); - exit(1); - } - size_t size = strlen(buf)*sizeof(char); - //将字符写如空洞文件 - if(write(fd, buf, size)!= size) - { - fprintf(stderr, "-usage %s open failed!", argv[1]); - exit(1); - } - //定位文件尾部10个字节处 - if(lseek(fd, 10L, SEEK_END)<0) - { - perror("lseek error"); - exit(1); - } - //从文件尾部10个字节出写入 - if(write(fd, buf, size) != size) - { - perror("lseek error"); - exit(1); - } - close(fd); - return 0; -} diff --git a/Linux_study/src/iotek b/Linux_study/src/iotek deleted file mode 100644 index d5279d4..0000000 --- a/Linux_study/src/iotek +++ /dev/null @@ -1,2 +0,0 @@ -hello iotek -hello iotek diff --git a/Linux_study/src/line_buffer.c b/Linux_study/src/line_buffer.c deleted file mode 100644 index b47a405..0000000 --- a/Linux_study/src/line_buffer.c +++ /dev/null @@ -1,20 +0,0 @@ -/************************************************************************* - > File Name: ./src/line_buffer.c - > Author: - > Mail: - > Created Time: 2017年09月17日 12时51分22秒 PDT - ************************************************************************/ - -#include -/*行缓冲只有遇到换行符、程序结束或者行缓冲区满才会输出,这是证明*/ -int main() -{ - printf("hello iotek"); - // putchar(10); - while(1) - { -// putchar(10);//这里不加换行符,hello iotek不会输出 - - sleep(2); - }; -} diff --git a/Linux_study/src/process_appand.c b/Linux_study/src/process_appand.c deleted file mode 100644 index fadd6f1..0000000 --- a/Linux_study/src/process_appand.c +++ /dev/null @@ -1,53 +0,0 @@ -/************************************************************************* - > File Name: process_appand.c - > Author: - > Mail: - > Created Time: 2017年09月28日 14时56分30秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include -int main(int argc, char *argv[]) -{ - if(argc < 2){ - fprintf(stderr, "usag: %s file\n", argv[0]); - exit(1); - } - int fd = open(argv[1], O_WRONLY|O_CREAT, S_IRWXU|S_IRWXG); - if(fd < 2){ - perror("open error"); - exit(1); - } - - pid_t pid = fork(); - if(pid < 0){ - perror("fork error"); - exit(1); - }else if(pid > 0){ - //parent process - //父进程把文件偏移量跳到文件尾步 - if(lseek(fd, 0L, SEEK_END)< 0){ - perror("lseek error"); - exit(1); - } - }else{ - //child process - //子进程从文件末尾写入 - char *str = "hello iotek\n"; - ssize_t size = strlen(str)*sizeof(char); - sleep(3); - if(write(fd , str, size)!= size){ - perror("write error"); - exit(1); - } - } - printf("pid: %d finish!\n", getpid()); - sleep(1); - - //父子进程关闭fd - close(fd); - return 0; -} diff --git a/Linux_study/src/process_fork b/Linux_study/src/process_fork deleted file mode 100755 index 14fb6ae..0000000 Binary files a/Linux_study/src/process_fork and /dev/null differ diff --git a/Linux_study/src/process_fork. b/Linux_study/src/process_fork. deleted file mode 100644 index 760a73a..0000000 --- a/Linux_study/src/process_fork. +++ /dev/null @@ -1,2 +0,0 @@ -A - diff --git a/Linux_study/src/process_fork.c b/Linux_study/src/process_fork.c deleted file mode 100644 index 2844fbe..0000000 --- a/Linux_study/src/process_fork.c +++ /dev/null @@ -1,63 +0,0 @@ -/************************************************************************* - > File Name: process_fork.c - > Author: - > Mail: - > Created Time: 2017年09月27日 20时27分58秒 PDT - ************************************************************************/ - -#include -//进程创建 -#include -#include -#include -#include -//父子进程分别通过标准c和系统调用函数操作文件 - -int g_v = 30;//数据段 - -int main(void) -{ - int a_v = 30;//栈 - static int s_v = 30;//数据段 - - printf("pid = %d\n", getpid()); - - FILE *fp = fopen("s.txt", "w");//标准c的io函数是基于缓存的,这个缓存在堆中 - int fd = open("s_fd.txt",O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG); -//系统调用的io函数不带缓存。末尾文件权限宏有三种 - - char *s = "hello iotek"; - ssize_t size = strlen(s)*sizeof(char); - - /*父进程调用*/ - //标准io函数(带缓存-->全缓存) - fprintf(fp, "s:%s, pid:%d\n", s, getpid());//先写入缓存 - //kernel提供的io系统调用(不带缓存) - write(fd, s, size);//直接写入文件 - pid_t pid; - pid = fork();//创建子进程 - //在运行以后运行两个进程 - if(pid < 0){ - perror("fork error"); - }else if(pid == 0){ - //子进程返回0 - //子进程代码... - printf("I am child process pid is %d\n,ppid is %d,fork return is %d\n",getpid(), getppid(), pid); - g_v = 50; a_v= 50; s_v = 50; - printf("g_v: %p, a_v: %p, s_v: %p\n", &g_v, &a_v, &s_v); - }else{ - //父进程(在父进程中返回的子进程的pid) - //父进程的代码... - printf("I am parent process pid is %d\n,ppid is %d,fork return is %d\n",getpid(), getppid(), pid); - g_v = 40; a_v= 40; s_v = 40; - printf("g_v: %p, a_v: %p, s_v: %p\n", &g_v, &a_v, &s_v); - } - //printf("pid: %d, g_v: %d, a_v: %d, s_v; %d\n", getpid(), g_v, a_v, s_v); - fprintf(fp, "pid: %d, g_v: %d, a_v: %d, s_v; %d\n", getpid(), g_v, a_v, s_v); - - sleep(1); - fclose(fp); - fclose(fd); - - exit(0); -} diff --git a/Linux_study/src/process_fork2.c b/Linux_study/src/process_fork2.c deleted file mode 100644 index ba964f0..0000000 --- a/Linux_study/src/process_fork2.c +++ /dev/null @@ -1,34 +0,0 @@ -/************************************************************************* - > File Name: process_fork2.c - > Author: - > Mail: - > Created Time: 2017年09月27日 20时58分02秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -//父子进程交替运行 -int main(void) -{ - printf("current pid: %d", getpid()); - pid_t pid = fork(); - if(pid < 0){ - perror("fork error"); - }else if(pid > 0){ - int i = 0; - for(i = 0; i < 10; ++i) - { - printf("This is parent process pid is %d\n",getpid()); - sleep(1); - } - }else{ - for(int i =0; i < 10; ++i){ - printf("This is child process pid is %d\n", getpid()); - sleep(1); - } - } - - return 0; -} diff --git a/Linux_study/src/process_link.c b/Linux_study/src/process_link.c deleted file mode 100644 index ef0f136..0000000 --- a/Linux_study/src/process_link.c +++ /dev/null @@ -1,35 +0,0 @@ -/************************************************************************* - > File Name: process_link.c - > Author: - > Mail: - > Created Time: 2017年09月26日 00时19分41秒 PDT - ************************************************************************/ - -#include -#include -#include -int main(int argc, char *argv[]) -{ - if(argc < 2) - { - fprintf(stderr, "usage: %s file counter", argv[1]); - } - //接收输入,然后字符转换 - int counter = atoi(argv[1]); - - pid_t pid; - for(int i = 0; i < counter; ++i) - { - pid = fork(); - if(pid < 0) - { - fprintf(stderr, "usage: fork error"); - } - else if(pid > 0) - { - break; - } - printf("pid = %d, ppid = %d\n", getpid(), getppid()); - } - return 0; -} diff --git a/Linux_study/src/process_mark.c b/Linux_study/src/process_mark.c deleted file mode 100644 index a8991e1..0000000 --- a/Linux_study/src/process_mark.c +++ /dev/null @@ -1,24 +0,0 @@ -/************************************************************************* - > File Name: process_mark.c - > Author: - > Mail: - > Created Time: 2017年09月27日 19时52分40秒 PDT - ************************************************************************/ - -#include -#include -#include -//进程标识 -int main() -{ - printf("pid = %d\n",getpid());//当前进程id - printf("ppid = %d\n",getppid());//父进程id - printf("uid = %d\n",getuid());//实际用户id - printf("euid = %d\n",geteuid());//有效用户id - printf("user gid = %d\n", getgid());//用户的组id - printf("gid = %d\n", getpgrp());//当前进程组id - printf("pgid = %d\n",getpgid(getpid()));//当前进程所在进程族的id - printf("ppgid = %d\n", getpgid(getppid()));//父进程所在的进程组id - - return 0; -} diff --git a/Linux_study/src/process_wait.c b/Linux_study/src/process_wait.c deleted file mode 100644 index ea71ff1..0000000 --- a/Linux_study/src/process_wait.c +++ /dev/null @@ -1,83 +0,0 @@ -/************************************************************************* - > File Name: process_wait.c - > Author: - > Mail: - > Created Time: 2017年09月28日 16时11分16秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include -#include - -void out_status(int status) -{ - //状态返回状态码 - if(WIFEXITED(status)){//正常终止 - printf("normal exit: %d\n", WEXITSTATUS(status)); - }else if(WIFSIGNALED(status)){//非正常终止 - printf("abnormal term: %d\n", WTERMSIG(status)); - }else if(WIFSTOPPED(status)){//在终止前是否暂停,只能接受特定的暂停信号 - printf("stopped sig: %d\n", WSTOPSIG(status)); - }else{ - printf("unknow sig\n"); - } -} -int main(void) -{ - int status; - pid_t pid; - //正常终止 - if((pid = fork()) < 0){ - fprintf(stderr, "fork error"); - exit(1); - }else if(pid ==0){ - printf("pid = %d, ppid = %d\n", getpid(), getppid()); - exit(3);//子进程终止运行 - } - //父进程调用wait()阻塞,等待子进程结束并收回 - wait(&status); - out_status(status); - printf("--------------------\n"); - - //非正常终止 - if((pid = fork())< 0){ - perror("fork error"); - }else if(pid ==0){ - printf("pid = %d, ppid = %d\n", getpid(), getppid()); - int i = 3, j = 0; - int k = i/j; - printf("k = %d\n", k); - } - wait(&status); - out_status(status); - printf("--------------------\n"); - - //暂停状态信号 - if((pid = fork())< 0){ - perror("fork error"); - }else if(pid ==0){ - printf("pid = %d, ppid = %d\n", getpid(), getppid()); - pause();//暂停,等待信号唤醒,实际运行kill该进程显示非正常终止,下同。 - /*int i = 0; - while(++i>0) - { - sleep(3); - }*/ - } - pid = waitpid(pid, &status,WUNTRACED);//阻塞状态且监听暂停信号 - /*do{//因为我们设置的非阻塞状态,所以要用一个循环不断检测子进程是否运行完 - //也可以设置成阻塞的 - pid = waitpid(pid, &status, WNOHANG|WUNTRACED); - if(pid == 0){ - sleep(1); - } - }while(pid == 0);*/ - - //wait(&status); - out_status(status); - printf("--------------------\n"); -} - diff --git a/Linux_study/src/pthread b/Linux_study/src/pthread deleted file mode 100755 index 08f5dfb..0000000 Binary files a/Linux_study/src/pthread and /dev/null differ diff --git a/Linux_study/src/pthread_dectach.c b/Linux_study/src/pthread_dectach.c deleted file mode 100644 index 84ca6c6..0000000 --- a/Linux_study/src/pthread_dectach.c +++ /dev/null @@ -1,104 +0,0 @@ -/************************************************************************* -> File Name: pthread_dectach.c -> Author: -> Mail: -> Created Time: 2017年10月02日 22时03分09秒 PDT -************************************************************************/ - -#include -#include -#include - -void status_out(pthread_attr_t *attr) -{ - int stat = 0; - if(pthread_attr_getdetachstate(attr, &stat) != 0) - { - perror("getdetachstate error"); - exit(1); - } - else - { - if(stat == PTHREAD_CREATE_JOINABLE) - { - printf("joinable stat\n"); - } - else if(stat == PTHREAD_CREATE_DETACHED) - { - printf("detach stat\n"); - } - else - { - printf("error status_out\n"); - } - } -} -void* thfd(void *arg) -{ - int i; - int sum = 0; - for(i = 1; i <= 100; ++i) - { - sum += i; - } - return (void *)sum; -} - -int main() -{ - int err; - pthread_t default_th, detach_th; - pthread_attr_t attr;//设置线程属性 - - //对线程属性初始化 - pthread_attr_init(&attr); - //输出分离属性 - status_out(&attr); - - //以分离属性的默认值正常启动子线程 - if((err = pthread_create(&default_th, &attr, thfd, (void*)0)) != 0) - { - perror("pthread create error"); - exit(1); - } - int res; - //默认分离属性值启动的线程不会自动回收现成的资源,所以需要调用join回收 - if(pthread_join(default_th, (void *)&res) != 0) - { - perror("join error"); - exit(1); - } - else - { - printf("default_th return %d\n", (int)res); - } - printf("*************************************\n"); - - //设置分离属性为分离状态启动 - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - - status_out(&attr); - - if((err = pthread_create(&detach_th, &attr, thfd, (void *)0)) != 0) - { - perror("pthread_create error!"); - exit(1); - } - //这里再调用join运行成功但是会报错 - /*if((pthread_join(detach_th, (void *)&res)) != 0) - { - perror("join error"); - exit(1); - } - else - { - printf("detach_th return %d\n", (int)res); -}*/ - printf("*************************************\n"); - - //销毁线程属性 - pthread_attr_destroy(&attr); - printf("0x%lx finished!\n", pthread_self()); - - sleep(1); -} diff --git a/Linux_study/src/s.txt b/Linux_study/src/s.txt deleted file mode 100644 index e8702d5..0000000 --- a/Linux_study/src/s.txt +++ /dev/null @@ -1,4 +0,0 @@ -s:hello iotek, pid:4352 -pid: 4352, g_v: 40, a_v: 40, s_v; 40 -s:hello iotek, pid:4352 -pid: 4353, g_v: 50, a_v: 50, s_v; 50 diff --git a/Linux_study/src/s_fd.txt b/Linux_study/src/s_fd.txt deleted file mode 100755 index c74b670..0000000 --- a/Linux_study/src/s_fd.txt +++ /dev/null @@ -1 +0,0 @@ -hello iotek \ No newline at end of file diff --git a/Linux_study/src/signal_catch.c b/Linux_study/src/signal_catch.c deleted file mode 100644 index 35b3228..0000000 --- a/Linux_study/src/signal_catch.c +++ /dev/null @@ -1,37 +0,0 @@ -/************************************************************************* - > File Name: signal_catch.c - > Author: - > Mail: - > Created Time: 2017年10月02日 01时36分51秒 PDT - ************************************************************************/ - -#include -#include -#include -#include - -//定义信号处理函数 -void sig_handler(int signo) -{ - printf("%d, %d occured\n", getpid(), signo); -} -int main(void) -{ - //向内核登记信号处理函数以及信号值 - if(signal(SIGTSTP, sig_handler) == SIG_ERR) - { - perror("SIGTSTP error\n"); - } - if(signal(SIGINT, SIG_IGN) == SIG_ERR) - { - //忽略信号 - perror("SIGINT error\n"); - } - int i = 0 ; - while(i < 30) - { - printf("%d out %d\n", getpid(), i++); - sleep(1); - } - return 0; -} diff --git a/Linux_study/src/signal_sigchld.c b/Linux_study/src/signal_sigchld.c deleted file mode 100644 index d387b9e..0000000 --- a/Linux_study/src/signal_sigchld.c +++ /dev/null @@ -1,48 +0,0 @@ -/************************************************************************* - > File Name: signal_sigchld.c - > Author: - > Mail: - > Created Time: 2017年10月02日 03时57分18秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -void sig_handler(int signo) -{ - - printf("child process %d stop\n", signo); - wait(0); -} -void out(int n) -{ - for(int i = 0; i < n; ++i) - { - printf("%d out %d\n", getpid(), i); - sleep(1); - } -} -int main(void) -{ - if(signal(SIGCHLD, sig_handler) == SIG_ERR) - { - perror("sigchld error"); - exit(1); - } - pid_t pid = fork(); - if(pid < 0) - { - perror("fork error"); - exit(1); - } - else if(pid > 0) - { - out(100); - } - else - { - out(20); - } - return 0; -} diff --git a/Linux_study/src/text.txt b/Linux_study/src/text.txt deleted file mode 100644 index 93c0004..0000000 --- a/Linux_study/src/text.txt +++ /dev/null @@ -1,5 +0,0 @@ -cccbbb -aaaa -AAAAAcc:q -ccccccccccccccccccccccccccccccccc -cccccc \ No newline at end of file diff --git a/Socket/a.out b/Socket/a.out deleted file mode 100755 index 72eec93..0000000 Binary files a/Socket/a.out and /dev/null differ diff --git a/Socket/client.c b/Socket/client.c deleted file mode 100644 index 3f31f43..0000000 --- a/Socket/client.c +++ /dev/null @@ -1,85 +0,0 @@ -/************************************************************************* - > File Name: client.c - > Author: - > Mail: - > Created Time: 2017年08月15日 22时05分12秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -//#include"getstr.h" -#include - -int main(int argc, char *argv[]) -{ - int client_sockfd; - int len; - struct sockaddr_in remote_addr; //服务器端网络地址结构体 - char buf[BUFSIZ]; //数据传送的缓冲区 - memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零 - remote_addr.sin_family=AF_INET; //设置为IP通信 - remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址 - remote_addr.sin_port=htons(8000); //服务器端口号 - - /*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/ - if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0) - { - perror("socket"); - return 1; - } - - /*将套接字绑定到服务器的网络地址上*/ - if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0) - { - perror("connect"); - return 1; - } - printf("connected to server\n"); - len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息 - buf[len]='\0'; - printf("%s",buf); //打印服务器端信息 - - /*循环的发送接收信息并打印接收信息--recv返回接收到的字节数,send返回发送的字节数*/ - - while(1) - { - while(1) - { - printf("Send@:"); - gets(buf); - len=send(client_sockfd,buf,strlen(buf),0); - if(!strcmp(buf,"quit")) - break; - } - - while(1) - { - int len = recv(client_sockfd, buf, BUFSIZ, 0); - buf[len] = '\0'; - if(len > 0) - { - printf("received@: %s\n", buf); - } - if(!strcmp(buf,"quit")) - { - break; - } - if(len = 0) - { - exit(0); - } - } - /*while((len=recv(client_sockfd,buf,BUFSIZ,0)) > 0) - { - buf[len]='\0'; - printf("received:%s\n",buf); - }*/ - } - close(client_sockfd);//关闭套接字 - return 0; -} diff --git a/Socket/clt b/Socket/clt deleted file mode 100755 index 0fb8153..0000000 Binary files a/Socket/clt and /dev/null differ diff --git a/Socket/fork.c b/Socket/fork.c deleted file mode 100644 index 91aacb5..0000000 --- a/Socket/fork.c +++ /dev/null @@ -1,47 +0,0 @@ -/************************************************************************* - > File Name: fork.c - > Author: - > Mail: - > Created Time: 2017年08月15日 23时14分27秒 PDT - ************************************************************************/ - -#include -#include - -#define MAX_COUNT 10 - -void ChildProcess(void); /* child process prototype */ -void ParentProcess(void); /* parent process prototype */ - -void main(void) -{ - pid_t pid; - - pid = fork(); - if (pid == 0) - ChildProcess(); - else - ParentProcess(); -} - -void ChildProcess(void) -{ - int i; - - for (i = 1; i <= MAX_COUNT; i++) - printf(" This line is from child, value = %d\n", i); - printf(" *** Child process is done ***\n"); -} - -void ParentProcess(void) -{ - int i; - - for (i = 1; i <= MAX_COUNT; i++) - { - printf("This line is from parent, value = %d\n", i); - - sleep(1); - } - printf("*** Parent is done ***\n"); -} diff --git a/Socket/forktest1.c b/Socket/forktest1.c deleted file mode 100644 index 382cd73..0000000 --- a/Socket/forktest1.c +++ /dev/null @@ -1,56 +0,0 @@ -/************************************************************************* - > File Name: forktest1.c - > Author: - > Mail: - > Created Time: 2017年08月15日 23时22分06秒 PDT - ************************************************************************/ - -#include -#include -#include -//自测代码 - -void ChildProcess(int count); /* child process prototype */ -void ParentProcess(int count); - -int main() -{ - int memory = 20; - pid_t pid; - pid =fork(); - if(0 == pid) - { - //得到子进程的pid 和 他的父进程 pid 下同。 - printf("I am child! My pid is %d, My father pid is %d!\n", getpid(), getppid()); - ChildProcess(memory); - } - else if(pid < 0) //进程创建失败! - { - printf("error:创建失败!"); - } - - else - { - printf("I am Parent! My pid is %d, My father pid is %d!\n",getpid(), getppid()); - ParentProcess(memory); - } - -} - -void ChildProcess(int count) -{ - for(int i = 1; i < 21; ++i) - { - printf("ChildProcess第%d次: %d .\n",i, count+i); - } -} - -void ParentProcess(int count) -{ - for(int i = 1; i < 21; ++i) - { - printf("ParentProcess第%d次: %d .\n", i, count); - sleep(0.1); - } -} - diff --git a/Socket/ser b/Socket/ser deleted file mode 100755 index 8a54936..0000000 Binary files a/Socket/ser and /dev/null differ diff --git a/Socket_room/cli_room.c b/Socket_room/cli_room.c deleted file mode 100644 index c7214c5..0000000 --- a/Socket_room/cli_room.c +++ /dev/null @@ -1,107 +0,0 @@ -/************************************************************************* - > File Name: cli_room.c - > Author: - > Mail: - > Created Time: 2017年08月21日 03时07分58秒 PDT - -************************************************************************/ -#include"clientmsg.h" -void *func(void *arg); -void process_cli(int sockfd, struct CLIENTMSG clientMSG); - -struct ARG{ - int sockfd; - struct CLIENTMSG clientMSG; -}; -int main() -{ - int sockfd; - char ip[20];//存放ip地址 - int port;//端口变量 - pthread_t tid; - struct sockaddr_in ser_address; - struct CLIENTMSG clientmsgSEND; - struct ARG *arg; - //******socket***********// - sockfd = socket(AF_INET, SOCK_STREAM, 0); - assert(sockfd != -1); - //*******connect*********// - printf("Please input a ip address:\n");//读入客户端信息:ip、port、用户名 - scanf("%s", ip); - printf("Please input the port:\n"); - scanf("%d", &port); - bzero(&ser_address, sizeof(ser_address)); - ser_address.sin_family = AF_INET; - ser_address.sin_port = htons(port); - inet_aton(ip, &ser_address.sin_addr); - - int con = connect(sockfd, (struct sockaddr*)&ser_address, sizeof(ser_address)); - assert(con != -1); - - recv(sockfd, &clientmsgSEND, sizeof(clientmsgSEND), 0); - if(clientmsgSEND.OP == OK) - { - //创建一个线程 - arg = (struct ARG*)malloc(sizeof(struct ARG)); - arg->sockfd = sockfd; - pthread_create(&tid, NULL, func, (void *)arg); - //主线程 - printf("Please input your username:\n"); - scanf("%s", clientmsgSEND.username); - clientmsgSEND.OP = USER;//进入房间 - send(sockfd, &clientmsgSEND, sizeof(clientmsgSEND), 0); - while(1) - { - clientmsgSEND.OP = MSG;//标记为发送状态 - scanf("%s", clientmsgSEND.buf); - if(strcmp("bye", clientmsgSEND.buf) == 0) - { - clientmsgSEND.OP = EXIT;//退出聊天室 - send(sockfd, &clientmsgSEND, sizeof(clientmsgSEND), 0); - break; - } - send(sockfd, &clientmsgSEND, sizeof(clientmsgSEND), 0); - } - pthread_cancel(tid); - } - else - { - printf("警告:达到用户上限!"); - } - close(sockfd); - return 0; -} - -void *func(void *arg) -{ - struct ARG *info; - info = (struct ARG*)arg; - process_cli(info->sockfd, info->clientMSG); - free(arg); - pthread_exit(NULL); -} -void process_cli(int sockfd, struct CLIENTMSG clientMSG) -{ - int len; - while(1) - { - bzero(&clientMSG,sizeof(clientMSG)); - len = recv(sockfd, &clientMSG, sizeof(clientMSG), 0); - if(len > 0) - { - if(clientMSG.OP == USER) - { - printf("The user %s is login!", clientMSG.username); - } - else if(clientMSG.OP == EXIT) - { - printf("The user %s is logout!", clientMSG.username); - } - else - { - printf("%s:%s\n", clientMSG.username, clientMSG.buf); - } - } - } -} - diff --git a/Socket_room/client b/Socket_room/client deleted file mode 100755 index 0c978b9..0000000 Binary files a/Socket_room/client and /dev/null differ diff --git a/Socket_room/clientmsg.h b/Socket_room/clientmsg.h deleted file mode 100644 index 56d4780..0000000 --- a/Socket_room/clientmsg.h +++ /dev/null @@ -1,37 +0,0 @@ -/************************************************************************* - > File Name: clientmsg.h - > Author: - > Mail: - > Created Time: 2017年08月21日 03时01分10秒 PDT - ************************************************************************/ - -#ifndef _CLIENTMSG_H -//防止重复引用。 跨平台。 -#define _CLIENTMSG_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#define EXIT -1 -#define USER 1 -#define MSG 2 -#define OK 3 -#ifndef CMSGLEN -#define CMSGLEN 100 - -#endif -struct CLIENTMSG -{ - int OP;//用于标记状态 - char username[20];//存放用户名 - char buf[CMSGLEN];//接收用户输入的信息缓冲区 -}; -#endif diff --git a/Socket_room/ser_room.c b/Socket_room/ser_room.c deleted file mode 100644 index aee6085..0000000 --- a/Socket_room/ser_room.c +++ /dev/null @@ -1,155 +0,0 @@ -/************************************************************************* - > File Name: ser_room.c - > Author: mZ > Mail: - > Created Time: 2017年08月21日 20时29分18秒 PDT - ************************************************************************/ - -#include "clientmsg.h" -void *func(void *arg); - void communicate_process(int index); -struct Entity -{ - int sockfd; - char username[20]; - char buf[CMSGLEN]; - struct sockaddr_in cli_address; - int stat; -}; - -struct Entity ent[5]; -int main() -{ - struct sockaddr_in ser_address; - struct sockaddr_in cli_address; - char ip[20]; - int port; - struct CLIENTMSG clientMSG; - pthread_t tid; - int *arg; - int sock = socket(AF_INET, SOCK_STREAM, 0); - assert(sock != -1); - - /*输入服务器ip地址和端口号*/ - printf("Please input the ip address:\n"); - scanf("%s",ip); - printf("Please input the port:\n"); - scanf("%d", &port); - - bzero(&ser_address, sizeof(ser_address)); - ser_address.sin_port = htons(port); - ser_address.sin_family = AF_INET; - inet_aton(ip, &ser_address.sin_addr); - - int bindfd = bind(sock, (struct sockaddr*)&ser_address, sizeof(ser_address)); - assert(bindfd != -1); - - int len = listen(sock, 5); - assert(len != -1); - - for(int i = 0; i < 5; ++i) - { - ent[i].stat = 0;//初始化用户状态 - } - - while(1) - { - int addrlen = sizeof(cli_address); - int con = accept(sock, (struct sockaddr*)&cli_address, &addrlen); - assert(con != -1); - - int index = 5; - for(int i = 0; i - < 5; ++i) - { - if(ent[i].stat == 0) - { - index = i; - } - } - - if(index <= 4) - { - printf("connetfd: %d\n", con); - ent[index].cli_address = cli_address; - ent[index].sockfd = con; - ent[index].stat = 1; - arg = malloc(sizeof(int)); - *arg = index; - pthread_create(&tid, NULL, func, (void*)arg); - } - else - { - bzero(&clientMSG,sizeof(clientMSG)); - clientMSG.OP = EXIT; - send(con, &clientMSG, sizeof(clientMSG), 0); - close(con); - } - } - close(sock); -} -void *func(void *arg) -{ - int *info ; - info = (int *)arg; - communicate_process( *info); - pthread_exit(NULL); - } - void communicate_process(int index) - { - struct CLIENTMSG sendMsg; - struct CLIENTMSG recvMsg; - printf("sockfd:%d\n",ent[index].sockfd ); - sendMsg.OP = OK; - send(ent[index].sockfd,&sendMsg,sizeof(sendMsg),0); - - while(1){ - bzero(&sendMsg,sizeof(sendMsg)); - bzero(&recvMsg,sizeof(recvMsg)); - int len =recv(ent[index].sockfd,&recvMsg,sizeof(recvMsg),0); - if(len > 0) - { - if(recvMsg.OP == USER) - { - printf("user %s login from ip:%s,port:%d\n",recvMsg.username,inet_ntoa(ent[index].cli_address.sin_addr),ntohs(ent[index].cli_address.sin_port) ); - bcopy(recvMsg.username,ent[index].username,strlen(recvMsg.username)); - sendMsg.OP = USER; - } - else if(recvMsg.OP == EXIT) - { - printf("user %s is logout\n",recvMsg.username ); - sendMsg.OP = EXIT; - ent[index].stat = 0; - int i; - for(i=0;i<5;i++) - { - if(ent[i].stat == 1) - { - - send(ent[i].sockfd,&sendMsg,sizeof(sendMsg),0); - } - } - break; - } - else if(recvMsg.OP == MSG) - { - sendMsg.OP = MSG; - } - bcopy(recvMsg.username,sendMsg.username,strlen(recvMsg.username)); - bcopy(recvMsg.buf,sendMsg.buf,strlen(recvMsg.buf)); - int i; - for(i=0;i<5;i++) - { - if(ent[i].stat == 1) - { - printf("stat 1...\n"); - send(ent[i].sockfd,&sendMsg,sizeof(sendMsg),0); - } - } - } - else - { - continue; - } - } - } - diff --git a/Socket_room/server b/Socket_room/server deleted file mode 100755 index 7c6cd09..0000000 Binary files a/Socket_room/server and /dev/null differ diff --git a/big_little.c b/big_little.c new file mode 100644 index 0000000..d562321 --- /dev/null +++ b/big_little.c @@ -0,0 +1,37 @@ +/************************************************************************* + > File Name: big_little.c + > Author: + > Mail: + > Created Time: 2017年08月18日 03时42分25秒 PDT + ************************************************************************/ + +#include +void byteorder(); +//测试大小端 +int main() +{ + byteorder(); + +} + +void byteorder() +{ + union + { + short value; + char union_bytes[sizeof(short)]; + }test; + test.value = 0x0102; + if(test.union_bytes[0] == 1 && test.union_bytes[1] == 2) + { + printf("big"); + } + else if(test.union_bytes[0] == 2 && test.union_bytes[1] == 1) + { + printf("little"); + } + else + { + printf("unknow"); + } +} diff --git a/c_homework/Person.c b/c_homework/Person.c deleted file mode 100644 index 467d10c..0000000 --- a/c_homework/Person.c +++ /dev/null @@ -1,120 +0,0 @@ -/************************************************************************* - > File Name: Person.c - > Author: - > Mail: - > Created Time: 2017年08月29日 20时03分21秒 PDT - ************************************************************************/ - -#include -#include -void save(int m); -typedef void (*Pgedu)(); -typedef void (*Psedu)(); -#define Per \ - char name[20];\ - bool isfame;\ - int age; - -struct Person -{ - Per; -}; -struct Student -{ - Per; - int Edu; - int number; - Pgedu pgedu;//函数指针 - Psedu psedu;//函数指针 -}S, *p; -enum Edu -{ - primary, - middle, - high, - university -}Select; -void setEdu() -{ - printf("请按照数字提示选择你的学历:\n"); - printf("0: primary\n1: middle\n2: high\n3: university\n"); - printf("请输入你的选择:\n"); - int m; - scanf("%d", &m); - switch(m) - { - case 0: - save(m);break; - case 1: - save(m);break; - case 2: - save(m);break; - case 3: - save(m);break; - } -} -void getEdu() -{ - FILE *fp; - p = &S; - if((fp = fopen("student.txt", "rb")) == NULL) - { - printf("Connot open the file!\n"); - return; - } - rewind(fp); - printf("\nname(姓名) sex(性别) age(年龄) 学历\n"); - fread(p, sizeof(struct Student), 1, fp); - printf("%s %d %d %d\n", p->name, p->isfame, p->age, p->Edu); - -} - -void save(int m) -{ - printf("please input students' informations:\n"); - printf("name: "); - scanf("%s",S.name); - printf("\nsex(性别)(1.女生 0.男生): "); - scanf("%d", &S.isfame); - printf("\nage(年龄): "); - scanf("%d", &S.age); - //scanf("%s%d%d",S.name, &S.isfame, &S.age); - S.Edu = m; - - FILE *fp; - if((fp = fopen("student.txt", "wb"))== NULL) - { - printf("open file failed!\n"); - return; - } - if(fwrite(&S, sizeof(S), 1, fp) == 0) - { - printf("file write error!\n"); - } -} -int main() -{ - struct Student STU; - - STU.pgedu = getEdu;//函数指针指向函数体 - STU.psedu = setEdu; - while(1) - { - printf("请选择你要进行的操作:\n"); - printf("\n1: 录入学生信息\n2: 修改信息\n3: 查看信息\n4: 退出\n"); - int m; - scanf("%d", &m); - switch(m) - { - case 1: - STU.psedu();break; - case 2: - STU.psedu();break; - case 3: - STU.pgedu();break; - case 4: - return 0;break; - } - } - return 0; -} diff --git a/c_homework/array_A.c b/c_homework/array_A.c deleted file mode 100644 index fb64b1d..0000000 --- a/c_homework/array_A.c +++ /dev/null @@ -1,64 +0,0 @@ -/************************************************************************* - > File Name: array_A.c - > Author: - > Mail: - > Created Time: 2017年08月20日 18时19分18秒 PDT - ************************************************************************/ - -#include -//homework3 数组A中的0移至末尾,非零整数有序排列I -/*感觉把数组元素降序排列就好了*/ -//#define nSIZE 10 -int Func(int *A, int nSIZE); -void swap(int *a, int *b); -int main() -{ - int A[] = {1, [3] = 3, [6] = 8}; - int nSIZE = sizeof(A) / sizeof(int); - - printf("排序前:\n"); - for(int i = 0; i < nSIZE; ++i) - { - printf("%d ", A[i]); - } - putchar(10); - - int count = Func(A, nSIZE); - - for(int i = 0; i < nSIZE; ++i) - { - printf("%d ", A[i]); - } - - putchar(10); - - printf("数组的第一个0直对应的下标为: %d\n", count); - - return 0; -} - -int Func(int *A, int nSIZE) -{ - for(int i = 0; i < nSIZE - 1; ++i) - { - for(int j = 0; j < nSIZE - i - 1; ++j) - { - if(A[j] < A[j+1]) - { - swap(&A[j], &A[j+1]); - } - } - } - for(int i = 0; i < nSIZE; ++i) - { - if(A[i] == 0) - return i; - } -} - -void swap(int *a, int *b) -{ - int temp = *a; - *a = *b; - *b = temp; -} diff --git a/c_homework/array_A1.c b/c_homework/array_A1.c deleted file mode 100644 index c4d201c..0000000 --- a/c_homework/array_A1.c +++ /dev/null @@ -1,76 +0,0 @@ -/************************************************************************* - > File Name: array_A1.c - > Author: - > Mail: - > Created Time: 2017年08月20日 23时31分10秒 PDT - ************************************************************************/ - -#include -//原来忘了考虑数组中存在负数怎么办 -void swap(int *a, int *b); -void bubble(int *A, int nSize); -int Func(int *A, int nSize); -int main() -{ - int array[] = {-1, [2] = 0, 4, 9, [8] = -2}; - int arrlen = sizeof(array)/sizeof(int); - //printf("arrlen: %d\n", arrlen); - - printf("处理前:\n"); - for(int i = 0; i < arrlen; ++i) - { - printf("%d ", array[i]); - } - putchar(10); - Func(array, arrlen); - - printf("处理后:\n"); - for(int i = 0; i < arrlen; ++i) - { - printf("%d ", array[i]); - } - putchar(10); - - return 0; -} -int Func(int *A, int nSize) -{ - int flag = 0; - for(int i = 0; i < nSize - flag; ++i) - { - flag = 0; - if(A[i] == 0) - { - flag++; - while(A[nSize - flag] == 0)//防止存在0已经在末尾 - { - flag ++; - } - if(i <= nSize - flag)//交换以后的不再交换。 - swap(&A[i], &A[nSize - flag]); - } - } - printf("处理后第一个0位置下标: %d\n", flag - 1); - bubble(A, nSize - flag + 1); - -} -void bubble(int *A, int nSize) -{ - for(int i = 0; i < nSize - 1; ++i) - { - for(int j = 0; j < nSize - i -1; ++j) - { - if(A[j] < A[j + 1]) - { - swap(&A[j], &A[j + 1]); - } - - } - } -} -void swap(int *a, int *b) -{ - int temp = *a; - *a = *b; - *b = temp; -} diff --git a/c_homework/huiwen.c b/c_homework/huiwen.c deleted file mode 100644 index 4432aa4..0000000 --- a/c_homework/huiwen.c +++ /dev/null @@ -1,53 +0,0 @@ -/************************************************************************* - > File Name: huiwen.c - > Author: - > Mail: - > Created Time: 2017年08月20日 18时03分25秒 PDT - ************************************************************************/ - -#include -#include -int fun(char *src); -#define MAX 100 -//判断字符串是否回文 -int main() -{ - char str[MAX]; - - printf("请输入字符串:\n"); - scanf("%s", str); - - int a = fun(str); - if(a == 1) - { - printf("是回文字符串!"); - } - if(a == 0) - { - printf("不是回文字符串!"); - } - - return 0; -} - -int fun(char *src) -{ - int len = strlen(src); - char temp[len]; - for(int i = 0; i < len; ++i) - { - temp[i] = src[len-i-1]; - } - int count = strcmp(temp, src); - - if(count == 0) - { - return 1; - } - - else - { - return 0; - } - -} diff --git a/c_homework/return.c b/c_homework/return.c deleted file mode 100644 index c8f8a70..0000000 --- a/c_homework/return.c +++ /dev/null @@ -1,27 +0,0 @@ -/************************************************************************* - > File Name: return.c - > Author: - > Mail: - > Created Time: 2017年08月27日 12时03分27秒 PDT - ************************************************************************/ - -#include -//int fun(); -int *fun() -{ - int a = 10; - return a; -} -//char *fun(); -int main(){ - // for(int i = 0; i < 2; ++i) - printf("%d\n", *fun()); - int *b = *fun(); - printf("%d\n", *b); - return 0; -} -/*int fun() -{ - int a[2] = {1,2}; - return *a; -}*/ diff --git a/c_homework/size.c b/c_homework/size.c deleted file mode 100644 index 43ebaf5..0000000 --- a/c_homework/size.c +++ /dev/null @@ -1,16 +0,0 @@ -/************************************************************************* - > File Name: size.c - > Author: - > Mail: - > Created Time: 2017年08月27日 10时18分38秒 PDT - ************************************************************************/ - -#include -int main() -{ - char *a = "abcd"; - /// int *b = a; - printf("sizeof(a) = %lu", sizeof(a)); - printf("%s", a); - //printf("%d", a); -} diff --git a/c_homework/strcmp.c b/c_homework/strcmp.c deleted file mode 100644 index e670415..0000000 --- a/c_homework/strcmp.c +++ /dev/null @@ -1,44 +0,0 @@ -/************************************************************************* - > File Name: strcmp.c - > Author: - > Mail: - > Created Time: 2017年08月20日 17时51分18秒 PDT - ************************************************************************/ - -#include -#include -//比较字符串大小 -int strcmp(const char *src, const char *dst); -int main() -{ - char *src = "abcdefgh"; - char *dst = "abcdefghb"; - - int count = strcmp(src, dst); - - printf("%d\n", count); - - if(count < 0) - { - printf("第一个字符串大!\n"); - } - else if(count == 0) - { - printf("两个字符串一样大!\n"); - } - else - { - printf("第二个字符串大!\n"); - } - - return 0; -} - -int strcmp(const char *src, const char *dst) -{ - assert(NULL != src && NULL != dst); - - while(*src && *dst && *src++ == *dst++); - - return *src - *dst; -} diff --git a/c_homework/student.txt b/c_homework/student.txt deleted file mode 100644 index 364eeaf..0000000 Binary files a/c_homework/student.txt and /dev/null differ diff --git a/c_stack/-g b/c_stack/-g deleted file mode 100755 index 4624a71..0000000 Binary files a/c_stack/-g and /dev/null differ diff --git a/c_stack/Node.c b/c_stack/Node.c deleted file mode 100644 index f3d2ac9..0000000 --- a/c_stack/Node.c +++ /dev/null @@ -1,71 +0,0 @@ -/************************************************************************* - > File Name: Node.c - > Author: - > Mail: - > Created Time: 2017年09月22日 23时41分57秒 PDT - ************************************************************************/ - -#include -#include -typedef int ElementType; -typedef struct node{ - ElementType data; - struct node *next; -}Node; - -typedef Node* List; - -//创建新链表 -List MakeEmpty(){ - List link_table = (List)malloc(sizeof(struct node)); - link_table->data = 0; - link_table->next = NULL; - return link_table; -} -//打印链表 -void Print_Table(List L){ - List temp = L; - while(temp != NULL){ - printf("%d|", temp->data); - temp = temp->next; - } - putchar(10); -} -//向链表中插入数据 -void Insert_Node(List L, ElementType e){ - List new_node; - new_node = (List)malloc(sizeof(struct node)); - new_node->data = e; - while(L->next != NULL){ - L = L->next; - } - new_node->next = L->next; - L->next = new_node; -} - -//头插 -void Head_Insert(List *H, ElementType e){ - List new_node; - new_node = (List)malloc(sizeof(struct node)); - new_node->data = e; - new_node->next = *H; - *H = new_node; -} -//delet ElementType -void Dele_Elem(List L, ElementType e){ - if(L->data == e){ - L->next = NULL; - free(L); - } - while(L->next->data != e){ - L = L->next; - } -} -int main() -{ - List L = MakeEmpty(); - Insert_Node(L, 10); - Head_Insert(&L, 11); - Print_Table(L); - return 0; -} diff --git a/c_stack/stack_test.c b/c_stack/stack_test.c deleted file mode 100644 index 7797673..0000000 --- a/c_stack/stack_test.c +++ /dev/null @@ -1,107 +0,0 @@ -/************************************************************************* - > File Name: stack_test.c - > Author: - > Mail: - > Created Time: 2017年10月26日 20时17分13秒 PDT - ************************************************************************/ - -#include -#include - -typedef struct{ - int *data; - int top; -}Stack; - -//栈初始化 -Stack *stack_init(int stack_size){ - Stack *p; - p = (Stack *)malloc(sizeof(Stack)); - if((p->data = (int *)malloc(stack_size * sizeof(int))) != 0){ - //printf("comed!\n"); - p->top = 0; - printf("init top = %d\n", p->top); - return p; - } - printf("申请内存失败!\n"); - return NULL; -} - -//数据入栈 -int stack_push(Stack *stack, int array_len, int data_arg){ - if(((stack->top) + 1) > array_len){ - printf("栈满\n"); - return 0; - }else{ - //printf("comed!\n"); - //printf("top = %d\n", stack->top); - stack->top ++; - //printf("top ++ = %d\n", stack->top); - stack->data[stack->top] = data_arg; - return 1; - } -} - -//数据出栈 -int stack_pop(Stack *stack){ - if(stack->top == -1){ - perror("栈空"); - return 0; - }else{ - return (stack->data[stack->top--]); - } -} -//获取栈顶元素 -int getdata_top(Stack *stack){ - if(stack->top == 0){ - perror("栈空"); - return 0; - }else{ - return (stack->data[stack->top]); - } -} - -//清空栈 -void stack_clear(Stack* stack){ - stack->top = 0; -} -//释放栈空间 -void stack_free( - Stack *stack){ - if(stack){ - free(stack); - } -} -int main(void) -{ - int array_len; - printf("请输入你想申请的栈数组大小:\n"); - scanf("%d", &array_len); - - Stack *stack = stack_init(array_len); - printf("main top = %d\n", stack->top); - printf("stack %p\n", stack); - - //数据入栈 - int temp; - //这里我只测试5个数,count作为计数器 - int count1 = 0; - int count2 = 0; - do{ - printf("请输入数据:\n"); - scanf("%d", &temp); - stack_push(stack, array_len, temp); - count1 ++; - }while(count1 <= 5); - - do{ - printf("请按任意键进行出栈操作:\n"); - getchar(); - int pop = stack_pop(stack); - printf("出栈的数据是%d\n", pop); - count2++; - }while(count2 <= 5); - - - return 0; -} diff --git "a/c_stack/\346\240\207\350\257\206\347\254\246.c" "b/c_stack/\346\240\207\350\257\206\347\254\246.c" deleted file mode 100644 index 05265cb..0000000 --- "a/c_stack/\346\240\207\350\257\206\347\254\246.c" +++ /dev/null @@ -1,23 +0,0 @@ -/************************************************************************* - > File Name: 标识符.c - > Author: - > Mail: - > Created Time: 2017年08月28日 23时09分00秒 PDT - ************************************************************************/ - -#include -#include -//命名空间不同。可以起相同名称的标识符 -struct pin -{ - char pin[20]; - /*...*/ -}; -int _Blll(struct pin *pin) -{ - int len = strlen(pin->pin); -} -int main() -{ - -} diff --git "a/io\345\244\215\347\224\250/a.out" "b/io\345\244\215\347\224\250/a.out" deleted file mode 100755 index f36dae1..0000000 Binary files "a/io\345\244\215\347\224\250/a.out" and /dev/null differ diff --git "a/io\345\244\215\347\224\250/client" "b/io\345\244\215\347\224\250/client" deleted file mode 100755 index f36dae1..0000000 Binary files "a/io\345\244\215\347\224\250/client" and /dev/null differ diff --git "a/io\345\244\215\347\224\250/client.c" "b/io\345\244\215\347\224\250/client.c" deleted file mode 100644 index ef06137..0000000 --- "a/io\345\244\215\347\224\250/client.c" +++ /dev/null @@ -1,84 +0,0 @@ -/************************************************************************* - > File Name: client.c - > Author: - > Mail: - > Created Time: 2017年08月15日 22时05分12秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -//#include"getstr.h" -#include - -int main(int argc, char *argv[]) -{ - int client_sockfd; - int len; - struct sockaddr_in remote_addr; //服务器端网络地址结构体 - char buf[BUFSIZ]; //数据传送的缓冲区 - memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零 - remote_addr.sin_family=AF_INET; //设置为IP通信 - remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址 - remote_addr.sin_port=htons(8000); - /*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/ - if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0) - { - perror("socket"); - return 1; - } - - /*将套接字绑定到服务器的网络地址上*/ - if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0) - { - perror("connect"); - return 1; - } - printf("connected to server\n"); - len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息 - buf[len]='\0'; - printf("%s",buf); //打印服务器端信息 - - /*循环的发送接收信息并打印接收信息--recv返回接收到的字节数,send返回发送的字节数*/ - - while(1) - { - while(1) - { - printf("Send@:"); - gets(buf); - len=send(client_sockfd,buf,strlen(buf),0); - if(!strcmp(buf,"quit")) - break; - } - - while(1) - { - int len = recv(client_sockfd, buf, BUFSIZ, 0); - buf[len] = '\0'; - if(len > 0) - { - printf("received@: %s\n", buf); - } - if(!strcmp(buf,"quit")) - { - break; - } - if(len = 0) - { - exit(0); - } - } - /*while((len=recv(client_sockfd,buf,BUFSIZ,0)) > 0) - { - buf[len]='\0'; - printf("received:%s\n",buf); - }*/ - } - close(client_sockfd);//关闭套接字 - return 0; -} diff --git "a/io\345\244\215\347\224\250/io.h" "b/io\345\244\215\347\224\250/io.h" deleted file mode 100644 index 271c3bb..0000000 --- "a/io\345\244\215\347\224\250/io.h" +++ /dev/null @@ -1,25 +0,0 @@ -/************************************************************************* - > File Name: io.h - > Author: - > Mail: - > Created Time: 2017年08月27日 22时45分41秒 PDT - ************************************************************************/ - -#ifndef _IO_H -#define _IO_H - -#include -#define MAX 20 -struct Sock -{ - char username[MAX]; - int OB; - char buffer[1024]; -}; -struct SerMsg -{ - int OP; - char username[20]; - char buf[1024]; -}SERVER[FD_SETSIZE]; -#endif diff --git "a/io\345\244\215\347\224\250/io_client.c" "b/io\345\244\215\347\224\250/io_client.c" deleted file mode 100644 index 87c49a3..0000000 --- "a/io\345\244\215\347\224\250/io_client.c" +++ /dev/null @@ -1,139 +0,0 @@ -/************************************************************************* - > File Name: io_client.c - > Author: - > Mail: - > Created Time: 2017年08月24日 22时21分44秒 PDT - ************************************************************************/ - -#include "io_select.h" -#include "io.h" -int main(int argc, char *argv[]) -{ - int connectfd; - struct sockaddr_in seraddr;//服务器网络地址结构体 - - struct Sock my; - char sendbuff[1024] = {0}; - char recvbuff[1024] = {0}; - printf("Prease input the ip:\n"); - scanf("%s", my.ip); - printf("Please input the port:\n"); - scanf("%d", &my.port); - printf("Please input the username\n"); - scanf("%s", my.username); - - bzero(&seraddr, sizeof(seraddr)); - seraddr.sin_family = AF_INET; - seraddr.sin_port = htons(my.port); - inet_aton(my.ip, &seraddr.sin_addr); - - connectfd = socket(AF_INET, SOCK_STREAM, 0); - assert(connectfd != -1); - - int conn = connect(connectfd, (struct sockaddr*)&seraddr, sizeof(struct sockaddr)); - if(conn == -1) - { - perror("connect!"); - return 1; - } - fd_set rset; - FD_ZERO(&rset); - int nready = 0; - int maxfd; - int std = fileno(stdin); - if(connectfd > std) - { - maxfd = connectfd; - } - else - maxfd = std; - while(1) - { - FD_SET(connectfd, &rset); - FD_SET(std, &rset); - nready = select(maxfd+1, &rset, NULL, NULL, NULL); - if(nready == -1) - { - perror("select!"); - } - if(nready == 0) - { - continue; - } - if(FD_ISSET(connectfd, &rset)) - { - int recvbytes = recv(connectfd, recvbuff, sizeof(recvbuff), 0); - if(recvbytes == -1) - { - perror("recv"); - } - if(recvbytes == 0) - { - printf("server close!"); - exit(EXIT_FAILURE); - } - fputs(recvbuff ,stdout); - memset(recvbuff, 0 , sizeof(recvbuff)); - } - if(FD_ISSET(std, &rset)) - { - if(fgets(sendbuff, sizeof(sendbuff), stdin) == NULL) - { - break; - } - send(connectfd, sendbuff, sizeof(sendbuff), 0); - memset(sendbuff, 0, sizeof(sendbuff)); - } - } - close(connectfd); - return 0; -} - /*while(1)//服务器和客户端循环通信,互相通信 - { - while(1) - { - printf("Send@:"); - gets(buff); - int len=send(connectfd,buff,strlen(buff),0); - // bzero(buff, len); - if(!strcmp(buff,"bye")) - // len = recv(connectfd, buff, BUFSIZ, 0); - //buff[len] = '\0'; - // printf("%s\n", buff); - break; - } - - while(1) - { - int len = recv(connectfd, buff, BUFSIZ, 0); - buff[len] = '\0'; - if(len > 0) - { - printf("received@: %s\n", buff); - } - if(!strcmp(buff,"bye")) - { - break; - } - if(len == 0) - { - exit(0); - } - } - }*/ - - //服务器和客户端通信,服务器把客户端的消息原样返回 - - /*while(fgets(sendbuff, sizeof(sendbuff), stdin) != NULL) - { - send(connectfd, sendbuff, sizeof(sendbuff), 0); - if(strcmp(sendbuff, "bye\n") == 0) - { - break; - } - recv(connectfd, recvbuff, sizeof(recvbuff), 0); - fputs(recvbuff, stdout); - - memset(sendbuff, 0, sizeof(sendbuff)); - memset(recvbuff, 0, sizeof(recvbuff)); - }*/ diff --git "a/io\345\244\215\347\224\250/io_client2.c" "b/io\345\244\215\347\224\250/io_client2.c" deleted file mode 100644 index 3aca3c2..0000000 --- "a/io\345\244\215\347\224\250/io_client2.c" +++ /dev/null @@ -1,101 +0,0 @@ -/************************************************************************* - > File Name: io_client2.c - > Author: - > Mail: - > Created Time: 2017年08月27日 23时10分05秒 PDT - ************************************************************************/ - -#include "io_select.h" -#include "io.h" -int main(int argc, char *argv[]) -{ - int connectfd; - struct sockaddr_in seraddr;//服务器网络地址结构体 - - struct Sock my; - char sendbuff[1024] = {0}; - char recvbuff[1024] = {0}; - int port; - char ip[20]; - char username[20]; - printf("Prease input the ip:\n"); - scanf("%s", ip); - printf("Please input the port:\n"); - scanf("%d", &port); - printf("Please input the username\n"); - scanf("%s", username); - - bzero(&seraddr, sizeof(seraddr)); - seraddr.sin_family = AF_INET; - seraddr.sin_port = htons(port); - inet_aton(ip, &seraddr.sin_addr); - - connectfd = socket(AF_INET, SOCK_STREAM, 0); - assert(connectfd != -1); - - int conn = connect(connectfd, (struct sockaddr*)&seraddr, sizeof(struct sockaddr)); - if(conn == -1) - { - perror("connect!"); - return 1; - } - send(connectfd, username, sizeof(username), 0); - fd_set rset; - FD_ZERO(&rset); - int nready = 0; - int maxfd; - int std = fileno(stdin); - if(connectfd > std) - { - maxfd = connectfd; - } - else - { - maxfd = std; - } - FD_SET(connectfd, &rset); - FD_SET(std, &rset); - while(1) - { - nready = select(maxfd+1, &rset, NULL, NULL, NULL); - if(nready == -1) - { - perror("select!"); - } - if(nready == 0) - { - continue; - } - if(FD_ISSET(connectfd, &rset)) - { - int recvbytes = recv(connectfd, recvbuff, sizeof(recvbuff), 0); - if(recvbytes == -1) - { - perror("recv"); - } - if(recvbytes == 0) - { - printf("server close!"); - exit(EXIT_FAILURE); - } - fputs(recvbuff ,stdout); - memset(recvbuff, 0 , sizeof(recvbuff)); - } - if(FD_ISSET(std, &rset)) - { - if(fgets(sendbuff, sizeof(sendbuff), stdin) == NULL) - { - break; - } - send(connectfd, sendbuff, sizeof(sendbuff), 0); - if(strcmp("bye", sendbuff) == 0)//结束连接 - { - break; - } - memset(sendbuff, 0, sizeof(sendbuff)); - } - } - close(connectfd); - return 0; -} - diff --git "a/io\345\244\215\347\224\250/io_select.c" "b/io\345\244\215\347\224\250/io_select.c" deleted file mode 100644 index ce16744..0000000 --- "a/io\345\244\215\347\224\250/io_select.c" +++ /dev/null @@ -1,216 +0,0 @@ -/************************************************************************* - > File Name: io_select.c - > Author: - > Mail: - > Created Time: 2017年08月24日 21时19分11秒 PDT - ************************************************************************/ - -#include "io_select.h" -#include "io.h" -#define IOMAXSIZE 1024 -//#define MAXCOUNT 10 -//io复用select 的使用 -//服务器 -int main(int argc, char *argv[]) -{ - int listenfd;//监听套接字 - int connectfd;//连接以后的套接字,收发消息 - struct sockaddr_in seraddr;//服务器网络地址结构体 - struct sockaddr_in cliaddr;//客户端网络地址结构体 - - int port;//端口变量 - char ip[20];//用于接收ip地址 - printf("Please input the ip:\n"); - scanf("%s", ip); - printf("Please input the port:\n"); - scanf("%d", &port); - /*printf("Please input your username:\n"); - scanf("%s", username);*/ - - bzero(&seraddr, sizeof(seraddr)); - seraddr.sin_family = AF_INET;//协议族 - seraddr.sin_port = ntohs(port);//端口,大端转成小端 - inet_pton(AF_INET, ip, &seraddr.sin_addr); //ip地址 - - - /*------------------创建套接字-------------*/ - listenfd = socket(AF_INET, SOCK_STREAM, 0);//创建套接字 - assert(listenfd != -1); - - /*-------------绑定ip和端口------------------*/ - int optval = 1; - if(setsockopt(listenfd, SOCK_STREAM, SO_REUSEADDR, &optval, sizeof(optval)) < 0) - {//网络地址复用 - perror("setsockopt"); - } - int m = bind(listenfd, (struct sockaddr*)&seraddr, sizeof(seraddr));//绑定ip port; - assert(m != -1); - - char buf[IOMAXSIZE]; - char sendbuff[IOMAXSIZE]; - - /*------------进程数组-------------------*/ - int sockcount[FD_SETSIZE]; - for(int i = 0; i < FD_SETSIZE; ++i) - { - sockcount[i] = -1;//等于-1是因为进程描述符是从0开始的,负数表示没有该进程 - } - - - /*------------------监听套接字-----------------*/ - int lis = listen(listenfd, 5); - printf("listenfd = %d\n", listenfd); - assert(lis != -1); - socklen_t cliaddr_len; - - fd_set lisset, recset;//相当于声明了两个文件描述符集合 - FD_ZERO(&lisset);//清空lisset描述符集合 - FD_ZERO(&recset); - int maxfd;//select参数一,当前的最大描述符,集合中所有描述符的范围 - maxfd = listenfd; - int nready = 0;//select的返回值 - int i = 0; - - int count = 0; - while(1) - { - lisset = recset; - FD_SET(listenfd, &lisset);//将监听的文件描述符加入到监听集合中 - nready = select(maxfd+1, &lisset, NULL, NULL, NULL); - if(nready < 0) - { - perror("select"); - return 1; - } - if(nready == 0) - { - continue; - } - if(FD_ISSET(listenfd, &lisset)) - { - cliaddr_len = sizeof(cliaddr); - connectfd = accept(listenfd, (struct sockaddr*)&cliaddr, &cliaddr_len); - printf("connectfd = %d\n", connectfd); - if(connectfd == -1) - { - perror("accept!"); - return 1; - } - for(i = 0; i < FD_SETSIZE; ++i) - { - if(sockcount[i] < 0) - { - sockcount[i] = connectfd; //将刚刚监听进来的客户端进程唤醒 - break; - } - } - if(i == FD_SETSIZE) - { - fprintf(stderr ,"达到连接上限,与服务器连接失败!"); - exit(EXIT_FAILURE); - } - printf("IP = %s PORT = %d connect successful!\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); - count ++; - printf("当前连接人数: %d\n", count); - FD_SET(connectfd, &recset); - if(connectfd > maxfd) - { - maxfd = connectfd;//因为connectfd为新生成的文件描述符,maxfd要始终保持最大范围 - } - /*if(--nready <= 0)//???? - { - continue; - }*/ - } - for( i = 0; i < FD_SETSIZE; ++i) - { - //printf("dhskas"); - connectfd = sockcount[i];//将新进来的连接唤醒 - if(sockcount[i]== -1) - { - continue; - } - if(FD_ISSET(connectfd, &lisset)) - { - int recvbytes = recv(connectfd, buf, sizeof(buf), 0); - if(recvbytes == -1) - { - perror("recv"); - } - if(recvbytes == 0) - { - printf("client close!\n"); - FD_CLR(connectfd, &recset); - sockcount[i] = -1; - count --; - printf("当前连接人数:%d\n", count); - } - - for( i = 0; i < FD_SETSIZE; ++i) - { - if(sockcount[i] > 0 && sockcount[i] != connectfd) - { - send(sockcount[i], buf, recvbytes-1, 0); - } - } - printf("recv the data[%d]: %s", recvbytes, buf); - if(-nready <= 0) - { - break; - - } - - } - } - } - return 0; -} - - /* while(1)//服务器与客户端通信,互相发送消息就像两个客户通信一样 - { - while(1) - { - int len = recv(connectfd,buf,BUFSIZ,0); - buf[len]='\0'; - if(len > 0) - { - printf("received@: %s\n",buf); - } - if(!strcmp(buf,"bye")) - { - break; - } - // gets(buf); - - //len = send(connectfd,buf,sizeof(buf), 0); - - if(len == 0) - { - exit(0); - } - } - while(1) - { - printf("send@:"); - gets(buf); - int len = send(connectfd,buf,sizeof(buf), 0); - if(!strcmp(buf, "bye")) - { - break; - } - } - }*/ - - //客户端和服务器通信。服务器把客户端信息原样返回 - /*while(1) - { - memset(buf, 0, sizeof(buf)); - int len = recv(connectfd, buf, sizeof(buf), 0); - if(strcmp(buf, "bye\n") == 0) - break; - - fputs(buf, stdout); - send(connectfd, buf, sizeof(buf), 0); - - }*/ - //用select处理多客户端与服务器通信上限1024 diff --git "a/io\345\244\215\347\224\250/io_select.h" "b/io\345\244\215\347\224\250/io_select.h" deleted file mode 100644 index 382146e..0000000 --- "a/io\345\244\215\347\224\250/io_select.h" +++ /dev/null @@ -1,21 +0,0 @@ -/************************************************************************* - > File Name: io_select.h - > Author: - > Mail: - > Created Time: 2017年08月24日 21时35分21秒 PDT - ************************************************************************/ - -#ifndef _IO_SELECT_H -#define _IO_SELECT_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif diff --git "a/io\345\244\215\347\224\250/io_server.c" "b/io\345\244\215\347\224\250/io_server.c" deleted file mode 100644 index 595c42d..0000000 --- "a/io\345\244\215\347\224\250/io_server.c" +++ /dev/null @@ -1,176 +0,0 @@ -/************************************************************************* - > File Name: io_server.c - > Author: - > Mail: - > Created Time: 2017年08月27日 22时59分29秒 PDT - ************************************************************************/ - -#include "io_select.h" -#include "io.h" -#define IOMAXSIZE 1024 -//#define MAXCOUNT 10 -//io复用select 的使用 -//服务器 -int main(int argc, char *argv[]) -{ - int listenfd;//监听套接字 - int connectfd;//连接以后的套接字,收发消息 - struct sockaddr_in seraddr;//服务器网络地址结构体 - struct sockaddr_in cliaddr;//客户端网络地址结构体 - - int port;//端口变量 - char ip[20];//用于接收ip地址 - // char username[20]; - printf("Please input the ip:\n"); - scanf("%s", ip); - printf("Please input the port:\n"); - scanf("%d", &port); - /*printf("Please input your username:\n"); - scanf("%s", username);*/ - - - bzero(&seraddr, sizeof(seraddr)); - seraddr.sin_family = AF_INET;//协议族 - seraddr.sin_port = ntohs(port);//端口,大端转成小端 - inet_pton(AF_INET, ip, &seraddr.sin_addr); //ip地址 - - - /*------------------创建套接字-------------*/ - listenfd = socket(AF_INET, SOCK_STREAM, 0);//创建套接字 - assert(listenfd != -1); - - /*-------------绑定ip和端口------------------*/ - int optval = 1; - if(setsockopt(listenfd, SOCK_STREAM, SO_REUSEADDR, &optval, sizeof(optval)) < 0) - {//网络地址复用 - perror("setsockopt"); - } - int m = bind(listenfd, (struct sockaddr*)&seraddr, sizeof(seraddr));//绑定ip port; - assert(m != -1); - - char buffer[IOMAXSIZE]; - // char sendbuff[IOMAXSIZE]; - - /*------------进程数组-------------------*/ - //int sockcount[FD_SETSIZE]; - for(int i = 0; i < FD_SETSIZE; ++i) - { - SERVER[i].OP = -1;//等于-1是因为进程描述符是从0开始的,负数表示没有该进程 - } - - - /*------------------监听套接字-----------------*/ - int lis = listen(listenfd, 5); - printf("listenfd = %d\n", listenfd); - assert(lis != -1); - socklen_t cliaddr_len; - - fd_set lisset, recset;//相当于声明了两个文件描述符集合 - FD_ZERO(&lisset);//清空lisset描述符集合 - FD_ZERO(&recset); - int maxfd;//select参数一,当前的最大描述符,集合中所有描述符的范围 - maxfd = listenfd; - int nready = 0;//select的返回值 - int i = 0; - int temp; - int count = 0; - while(1) - { - lisset = recset; - FD_SET(listenfd, &lisset);//将监听的文件描述符加入到监听集合中 - nready = select(maxfd+1, &lisset, NULL, NULL, NULL); - if(nready < 0) - { - perror("select"); - return 1; - } - if(nready == 0) - { - continue; - } - if(FD_ISSET(listenfd, &lisset)) - { - cliaddr_len = sizeof(cliaddr); - connectfd = accept(listenfd, (struct sockaddr*)&cliaddr, &cliaddr_len); - printf("connectfd = %d\n", connectfd); - if(connectfd == -1) - { - perror("accept!"); - return 1; - } - for(i = 0; i < FD_SETSIZE; ++i) - { - if(SERVER[i].OP < 0) - { - recv(connectfd, SERVER[i].buf, sizeof(SERVER[i].buf), 0);//接收username - strcpy(SERVER[i].username, SERVER[i].buf); - printf("%s LOGIN successful!", SERVER[i].username); - SERVER[i].OP = connectfd; //将刚刚监听进来的客户端进程唤醒 - // memset(SERVER[i].buf, 0, sizeof(SERVER[i].buf)); - //接收当前用户的username - //memset(SERVER[i].buf, 0, sizeof(SERVER[i].buf));//清空当前用户的缓冲区 - //SERVER[i].cliport = ntohs(cliaddr.sin_port); - break; - } - } - temp = i; - if(i == FD_SETSIZE) - { - fprintf(stderr ,"达到连接上限,与服务器连接失败!"); - exit(EXIT_FAILURE); - } - // printf("IP = %s PORT = %d connect successful!\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); - count ++; - printf("当前连接人数: %d\n", count); - FD_SET(connectfd, &recset); - if(connectfd > maxfd) - { - maxfd = connectfd;//因为connectfd为新生成的文件描述符,maxfd要始终保持最大范围 - } - } - for( i = 0; i < FD_SETSIZE; ++i) - { - //printf("dhskas"); - //connectfd = SERVER[i].OP;//将新进来的连接唤醒 - if(SERVER[i].OP== -1) - { - continue; - } - if(FD_ISSET(connectfd, &lisset)) - { - int recvbytes = recv(SERVER[i].OP, buffer, sizeof(buffer), 0); - if(recvbytes == -1) - { - perror("recv"); - } - if(strcmp("bye", buffer) == 0) - { - printf("client [%s] close!\n",SERVER[i].username); - FD_CLR(SERVER[i].OP, &recset); - SERVER[i].OP = -1; - count --; - printf("当前连接人数:%d\n", count); - //这里需要告知用户(); - } - - for( i = 0; i < FD_SETSIZE; ++i) - { - if(SERVER[i].OP > 0 && SERVER[i].OP != connectfd) - - { - printf("1"); - send(SERVER[i].OP, buffer, recvbytes-1, 0); - } - } - printf("[%s]@: %s", SERVER[temp].buf, buffer); - if(-nready <= 0)//剔除标准输入输出 - { - break; - } - } - } - } - return 0; -} - - diff --git "a/io\345\244\215\347\224\250/pthread_lock.c" "b/io\345\244\215\347\224\250/pthread_lock.c" deleted file mode 100644 index 4086d6c..0000000 --- "a/io\345\244\215\347\224\250/pthread_lock.c" +++ /dev/null @@ -1,187 +0,0 @@ -/************************************************************************* - > File Name: pthread_lock.c - > Author: - > Mail: - > Created Time: 2017年08月27日 21时16分57秒 PDT - ************************************************************************/ - -#include - -#include -#include -#include - -#define BUFFER_SIZE 8 - -#if 0 - - 1.利用多线程进行程序设计,就是将一个程序(进程)的任务划分为执行的多个部分(线程) ,每一个线程为一个顺序的单控制流,而所有线程都是并发执行的,这样,多线程程序就可以实现并行计算,高效利用多处理器。 - - 2.linuxthread主要库函数说明 - 2.1线程的创建和终止 - int pthread_create(pthread_t * pthread,const pthread_attr_t *attr, - void *(*start_routine(void*)),void *arg); - 调用此函数可以创建一个新的线程,新线程创建后执行start_routine指定的程序。 - 其中参数attr是用户希望创建线程的属性, - 当为NULL时表示以默认的属性创建线程。arg是向start_routine传递的参数。当成功 - 创建一个新的线程时,系统会自动为新线程分配一个线程ID号,并通过pthread - 返回给调用者。 - - void pthread_exit(void *value_ptr); - 调用该函数可以退出线程,参数value_ptr是一个指向返回状态值的指针。 - - 2.2线程控制函数 - pthread_self(void);为了区分线程,在线程创建时系统为其分配一个唯一的ID号,由 - pthread_create()返回给调用者,也可以通过pthread_self()获取自己的线程ID。 - - Int pthread_join (pthread- t thread , void **status);这个函数的作用是等待 - 一个线程的结束。调用pthread_join()的线程将被挂起直到线程ID为参数thread指定的线程终止。 - - int pthread_detach(pthread_t pthread);参数pthread代表的线程一旦终止,立即释放调该线程占有的所有资源。 - - 2.3线程间的互斥 - 互斥量和临界区类似,只有拥有互斥量的线程才具有访问资源的权限,由于互斥对象只有一个,这就决定了任何情况下共享资源(代码或变量)都不会被多个线程同时访问。使用互斥不仅能够在同一应用程序的不同线程中实现资源的安全共享,而且可以在不同应用程序的线程之间实现对资源的安全共享。Linux中通过pthread_mutex_t来定义互斥体机制完成互斥操作。具体的操作函数如下 - - pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *attr);初使化一个互斥体变量mutex,参数attr表示按照attr属性创建互斥体变量mutex,如果参数attr为NULL,则以默认的方式创建。 - - pthread_mutex_lock(pthread_mutex_t *mutex);给一个互斥体变量上锁,如果mutex指定的互斥体已经被锁住,则调用线程将被阻塞直到拥有mutex的线程对mutex解锁为止。 - - pthread_mutex_unlock(pthread_mutex_t *mutex);对参数mutex指定的互斥体变量解锁。 - - 2.4线程间的同步 - 同步就是线程等待某一个事件的发生,当等待的事件发生时,被等待的线程和事件一起继续执行。如果等待的事件未到达则挂起。在linux操作系统中是通过条件变量来实现同步的。 - - pthread_cond_init(pthread_cond_t *cond,const pthread_cond_t *attr);这个函数按参数attr指定的属性初使化一个条件变量cond。 - - pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);等待一个事件(条件变量)的发生,发出调用的线程自动阻塞,直到相应的条件变量被置1。等待状态的线程不占用CPU时间。 - - pthread_cond_signal(pthread_cond_t *cond);解除一个等待参数cond指定的条件变量的线程的阻塞状态。 - - //////////////////////////// - 在这里利用多线程技术实现生产者和消费者问题,生产者线程向一缓冲区中写数据,消费 - 者线程从缓冲区中读取数据,由于生产者线程和消费者线程共享同一缓冲区,为了正确读 - 写数据,在使用缓冲队列时必须保持互斥。生产者线程和消费者线程必须满足:生产者写 - 入缓冲区的数目不能超过缓冲区容量,消费者读取的数目不能超过生产者写入的数目。 - 在程序中使用了一个小技巧来判断缓冲区是空还是满。在初始化时读指针和写指针为0; - 如果读指针等于写指针,则缓冲区是空的;如果(写指针+ 1) % N 等于读指针,则缓冲区是满的, - %表示取余数,这时实际上有一个单元空出未用。下面是完整的程序段和注释。 - -#endif - - -struct prodcons { - int buffer[BUFFER_SIZE]; - pthread_mutex_t lock; //互斥LOCK - int readpos , writepos; - pthread_cond_t notempty; //缓冲区非空条件判断 - pthread_cond_t notfull; //缓冲区未满条件判断 -}; - -char * get_time(void){ - time_t rawtime; - struct tm * timeinfo; - time(&rawtime); - return asctime(localtime(&rawtime)); -} - -void init(struct prodcons * b){ - pthread_mutex_init(&b->lock,NULL); - pthread_cond_init(&b->notempty,NULL); - pthread_cond_init(&b->notfull,NULL); - - b->readpos=0; - b->writepos=0; -} - -void put(struct prodcons* b,int data){ - pthread_mutex_lock(&b->lock); - if((b->writepos + 1) % BUFFER_SIZE == b->readpos) - { - pthread_cond_wait(&b->notfull, &b->lock) ; - } -// pthread_mutex_lock(&b->lock); -// 大家仔细理解互斥锁的位置,以及pthread_cond_wait函数的内核实现(解锁,休眠让出cpu,条件满足后被内核唤醒,上锁,形成临界区,保护资源) - b->buffer[b->writepos]=data; - b->writepos++; - if(b->writepos >= BUFFER_SIZE) - b->writepos=0; - printf("put %d at %s \n", data, get_time()); - pthread_mutex_unlock(&b->lock); - pthread_cond_signal(&b->notempty); -} - -int get(struct prodcons *b){ - int data; - - pthread_mutex_lock(&b->lock); - if(b->writepos == b->readpos) - { - printf("0 == 0\n"); - pthread_cond_wait(&b->notempty, &b->lock); - printf("start read \n"); - } - - data = b->buffer[b->readpos]; - b->readpos++; - - if(b->readpos >= BUFFER_SIZE) - b->readpos=0; - -// sleep(4); - printf("get the data is : %d\n", data); - pthread_cond_signal(&b->notfull); - pthread_mutex_unlock(&b->lock); - - return data; -} - -#define OVER (-1) -struct prodcons buffer; - -void *producer(void *data) -{ - int n; - - for(n = 0; n < 10; n++) - { - printf("before produce %d \n", n) ; - put(&buffer, n); - } - - put(&buffer, OVER); - - return NULL; -} -void *consumer(void * data) -{ - int d; - while(1) - { - d = get(&buffer); -// sleep(100); - if(d == OVER) - break; - printf("consumerd the num is %d\n", d); - } - return NULL; -} - -int main(void) -{ - pthread_t th_a, th_b; - pthread_attr_t attr; - void *retval; - pthread_attr_init(&attr); - init(&buffer); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); -// pthread_create(&th_b, &attr, consumer, 0); - pthread_create(&th_b, NULL, consumer, 0); - // sleep(3); - pthread_create(&th_a, NULL, producer, 0); - pthread_join(th_a, &retval); - pthread_join(th_b, &retval); - //sleep(100); - - return 0; -} - diff --git "a/io\345\244\215\347\224\250/server" "b/io\345\244\215\347\224\250/server" deleted file mode 100755 index 088576f..0000000 Binary files "a/io\345\244\215\347\224\250/server" and /dev/null differ diff --git a/peixun/.f1 b/peixun/.f1 deleted file mode 100644 index e69de29..0000000 diff --git a/peixun/8-29.c b/peixun/8-29.c deleted file mode 100644 index 54f7eec..0000000 --- a/peixun/8-29.c +++ /dev/null @@ -1,25 +0,0 @@ -/************************************************************************* - > File Name: 8-29.c - > Author: - > Mail: - > Created Time: 2017年08月29日 00时30分46秒 PDT - ************************************************************************/ - -#include -void fun() -{ - int a = 0; - static int b; - int c; - printf("b = %d c = %d ", b,c); - a++; - b++; -} -int main() -{ - int a = 1; - int *p; - p = 0; - p = &a; - printf("%d", *p); -} diff --git a/peixun/8-29_1.c b/peixun/8-29_1.c deleted file mode 100644 index 6d12ba4..0000000 --- a/peixun/8-29_1.c +++ /dev/null @@ -1,20 +0,0 @@ -/************************************************************************* - > File Name: 8-29_1.c - > Author: - > Mail: - > Created Time: 2017年08月29日 01时00分47秒 PDT - ************************************************************************/ - -#include -int a =6; -static int b = 10; -int c = 5; -void print() -{ - printf("a = %d\n b = %d\n c = %d\n", a, b , c); -} -static void s_print() -{ - printf("a = %d\n b = %d\n c = %d\n", a, b , c); - -} diff --git a/peixun/8-29_2.c b/peixun/8-29_2.c deleted file mode 100644 index 1be4d97..0000000 --- a/peixun/8-29_2.c +++ /dev/null @@ -1,25 +0,0 @@ -/************************************************************************* - > File Name: 8-29_2.c - > Author: - > Mail: - > Created Time: 2017年08月29日 01时02分30秒 PDT - ************************************************************************/ - -#include -int a; -static int b = 100;//只限定在本文件有用。 -extern int c; -//extern int d; -extern void print(); -extern void s_print(); - -int main() -{ - print(); - - printf("a = %d\n b = %d\n c = %d\n", a, b , c); - // printf("%d\n", d); - - // s_print(); - return 0; -} diff --git a/peixun/8-29_3.c b/peixun/8-29_3.c deleted file mode 100644 index b03ec34..0000000 --- a/peixun/8-29_3.c +++ /dev/null @@ -1,22 +0,0 @@ -/************************************************************************* - > File Name: 8-29_3.c - > Author: - > Mail: - > Created Time: 2017年08月29日 02时13分55秒 PDT - ************************************************************************/ - -#include -#include -int main() -{ - int a = 4; - int *p = &a; - printf("%p\n", p); - int *p1 = realloc(p, 10); - printf("%p\n", p); - - - printf("%p\n", p1); - free(p); - -} diff --git a/peixun/8.15_1.c b/peixun/8.15_1.c deleted file mode 100644 index 44e9c81..0000000 --- a/peixun/8.15_1.c +++ /dev/null @@ -1,17 +0,0 @@ -/************************************************************************* - > File Name: 8.15_1.c - > Author: - > Mail: - > Created Time: 2017年08月14日 18时36分58秒 PDT - ************************************************************************/ - -#include -int main() -{ - int a[5] = {1,2,[3] = 4, 5, [1] = 100}; - for(int i= 0; i < 5; ++i) - { - printf("%d ", a[i]); - } - return 0; -} diff --git a/peixun/8.15_10.c b/peixun/8.15_10.c deleted file mode 100644 index d1c2d39..0000000 --- a/peixun/8.15_10.c +++ /dev/null @@ -1,37 +0,0 @@ -/************************************************************************* - > File Name: 8.15_10.c - > Author: - > Mail: - > Created Time: 2017年08月15日 01时35分37秒 PDT - ************************************************************************/ - -#include -//常量传非常量有问题。 -//非常量传给常量没问题 -void print_array(const int a[], int n) -{ - for(int i = 0; i < n; ++i) - { - printf("%d\n", a[i]); - } -} - -void print_array2(int a[], int n) -{ - for(int i= 0; i < n; ++i) - { - printf("%d\n", a[i] ++); - } -} -int main() -{ - const int a[3] = {1,2,3}; - int b[3] = {4, 5, 6}; - - print_array(a,3); - print_array(b,3); - // print_array2(a,3); - print_array2(b, 3); - - return 0; -} diff --git a/peixun/8.15_2.c b/peixun/8.15_2.c deleted file mode 100644 index 840011b..0000000 --- a/peixun/8.15_2.c +++ /dev/null @@ -1,18 +0,0 @@ -/************************************************************************* - > File Name: 8.15_2.c - > Author: - > Mail: - > Created Time: 2017年08月14日 18时54分02秒 PDT - ************************************************************************/ - -#include -int main() -{ - int a[3][2] = {(1, 2), (3, 4), (5, 6)}; - - int *p = a[0]; - - printf("%d\n", p[0]); - - return 0; -} diff --git a/peixun/8.15_3.c b/peixun/8.15_3.c deleted file mode 100644 index af81b97..0000000 --- a/peixun/8.15_3.c +++ /dev/null @@ -1,31 +0,0 @@ -/************************************************************************* - > File Name: 8.15_3.c - > Author: - > Mail: - > Created Time: 2017年08月14日 19时05分40秒 PDT - ************************************************************************/ - -#include -int Sum(int m, int n, int a[*][*]); -int main() -{ - int m = 3,n = 2; - int a[3][2] = {{1,2}, {3,4}, {5,6}}; - - int sum = Sum(m,n,a); - printf("%d\n", sum); - return 0; -} - -int Sum( int m, int n, int a[][n]) -{ - int sum = 0; - for(int i = 0; i < m; ++i) - { - for(int j = 0; j < n; ++j) - { - sum += a[i][j]; - } - } - return sum; -} diff --git a/peixun/8.15_4.c b/peixun/8.15_4.c deleted file mode 100644 index f81a458..0000000 --- a/peixun/8.15_4.c +++ /dev/null @@ -1,39 +0,0 @@ -/************************************************************************* - > File Name: 8.15_4.c - > Author: - > Mail: - > Created Time: 2017年08月14日 20时19分49秒 PDT - ************************************************************************/ - -#include -//指针数组 -/*int main() -{ - int a = 1, b = 2, c = 3; - int *p[3] = {&a, &b, &c}; - - for(int i= 0; i < 3; ++i) - { - printf("%d %p\n", *p[i], p[i]); - } - putchar(10); - return 0; -}*/ - -//数组指针 -int main() -{ - int a[2][3]= {1, 2, 3, 4, 5, 6}; - int (*p)[3]; - p = a; - - for(int i = 0; i < 2; ++i) - { - for(int j = 0; j < 3; ++j) - { - printf("%d ", p[i][j]); - printf("%d\t", *(*(p + i)+j)); - } - } - return 0; -} diff --git a/peixun/8.15_5.c b/peixun/8.15_5.c deleted file mode 100644 index 772433a..0000000 --- a/peixun/8.15_5.c +++ /dev/null @@ -1,23 +0,0 @@ -/************************************************************************* - > File Name: 8.15_5.c - > Author: - > Mail: - > Created Time: 2017年08月14日 20时43分17秒 PDT - ************************************************************************/ - -#include -int sum (int a, int b); -int main() -{ - int (*p)(int a, int b); - p = sum; - - printf("%d %d", p(1,2), sum(1,2)); - - return 0; -} - -int sum (int a, int b) -{ - return a+b; -} diff --git a/peixun/8.15_6.c b/peixun/8.15_6.c deleted file mode 100644 index 3d9823e..0000000 --- a/peixun/8.15_6.c +++ /dev/null @@ -1,42 +0,0 @@ -/************************************************************************* - > File Name: 8.15_6.c - > Author: - > Mail: - > Created Time: 2017年08月14日 20时53分46秒 PDT - ************************************************************************/ - -#include -int function1(int a); -int function2(int a); -int function3(int a); - - -int main() -{ - int (*p[3])(int a); - p[0] = function1; - p[1] = function2; - p[2] = function3; - for(int i = 0; i < 3; ++i) - { - p[i](1); - } - -} - -int function1(int a) -{ - printf("int function1\n"); - return 0; -} -int function2(int a) -{ - printf("int function2\n"); - return 0; -} - -int function3(int a) -{ - printf("int function3\n"); - return 0; -} diff --git a/peixun/8.15_7.c b/peixun/8.15_7.c deleted file mode 100644 index 570cca8..0000000 --- a/peixun/8.15_7.c +++ /dev/null @@ -1,22 +0,0 @@ -/************************************************************************* - > File Name: 8.15_7.c - > Author: - > Mail: - > Created Time: 2017年08月14日 23时24分20秒 PDT - ************************************************************************/ - -#include -int main() -{ - int a[5] = {1, 2, 3, 4, 5}; - - printf("sizeof(unsigned long): %lu\n", sizeof(unsigned long)); - printf("sizeof(int): %lu\nsizeof(a): %lu\nszieof(&a): %lu\nsizeof(&a[0]): %lu\n",sizeof(int), sizeof(a), sizeof(&a), sizeof(&a[0])); - - printf("sizeof(&a[4]): %lu\n", sizeof(&a[4])); - - printf("a: %p\n&a : %p\n&a[0]: %p\na+1 : %p\n&a+1 : %p\n&a[0]+1: %p\n",a, &a, &a[0], a+1, &a+1, &a[0] + 1); - - printf("a[-1] : %d\n",a[-1]); - return 0; -} diff --git a/peixun/8.15_8.c b/peixun/8.15_8.c deleted file mode 100644 index 07898dd..0000000 --- a/peixun/8.15_8.c +++ /dev/null @@ -1,16 +0,0 @@ -/************************************************************************* - > File Name: 8.15_8.c - > Author: - > Mail: - > Created Time: 2017年08月15日 00时11分54秒 PDT - ************************************************************************/ - -#include -int main() -{ - int a[5]= {1, 2, 3, 4, 5}; - int *p = (int *)(&a + 1); - - printf("%d\n", *(p -1)); - return 0; -} diff --git a/peixun/8.15_9.c b/peixun/8.15_9.c deleted file mode 100644 index 40120f5..0000000 --- a/peixun/8.15_9.c +++ /dev/null @@ -1,30 +0,0 @@ -/************************************************************************* - > File Name: 8.15_9.c - > Author: - > Mail: - > Created Time: 2017年08月15日 00时54分40秒 PDT - ************************************************************************/ - -#include -int main() -{ - int a[2][3] = {{1,2, 4}, {3,4,5}}; - printf("a:%p\n&a[0]:%p\n", a, &a[0]); - - printf("&a + 1: %p\na+1: %p\n", &a + 1, a+1); - - int *p = a[0]; - //int (*p)[3]; - //p = a; - - for(int i= 0; i < 2; ++i) - { - for(int j = 0; j < 3; ++j) - { - printf("%d", *(p + i*3 + j)); - printf("%d ", *(p + 3*i +j)); - //printf("%d ", *(*(p + i)+ j)); - } - } - return 0; -} diff --git a/peixun/8.19_1.c b/peixun/8.19_1.c deleted file mode 100644 index bb1ef85..0000000 --- a/peixun/8.19_1.c +++ /dev/null @@ -1,25 +0,0 @@ -/************************************************************************* - > File Name: 8.19_1.c - > Author: - > Mail: - > Created Time: 2017年08月19日 09时48分02秒 PDT - ************************************************************************/ - -#include -#include -int main() -{ - char *str = "abc"; - int arr[strlen(str)]; - - for(int i= 0; i < strlen(str); ++i) - { - arr[i] = i; - } - for(int i= 0; i < strlen(str); ++i) - { - printf("%d ", arr[i]); - } - - return 0; -} diff --git a/peixun/8.19_2.c b/peixun/8.19_2.c deleted file mode 100644 index e081719..0000000 --- a/peixun/8.19_2.c +++ /dev/null @@ -1,12 +0,0 @@ -/************************************************************************* - > File Name: 8.19_2.c - > Author: - > Mail: - > Created Time: 2017年08月19日 09时58分45秒 PDT - ************************************************************************/ - -#include -int main() -{ - -} diff --git a/peixun/8.19_3.c b/peixun/8.19_3.c deleted file mode 100644 index 2b5876f..0000000 --- a/peixun/8.19_3.c +++ /dev/null @@ -1,17 +0,0 @@ -/************************************************************************* - > File Name: 8.19_3.c - > Author: - > Mail: - > Created Time: 2017年08月19日 11时25分44秒 PDT - ************************************************************************/ - -#include -int main() -{ - printf("%d ", fun(1.1, 0.2)); - return 0 ; -} -int fun(a, b) -{ - return a; -} diff --git a/peixun/8.19_4.c b/peixun/8.19_4.c deleted file mode 100644 index e817238..0000000 --- a/peixun/8.19_4.c +++ /dev/null @@ -1,22 +0,0 @@ -/************************************************************************* - > File Name: 8.19_4.c - > Author: - > Mail: - > Created Time: 2017年08月19日 14时20分17秒 PDT - ************************************************************************/ - -#include -void swap(int *a, int *b) -{ - int temp = *a; - *a = *b; - *b = temp; -} -int main() -{ - int a = 12, b = 13; - swap(&a, &b); - - printf("a = %d b = %d\n", a, b); - - return 0; diff --git a/peixun/8.19_5.c b/peixun/8.19_5.c deleted file mode 100644 index bf9e046..0000000 --- a/peixun/8.19_5.c +++ /dev/null @@ -1,20 +0,0 @@ -/************************************************************************* - > File Name: 8.19_5.c - > Author: - > Mail: - > Created Time: 2017年08月19日 14时48分27秒 PDT - ************************************************************************/ - -#include -void fun(int a, int b) -{ - printf("%d %d\n", a, b); -} -int main() -{ - int a = 1; - int b = 2; - fun(a, b); - return 0; -} - diff --git a/peixun/8.19_6.c b/peixun/8.19_6.c deleted file mode 100644 index b0b6365..0000000 --- a/peixun/8.19_6.c +++ /dev/null @@ -1,31 +0,0 @@ -/************************************************************************* - > File Name: 8.19_6.c - > Author: - > Mail: - > Created Time: 2017年08月19日 15时17分30秒 PDT - ************************************************************************/ - -#include -#include - -int sum(int num , ...); -int main() -{ - printf("%d \n", sum(6, 2.0, 3.1, 4, 5.9, 0.9, 1)); -} - -int sum(int num, ...) -{ - va_list ap; - va_start(ap , num); - double total = 0; - for(int i= 0; i < num; ++i) - { - int temp = va_arg(ap, int); - printf("%d \n", temp); - total += temp; - } - - va_end(ap); - return total; -} diff --git a/peixun/a.out b/peixun/a.out deleted file mode 100755 index 9c694f9..0000000 Binary files a/peixun/a.out and /dev/null differ diff --git a/peixun/main b/peixun/main deleted file mode 100755 index 494039c..0000000 Binary files a/peixun/main and /dev/null differ diff --git a/peixun/makefile b/peixun/makefile deleted file mode 100644 index 8280d88..0000000 --- a/peixun/makefile +++ /dev/null @@ -1,3 +0,0 @@ -main:8-29_1.c 8-29_2.c - gcc 8-29_1.c 8-29_2.c -o main - diff --git a/Socket/server.c b/ser_fork.c similarity index 50% rename from Socket/server.c rename to ser_fork.c index 80e555a..39d0ed3 100644 --- a/Socket/server.c +++ b/ser_fork.c @@ -1,10 +1,9 @@ /************************************************************************* - > File Name: server.c + > File Name: ser_fork.c > Author: > Mail: - > Created Time: 2017年08月15日 22时03分59秒 PDT + > Created Time: 2017年08月16日 18时58分09秒 PDT ************************************************************************/ - #include #include #include @@ -26,7 +25,7 @@ int main(int argc, char *argv[]) memset(&my_addr,0,sizeof(my_addr)); //数据初始化--清零 my_addr.sin_family=AF_INET; //设置为IP通信 my_addr.sin_addr.s_addr=INADDR_ANY;//服务器IP地址--允许连接到所有本地地址上 - my_addr.sin_port=htons(8000); //服务器端口号 + my_addr.sin_port=htons(8001); //服务器端口号 /*创建服务器端套接字--IPv4协议,面向连接通信,TCP协议*/ if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0) @@ -46,53 +45,60 @@ int main(int argc, char *argv[]) listen(server_sockfd,5); sin_size=sizeof(struct sockaddr_in); - - /*等待客户端连接请求到达*/ - if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0) - { - perror("accept"); - return 1; - } - printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr)); - len=send(client_sockfd,"Welcome to my server\n",21,0);//发送欢迎信息 - - /*接收客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数*/ + + pid_t pid, ppid; while(1) { - while(1) + if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0) { - len = recv(client_sockfd,buf,BUFSIZ,0); - buf[len]='\0'; - if(len > 0) - { - printf("received@: %s\n",buf); - } - if(!strcmp(buf,"quit")) - { - break; - } - if(len = 0) - { - exit(0); - } - /*if(send(client_sockfd,buf,len,0)<0) - { - perror("write"); - return 1; - }*/ + perror("accept"); + return 1; + } + else + { + send(client_sockfd,"You have connect server!", strlen("You have connect server!"), 0); } - while(1) + + //输出连接的客户端信息 + printf("\n%s: %d Login server!\n\n",inet_ntoa(remote_addr.sin_addr),ntohs(remote_addr.sin_port)); + pid = fork(); + int recvbytes; + if(pid == -1) { - printf("send@:"); - gets(buf); - len = send(client_sockfd,buf,sizeof(buf), 0); - if(!strcmp(buf, "quit")) + printf("fork failed!"); + } + else if(pid == 0) + { + while(1) { - break; + bzero(buf, 100); + if((recvbytes = recv(client_sockfd, buf, 100,0)) == -1) + { + perror("read client_sockfd failed!"); + } + else if(recvbytes != 0) + { + buf[recvbytes] = '\0'; + usleep(1000); + printf("%s: %d said: %s\n", inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port), buf); + //将客户端发送过来的消息发送回去。 + if(send(client_sockfd, buf, sizeof(buf), 0) == -1) + { + perror("send error!"); + break; + } + } + } } + else if(pid > 0){} } - + + /*等待客户端连接请求到达*/ + /* printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr)); + len=send(client_sockfd,"Welcome to my server\n",21,0);*/ + //发送欢迎信息 /*接收客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数*/ + close(client_sockfd); close(server_sockfd); return 0; diff --git a/servertest1.c b/servertest1.c new file mode 100644 index 0000000..5bbdb39 --- /dev/null +++ b/servertest1.c @@ -0,0 +1,55 @@ +/************************************************************************* + > File Name: clienttest1.c + > Author: + > Mail: + > Created Time: 2017年08月18日 04时04分30秒 PDT + ************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + if(argc <= 2) + { + printf("usage: %s ip_address port_number",argv[0]); + return 1; + } + const char *ip = argv[1]; + int port = atoi(argv[2]); + + struct sockaddr_in ser_address; + struct sockaddr_in cli_address; + + bzero(&ser_address, sizeof(ser_address)); + ser_address.sin_family = AF_INET; + inet_pton(AF_INET, ip, &ser_address.sin_addr); + ser_address.sin_port = htons(port); + + int sock = socket(AF_INET, SOCK_STREAM,0); + assert(sock != -1); + int ret = bind(sock,(struct sockaddr*)&ser_address,sizeof(ser_address)); + assert(ret != -1); + + ret = listen(sock, 5); + assert(ret != -1); + int sin_size = sizeof(cli_address); + int con = accept(sock, (struct sockaddr*)&cli_address,&sin_size); + if(con < 0) + { + printf("error!"); + return 1; + } + else + { + + } + + + diff --git "a/\347\272\242\345\214\205/pthreadpack.c" "b/\347\272\242\345\214\205/pthreadpack.c" deleted file mode 100644 index e5bf369..0000000 --- "a/\347\272\242\345\214\205/pthreadpack.c" +++ /dev/null @@ -1,128 +0,0 @@ -/************************************************************************* - > File Name: pthreadpack.c - > Author: - > Mail: - > Created Time: 2017年09月20日 04时23分59秒 PDT - ************************************************************************/ - -#include -#include -#include -#include -#include - -#define MAXNUM 10 -#define _RAND(max, min) (rand()%((max)- (min)) + min) -static void* _grab(void *args); -static int count = 1; -typedef struct event//线程实例 -{ - pthread_attr_t attr;//线程属性 - pthread_mutex_t mutex;//线程锁 - pthread_cond_t cond;//条件变量 -}event_t; - -typedef struct item -{ - int number; //红包数目 - int total; //红包总额 -}item_t; -static item_t item = {0}; - -int main() -{ - int n = 0; //用来代表线程数 - int number;//红包数目 - int total;//红包总额 - - event_t temp = {0}; - pthread_t tid;//线程变量 - - /****实例初始化****/ - //assert(ret = pthread_mutex_init(&temp.mutex, NULL));//初始化锁 - //assert(ret = pthread_cond_init(&temp.cond, NULL));//初始化条件变量 - //assert(ret = pthread_attr_init(&temp.attr));//初始化线程属性 - pthread_mutex_init(&temp.mutex, NULL);//初始化锁 - pthread_cond_init(&temp.cond, NULL);//初始化条件变量 - pthread_attr_init(&temp.attr);//初始化线程属性 - - /****创建线程*******/ - for(int i = 0; i < MAXNUM; ++i) - { - pthread_create(&tid, &temp.attr,_grab, &temp); - count ++; - } - - while(1) - { - sleep(0.01); - printf("请输入红包个数:\n"); - if(fscanf(stdin,"%d", &number) == EOF)break; - printf("请输入红包总金额:\n"); - if(fscanf(stdin,"%d", &total) == EOF)break; - - if(number > 10||number < 0) - { - fprintf(stderr,"error: Package out of rang: only allowed [0, 10]\n"); - exit(1); - } - - pthread_mutex_lock(&temp.mutex);//上锁 - item.number = number; - item.total = total*100; - - printf("初始的红包情况:<个数:%d 金额:%d.%02d>\n",item.number, item.total/100, item.total%100); - pthread_cond_broadcast(&temp.cond);//红包包好后唤醒所有线程抢红包 - pthread_mutex_unlock(&temp.mutex);//解锁 - - sleep(1); - } - - //程序结束时销毁实例成员 - //assert(ret = pthread_attr_destroy(&temp.attr)); - //assert(ret = pthread_cond_destroy(&temp.cond)); - //assert(ret = pthread_mutex_destroy(&temp.mutex)); - pthread_attr_destroy(&temp.attr); - pthread_cond_destroy(&temp.cond); - pthread_mutex_destroy(&temp.mutex); - - return 1; -} -static void* _grab(void *args) -{ - int money = 0; - event_t *ptid = (event_t *)args; - if(!args) - { - return NULL; - } - printf("线程#%d : %lu\n", count, (unsigned long)pthread_self());//使用lu防止线程tid超过int范围 - while(1) - { - pthread_mutex_lock(&ptid->mutex);//子线程上锁 - pthread_cond_wait(&ptid->cond, &ptid->mutex);//线程挂起等待 - - if(item.number <= 0) - { - printf("Thread #%lu,the package is empty!\n", (unsigned long)pthread_self()); - pthread_mutex_unlock(&ptid->mutex); - printf("********************Thread #%lu Stop!\n", (unsigned long)pthread_self()); - continue; - } - else if(item.number == 1)//对于最后一个红包,把所有金额都算上 - { - money = item.total; - } - else - { - money = item.total * _RAND(199, 1)/100/item.number; - } - item.total -= money; - item.number--; - - printf("Thread %lu get %d.%02d, left[%d, %d.%02d]\n", (unsigned long)pthread_self(), money/100, money%100, item.number,item.total/100, item.total%100); - pthread_mutex_unlock(&ptid->mutex); - } - //printf("Thread #%lu Stop!\n", (unsigned long)pthread_self()); - return NULL; -}