static int __init my_init(void) { int count = 32; char buf1[32], buf2[32]; my_fun(buf1, buf2, count, NULL); return 0; }
int main(int argc, char* argv[]) { my_fun(10); // 这是直接调用my_fun函数 my_fun_pointer = my_fun; // 将my_fun函数的地址赋给my_fun_pointer变量 my_fun_pointer(20); // 这是通过函数指针变量my_fun_pointer来调用my_fun函数的。 return 0; }
int main() { struct sockaddr_in sin; struct sockaddr_in cin; int l_fd; int c_fd; socklen_t len; char buf[MAX_LINE]; char addr_p[INET_ADDRSTRLEN]; int port = 8000; int n; bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); if ( (l_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("fail to create socket"); exit(1); } if (bind(l_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { perror("fail to bind"); exit(1); } if (listen(l_fd, 10) == -1) { perror("fail to listen"); exit(1); } printf("waiting ...\n"); while (1) { if ((c_fd = accept(l_fd, (struct sockaddr *)&cin, &len)) == -1) { perror("fail to accept"); exit(1); } n = my_read(c_fd, buf, MAX_LINE); if (n == -1) { exit(1); } else if (n == 0) { printf("the connect has been closed\n"); close(c_fd); continue; } inet_ntop(AF_INET, &cin.sin_addr, addr_p, sizeof(addr_p)); printf("client IP is %s, port is %d\n", addr_p, ntohs(cin.sin_port)); printf("contens is : %s\n", buf); my_fun(buf); n = write(c_fd, buf, n); if (n == -1) { exit(1); } if (close(c_fd) == -1) { perror("fail to close"); exit(1); } } return 0; }
int main() { struct sockaddr_in sin; struct sockaddr_in cin; int s_fd; int port = 8000; socklen_t addr_len; char buf[MAX_LINE]; char addr_p[INET_ADDRSTRLEN]; int n; bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); s_fd = socket(AF_INET, SOCK_DGRAM, 0); if (s_fd == -1) { perror("fail to create socket"); exit(1); } if (bind(s_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { perror("call to bind"); exit(1); } while (1) { addr_len = sizeof(sin); n = recvfrom(s_fd, buf, MAX_LINE, 0, (struct sockaddr *)&cin, &addr_len); if (n == -1) { perror("fail to receive\n"); exit(1); } inet_ntop(AF_INET, &cin.sin_addr, addr_p, sizeof(addr_p)); printf("client IP is %s, port, is %d\n", addr_p, ntohs(cin.sin_port)); printf("content is : %s\n", buf); my_fun(buf); n = sendto(s_fd, buf, n, 0, (struct sockaddr *)&cin, addr_len); if (n == -1) { perror("fail to send"); exit(1); } } if (close(s_fd) == -1) { perror("fail to close"); exit(1); } return 0; }
int main(void) { struct sockaddr_in sin; struct sockaddr_in cin; int l_fd; int c_fd; socklen_t len; char buf[MAX_LINE]; char addr_p[INET_ADDRSTRLEN]; int port = 8000; int n; bzero(&sin,sizeof(sin)); // clear address struct sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(port); l_fd = socket(AF_INET,SOCK_STREAM,0); // tcp bind(l_fd, (struct sockaddr *) &sin, sizeof(sin)); listen(l_fd,10); printf("waiting ...\n"); while(1) { c_fd = accept(l_fd, (struct sockaddr *) &cin, &len); n = read(c_fd,buf,MAX_LINE); inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p)); printf("client IP is %s port is %d\n",addr_p,1);//ntohs(c_fd.sin_port)); printf("content is :%s\n", buf); my_fun(buf); write(c_fd,buf,n); close(c_fd); } if(close(l_fd) == -1) { perror("fail to close"); exit(1); } return 0; }
int main(int argc,char *argv[]) { struct sockaddr_in sin; struct sockaddr_in cin; int l_fd; int c_fd; socklen_t len; char buf[MAX_LINE]; char addr_p[INET_ADDRSTRLEN]; int port = 8000; int n; bzero(&sin,sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); l_fd = socket(AF_INET,SOCK_STREAM,0); bind(l_fd,(struct sockaddr *)&sin,sizeof(sin)); listen(l_fd,10); printf("waiting ...\n"); while (1) { c_fd = accept(l_fd,(struct sockaddr *)&cin,&len); n = read(c_fd,buf,MAX_LINE); inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p)); printf("client IP is %s, port is %d\n",addr_p,ntohs(cin.sin_port)); printf("content is : %s\n",buf); my_fun(buf); write(c_fd,buf,n); close(c_fd); } return 0; }
int main (void) { printf("my_fun(2.2,0)= %f\n",my_fun(2.2,0)); printf("my_fun(2.2,1)= %f\n",my_fun(2.2,1)); printf("my_fun(2.2,2)= %f\n",my_fun(2.2,2)); printf("my_fun(2.2,3)= %f\n",my_fun(2.2,3)); printf("my_fun(2.2,4)= %f\n",my_fun(2.2,4)); printf("my_fun(2.2,5)= %f\n",my_fun(2.2,5)); printf("my_fun(2.2,6)= %f\n",my_fun(2.2,6)); printf("my_fun(2.2,7)= %f\n",my_fun(2.2,7)); printf("my_fun(2.2,8)= %f\n",my_fun(2.2,8)); printf("my_fun(2.2,9)= %f\n",my_fun(2.2,9)); printf("my_fun(2.2,10)= %f\n",my_fun(2.2,10)); printf("\n\n\n\n"); double margin =0.0; //差額 int n = 2; double big,small; do{ printf("----------------------\n"); if(my_fun(2.2,n)>my_fun(2.2,n-1)){ margin = my_fun(2.2,n) - my_fun(2.2,n-1);} else{ margin = my_fun(2.2,n-1) - my_fun(2.2,n) ; } printf("n=%d n-1=%d margin=%f \n",n,n-1,margin); n++; } while (margin >=0.0001) ; printf("\n\n當n等於%d時my_fun(2.2,n-1)與my_fun(2.2,n)的差額小於0.0001\n\n",n-1); system("pause"); return 0; }
int main(void) { int lfd, cfd; time_t staletime; /* 测试客户端进程的时间 */ struct sockaddr_un un_addr; struct stat statbuf; char buf[MAX]; int len, n; if(init(&lfd, PATH) == -1) /* 调用初始化函数,创建监听套接字,并且开始监听 */ exit(1); while(1){/* 服务器程序多半是死循环 */ len = sizeof(struct sockaddr_un); /* 处理一个连接,创建连接套接字,得到客户端进程的套接字文件路径 * 并将其保存在un_addr结构中,注意这里使用sockaddr_un结构的大小 */ if ((cfd = accept(lfd, (struct sockaddr *)&un_addr, &len)) == -1){ perror("fail to accept"); exit(1); } /* 得到客户端文件的路径,并且设置结束符 */ len -= offsetof(struct sockaddr_un, sun_path); un_addr.sun_path[len] = '\0'; /* 得到文件爱你的状态信息,为了验证客户端进程的通信时间 * 客户端进程如果长期没有修改通信用的套接字文件,说明该客户端有可能已经结束通信 * 下面分别验证文件的权限和修改时间,这些操作并不是必须的 * 但是出于程序的完整性考虑,这些操作还是必要的 */ if (stat(un_addr.sun_path, &statbuf) == -1) { perror("fail to get status"); exit(1); } /* 检查文件的权限,通信用的套接字文件的权限必须是"rwx------" * 也就是说之用所有者用户可以有读、写和执行该文件的权限,其他用户没有 * 这说明Unix域套接字只能用于同一用户的进程之间的通信 */ if ((statbuf.st_mode & (S_IRWXG | S_IRWXO)) || (statbuf.st_mode & S_IRWXU) != S_IRWXU) { printf("wrong permissions\n"); exit(1); } /* 检查套接字文件的更新时间,超过三十秒钟未作访问和修改 * 说明客户端进程可能已经断开了连接,关闭连接套接字,结束连接 */ staletime = time(NULL) - STALE; if (statbuf.st_atime < staletime || statbuf.st_ctime < staletime || statbuf.st_mtime < staletime){ printf("client is too old\n"); close(cfd); break; } /* 删除客户端的套接字文件 * 该套接字文件由客户端进程在调用bind函数进行套接字绑定的时候生成 */ if(unlink(un_addr.sun_path) == -1){ perror("fail to unlink"); exit(1); } my_fun(buf); /* 调用大小写转换函数 */ if(write(cfd, buf, n) == -1){ /* 将转换后的字串发给客户端进程 */ perror("fail to write"); exit(1); } close(cfd); /* 通讯结束,关闭套接字,准备下一次通信 */ } /* 删除服务器进程的套接字文件 */ if(unlink(PATH) == -1){ perror("fail to unlink"); exit(1); } close(lfd); return 0; }