//命令lsof -i:5999
//简单服务器
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define MYPORT 5999
#define BACKLOG 10
int main(){
int sock_fd, new_fd;
struct sockaddr_in my_addr, their_addr;
int sin_size;
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if(bind(sock_fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1){
perror("bind");
exit(1);
}
if(listen(sock_fd, BACKLOG) == -1){
perror("listen");
exit(1);
}
while(1){
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd, (struct sockaddr *)&their_addr, &sin_size)) == -1){
perror("accept");
continue;
}
printf("server.got connection from %s\n",inet_ntoa(their_addr.sin_addr));
if(!fork()){
if(send(new_fd, "Hello,world!\n", 14, 0) == -1){
perror("send");
close(new_fd);
exit(0);
}
}
while(waitpid(-1, NULL, WNOHANG) > 0);
}
}
//简单客户端
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 5999
#define MAXDATASIZE 100
int main(int argc, char *argv[]){
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if(argc != 2){
fprintf(stderr, "usage: client hostname\n");
exit(1);
}
if((he = gethostbyname(argv[1])) == NULL){
perror("gethostbyname");
exit(1);
}
// printf();
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
//their_addr.sin_addr = *((struct in_addr*)he->h_addr);
their_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(their_addr.sin_zero), 8);
if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1){
perror("connect");
exit(1);
}
if((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1){
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s", buf);
close(sockfd);
return 0;
}
//epoll 服务器端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/wait.h>
#define SERVPORT 5999
#define BACKLOG 10 //最大同时连接请求数
void setnonblocking(int sock){
int opts;
opts = fcntl(sock, F_GETFL);
if(opts < 0){
perror("fcntl(sock, GETFL)");
exit(1);
}
opts = opts | O_NONBLOCK;
if(fcntl(sock, F_SETFL, opts) < 0){
perror("fcntl(sock, SETFL, opts)");
exit(1);
}
}
void do_use_fd(int client_fd){
char msg[50];
const char str[] = "God bless you!\n";
recv(client_fd, msg, 50, 0);
printf("get from client msg %s\n", msg);
if(send(client_fd, str, sizeof(str), 0) == -1)
perror("send");
close(client_fd);
}
int main(){
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1){
perror("bind");
exit(1);
}
if(listen(sockfd, BACKLOG) == -1){
perror("listen");
exit(1);
}
size_t sin_size = sizeof(struct sockaddr_in);
#define MAX_EVENTS 10
struct epoll_event ev, events[MAX_EVENTS];
int conn_sock, nfds, epollfd;
epollfd = epoll_create(10);
if(epollfd == -1){
perror("epoll_create");
exit(EXIT_FAILURE);
}
ev.events = EPOLLIN;
ev.data.fd = sockfd;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1){
perror("epoll_ctl: sockfd");
exit(EXIT_FAILURE);
}
for(;;){
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if(nfds == -1){
perror("epoll_wait");
exit(EXIT_FAILURE);
}
int i;
for(i = 0; i < nfds; ++i){
if(events[i].data.fd == sockfd){
conn_sock = accept(sockfd,
(struct sockaddr *)&their_addr, &sin_size);
if(conn_sock == -1){
perror("accept");
exit(EXIT_FAILURE);
}
setnonblocking(conn_sock);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = conn_sock;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,
&ev) == -1){
perror("epoll_ctl: conn_sock");
exit(EXIT_FAILURE);
}
}else{
do_use_fd(events[i].data.fd);
}
}
}
}
//epoll 客户端
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#define PORT 5999
#define MSG 50
int main(){
int sockfd;
char msg[MSG];
struct sockaddr_in their_addr;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket() failed");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&their_addr.sin_zero, 8);
if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1){
perror("connect error");
exit(1);
}
send(sockfd, "HELLO", 20, 0);
recv(sockfd, msg, MSG, 0);
printf("%s\n", msg);
close(sockfd);
}
//管道
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAXMSG 0xff
int main(){
int fd_req[2];/*声明了两个管道描述符,一个用来发询问信息*/
int fd_ans[2];/* 一个用来回答 */
pid_t pid;
char msg[MAXMSG];
if(pipe(fd_req) < 0 || pipe(fd_ans) < 0){
perror("init pipe fault...\n");
return -1;
}
if((pid = fork()) < 0){
perror("init fork fault...\n");
}
if(pid > 0){//father process
close(fd_req[1]);
close(fd_ans[0]);
int res, ask;
printf("Enter a number between 1-1000: ");
scanf("%d", &res); //输入答案
write(fd_ans[1], "start", MAXMSG); //给子进程解锁
while(1){
read(fd_req[0], msg, MAXMSG);
if(strcmp(msg, "end") == 0){ //结束标识,则结束。
close(fd_req[0]);
close(fd_ans[1]);
return 0;
}
ask = atoi(msg);
if(res > ask)
write(fd_ans[1], "yes", MAXMSG);
else
write(fd_ans[1], "no", MAXMSG);
}
}else{
int from = 0, to = 1024, mid;
read(fd_ans[0], msg, MAXMSG); //首先阻塞子进程,直到父进程发消息
while(1){
mid = (from + to) / 2;
sprintf(msg, "%d", mid);
write(fd_req[1], msg, MAXMSG); //询问
printf("Is the number bigger than %d(yes/no)?\n", mid);
read(fd_ans[0], msg, MAXMSG); //回答
printf("%s\n", msg);
if(strcmp(msg, "yes") == 0)
from = mid;
else
to = mid;
if(from + 1 == to){
printf("I have succeeded in guessing your number! ans = %d\n", to);
write(fd_req[1], "end", MAXMSG);
return ;
}
}
}
}
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <semaphore.h>
#include <stdlib.h>
#define TIME1 200
#define TIME2 199
int apple_num(int from, int to){
if(to >= from)
return to - from;
return to + 6 - from;
}
sem_t sem_apple, sem_space;
pthread_mutex_t producer_mutex, customer_mutex;
int from = 0, to = 0;
void * producer(void *args){
int time = TIME1;
while(time--){
pthread_mutex_lock(&producer_mutex);
sem_wait(&sem_space);
to = (to + 1) % 6;
printf("生产一个,当前苹果数目%d个\n", apple_num(from, to));
sem_post(&sem_apple);
pthread_mutex_unlock(&producer_mutex);
}
}
void * customer(void *args){
int time = TIME2;
while(time--){
pthread_mutex_lock(&customer_mutex);
sem_wait(&sem_apple);
from = (from + 1) % 6;
printf("消费一个,当前苹果数目%d个\n", apple_num(from, to));
sem_post(&sem_space);
pthread_mutex_unlock(&customer_mutex);
}
}
int main(){
pthread_t id1[3], id2[3];
//int ret1, ret2;
sem_init(&sem_apple, 0, 0);
sem_init(&sem_space, 0, 5);
pthread_mutex_init(&producer_mutex, NULL);
pthread_mutex_init(&customer_mutex, NULL);
int i;
for(i = 0; i < 3; i++){
if(pthread_create(&id1[i], NULL, (void *)producer, NULL) != 0){
perror("pthread_create()");
exit(1);
}
if(pthread_create(&id2[i], NULL, (void *)customer, NULL) != 0){
perror("pthread_create()");
exit(1);
}
}
for(i = 0; i < 3; i++){
pthread_join(id1[i], NULL);
pthread_join(id2[i], NULL);
}
sem_destroy(&sem_apple);
sem_destroy(&sem_space);
return 0;
}