写个电梯的socket的代码,这个是server代码,以后写程序可以更加熟练的使用这个代码了
server:
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <signal.h>
-
#include <fcntl.h>
-
#include <pthread.h>
-
#include <errno.h>
-
-
#include <sys/wait.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <sys/stat.h>
-
#include <sys/file.h>
-
-
#include <arpa/inet.h>
-
#include <netinet/in.h>
-
-
static const char *service_pid_file = "/tmp/elevator_service.pid";
-
enum elevator_status {
-
idle = 1, up, down
-
};
-
-
typedef struct {
-
int name; //Name
-
enum elevator_status status; //Status
-
int position; //Number of floor
-
struct tm * open_time; //Bootup time
-
struct tm * close_time; //Close time
-
struct tm * run_time; //Running time
-
struct tm * idle_time; //Idle time
-
} elevator_class;
-
-
int read_pid_file(const char *pid_file)
-
{
-
FILE *f;
-
int pid = 0;
-
-
if ((f = fopen(pid_file, "r")) != NULL) {
-
fscanf(f, "%d", &pid);
-
fclose(f);
-
}
-
return (pid);
-
-
}
-
int check_elevator_service_process(void)
-
{
-
int is_locked = 1;
-
int stored_pid = read_pid_file(service_pid_file);
-
-
if ((stored_pid == 0) || (stored_pid == getpid()))
-
is_locked = 0;
-
else
-
if (kill(stored_pid, 0))
-
is_locked = 0;
-
-
return (is_locked);
-
}
-
-
int record_elevator_service_process(void)
-
{
-
int success = 0;
-
FILE *fp = NULL;
-
int fd = -1;
-
int pid = 0;
-
-
if ((fd = open(service_pid_file, (O_RDWR | O_CREAT), 0644)) != -1) {
-
if ((fp = fdopen(fd, "r+")) != NULL) {
-
if (flock(fd, (LOCK_EX | LOCK_NB)) < 0) {
-
fscanf(fp, "%d", &pid);
-
printf("Can't lock process, lock held by pid %d", pid);
-
}
-
else {
-
pid = getpid();
-
if ((fprintf(fp, "%d", pid)) && (!fflush(fp)) && (!flock(fd, LOCK_UN))) {
-
success = 1;
-
}
-
}
-
fclose(fp);
-
}
-
close(fd);
-
}
-
return (success);
-
}
-
-
int server_socket(int num)
-
{
-
int sfp;
-
struct sockaddr_in s_add;
-
unsigned short port = 0x8888;
-
-
sfp = socket(AF_INET, SOCK_STREAM, 0);
-
if(sfp == -1)
-
{
-
printf("Elevator service socket fail ! \r\n");
-
return -1;
-
}
-
printf("Elevator service socket ok !\r\n");
-
-
-
bzero(&s_add,sizeof(struct sockaddr_in));
-
s_add.sin_family=AF_INET;
-
s_add.sin_addr.s_addr=htonl(INADDR_ANY);
-
s_add.sin_port=htons(port);
-
-
if(bind(sfp, (struct sockaddr *)(&s_add), sizeof(struct sockaddr)) == -1)
-
{
-
printf("Elevator service bind fail !\r\n");
-
return -1;
-
}
-
printf("Elevator service bind ok !\r\n");
-
-
if(listen(sfp, num) == -1)
-
{
-
printf("Elevator service listen fail !\r\n");
-
return -1;
-
}
-
printf("Elevator service listen ok\r\n");
-
-
return sfp;
-
}
-
-
int elevator_accept_socket(int sockfd)
-
{
-
struct sockaddr_in peer_addr;
-
char buff[20];
-
int sin_size = sizeof(peer_addr);
-
-
memset(buff, 0, 20);
-
-
int client_fd = accept(sockfd, (struct sockaddr *) &peer_addr, (socklen_t * __restrict__) &sin_size);
-
-
printf("elevator client address is %d , sin_port is %d \n",
-
ntohl(peer_addr.sin_addr.s_addr), ntohs(peer_addr.sin_port));
-
inet_ntop(AF_INET, (void *) &peer_addr.sin_addr, buff, 20);
-
-
-
return client_fd;
-
}
-
-
int recv_socket(int sockfd, void *buffer, int size)
-
{
-
int result = -1;
-
-
result = recv(sockfd, buffer, size, MSG_WAITALL);
-
-
if (result == -1) {
-
printf("there is no data to receive \n");
-
}
-
-
return result;
-
}
-
-
int send_socket(int sockfd, void *buffer, int size)
-
{
-
int result = -1;
-
-
result = send(sockfd, buffer, size, MSG_DONTWAIT);
-
-
if (result == -1) {
-
printf("there is no data to receive \n");
-
}
-
-
return result;
-
}
-
-
void Dispatchercmd(int client_fd)
-
{
-
int num;
-
int lenth = 0;
-
elevator_class elevator;
-
int elevator_lenth = 0;
-
-
printf("DispatcherCmd \n");
-
printf("-------Evevator dispatcher commands begin\n");
-
-
lenth = recv_socket(client_fd, &num, sizeof(num));
-
-
if (lenth == -1) {
-
printf("The receive floor_num error\n");
-
goto err;
-
} else if (lenth == 0) {
-
printf("The receive floor_num is Null\n");
-
goto err;
-
} else {
-
printf("recv num = %d\n", num);
-
lenth = send_socket(client_fd, "OK", 3);
-
if (lenth == -1) {
-
printf("The send OK faile\n");
-
goto err;
-
}
-
}
-
-
elevator_lenth = recv_socket(client_fd, &elevator, sizeof(elevator));
-
-
if (elevator_lenth == -1) {
-
printf("The receive elevator data length error\n");
-
goto err;
-
} else if (elevator_lenth == 0) {
-
printf("The receive elevator data lenth is 0\n"); //notice
-
goto err;
-
} else {
-
printf("elevator.name = %d\n", elevator.name);
-
printf("elevator.status = %d\n", elevator.status);
-
-
//TodoList:
-
//Get user request and elevator status, return elevator function
-
lenth = send_socket(client_fd, "Run", 4);
-
if (lenth == -1) {
-
printf("The send OK faile\n");
-
goto err;
-
}
-
-
}
-
-
err:
-
close(client_fd);
-
}
-
int fork_child_process(void)
-
{
-
pid_t pid;
-
int status;
-
-
if (!(pid = fork())) {
-
switch (fork()) {
-
case 0:
-
return 0;
-
case -1:
-
_exit(errno);
-
default:
-
_exit(0);
-
}
-
}
-
-
printf("fork_child_process() function in Parent ");
-
-
if (pid < 0 || waitpid(pid, &status, 0) < 0)
-
return -1;
-
-
if (WIFEXITED(status)) {
-
if (WEXITSTATUS(status) == 0)
-
return 1;
-
else
-
errno = WEXITSTATUS(status);
-
} else
-
errno = EINTR;
-
-
return -1;
-
}
-
void listen_elevators_request()
-
{
-
int sockfd;
-
int clientfd;
-
pid_t child_id;
-
-
do {
-
sockfd = server_socket(5);
-
} while (sockfd < 0);
-
-
while (1)
-
{
-
printf("elevators_server_socket: %d \n", sockfd);
-
-
clientfd = elevator_accept_socket(sockfd);
-
-
if (clientfd == -1) {
-
printf("no elevator to connect to server\n");
-
continue;
-
}
-
-
printf("elevator client connect to server succecfully !\n");
-
printf("elevator the current data socket is :%d \n", clientfd);
-
-
if ((child_id = fork_child_process()) == 0) {
-
printf("elevator server child process created. pid is %d , connecting socket is %d\n",
-
getpid(), clientfd);
-
Dispatchercmd(clientfd);
-
printf("elevator clientfd = %d exit\n", clientfd);
-
exit(0);
-
} else
-
close(clientfd);
-
-
}
-
close(sockfd);
-
}
-
int main()
-
{
-
pthread_t thread_Id;
-
-
printf("Elevator service: begin Initializing ..... \n");
-
-
if (check_elevator_service_process() == 1) {
-
printf("Elevator service process already running, exiting elevator service!\n");
-
exit(1);
-
}
-
-
if (record_elevator_service_process() == 0) {
-
printf("Error writing pid to file, exiting elevator service!\n");
-
printf("Writing PID failed\n");
-
exit(1);
-
}
-
-
printf("Elevator service: end Initializing ..... \n");
-
-
if (!pthread_create(&thread_Id, NULL, (void *) &listen_elevators_request, NULL)) {
-
pthread_detach(thread_Id);
-
printf("Linste thread has been created!");
-
}
-
else
-
printf("could not create thread!");
-
-
while (1)
-
sleep(10);
-
-
printf("Elevator service code should not arrive here!");
-
return 0;
-
}
这个是client的代码
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <fcntl.h>
-
#include <unistd.h>
-
#include <pthread.h>
-
-
#include <sys/file.h>
-
#include <sys/stat.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
-
enum elevator_status {
-
idle = 1, up, down
-
};
-
-
typedef struct {
-
int name; //Name
-
enum elevator_status status; //Status
-
int position; //Number of floor
-
struct tm * open_time; //Bootup time
-
struct tm * close_time; //Close time
-
struct tm * run_time; //Running time
-
struct tm * idle_time; //Idle time
-
} elevator_class;
-
-
static const char *client_num_file = "/tmp/elevator_client.num";
-
elevator_class elevator;
-
-
int read_num_file(const char *num_file)
-
{
-
FILE *f;
-
int num = 0;
-
-
if ((f = fopen(num_file, "r")) != NULL) {
-
fscanf(f, "%d", &num);
-
fclose(f);
-
}
-
return (num);
-
}
-
int check_elevator_clinet_num()
-
{
-
int client_num = read_num_file(client_num_file);
-
-
if (client_num == 0)
-
client_num = 1;
-
else
-
client_num++;
-
-
return client_num;
-
}
-
-
int record_elevator_client_num()
-
{
-
FILE *fp;
-
int client_num = 0;
-
char str[2] = {'\0'};
-
-
client_num = check_elevator_clinet_num();
-
-
if ((fp = fopen(client_num_file, "w+")) != NULL) {
-
sprintf(str, "%d", client_num);
-
if (fwrite(str, sizeof(str), 1, fp) <= 0)
-
printf("record elevator client faile!\n");
-
fclose(fp);
-
}
-
return client_num;
-
}
-
int client_socket()
-
{
-
int cfd;
-
-
cfd = socket(AF_INET, SOCK_STREAM, 0);
-
-
if(cfd == -1)
-
{
-
printf("socket fail ! \r\n");
-
return -1;
-
}
-
-
return cfd;
-
}
-
-
int client_connect(int cfd)
-
{
-
struct sockaddr_in s_add;
-
unsigned short portnum = 0x8888;
-
-
bzero(&s_add,sizeof(struct sockaddr_in));
-
s_add.sin_family=AF_INET;
-
s_add.sin_addr.s_addr= inet_addr("127.0.0.1");
-
s_add.sin_port=htons(portnum);
-
-
if(connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)) == -1)
-
{
-
printf("connect fail !\r\n");
-
return -1;
-
}
-
-
return 1;
-
}
-
-
int recv_socket(int sockfd, void *buffer, int size)
-
{
-
int result = -1;
-
-
result = recv(sockfd, buffer, size, MSG_WAITALL);
-
-
if (result == -1) {
-
printf("there is no data to receive \n");
-
}
-
-
return result;
-
}
-
int send_socket(int sockfd, void *buffer, int size)
-
{
-
int result = -1;
-
-
result = send(sockfd, buffer, size, MSG_DONTWAIT);
-
-
if (result == -1) {
-
printf("there is no data to receive \n");
-
}
-
-
return result;
-
}
-
void listen_user_request()
-
{
-
int num;
-
int lenth = 0;
-
int sockfd;
-
char res[2];
-
-
printf("listen_user_request start ...\n");
-
-
while (1) {
-
-
do {
-
sockfd = client_socket();
-
} while (sockfd < 0);
-
-
if (client_connect(sockfd) == -1)
-
{
-
sleep(1);
-
continue;
-
}
-
-
printf("please input num : \n");
-
scanf("%d", &num);
-
-
lenth = send_socket(sockfd, &num, sizeof(int));
-
-
if (lenth == -1) {
-
printf("The send num faile\n");
-
close(sockfd);
-
continue;
-
}
-
-
lenth = recv_socket(sockfd, res, 3);
-
-
if (lenth == -1) {
-
printf("The recv faile\n");
-
close(sockfd);
-
continue;
-
}
-
-
if (strcmp("OK", res)) {
-
close(sockfd);
-
continue;
-
}
-
-
printf("%s\n", res);
-
-
lenth = send_socket(sockfd, &elevator, sizeof(elevator_class));
-
-
if (lenth == -1) {
-
printf("The send elevator_status faile\n");
-
close(sockfd);
-
continue;
-
}
-
-
lenth = recv_socket(sockfd, res, 4);
-
-
if (lenth == -1) {
-
printf("The recv faile\n");
-
close(sockfd);
-
continue;
-
}
-
-
if (strcmp("Run", res)) {
-
close(sockfd);
-
continue;
-
}
-
printf("%s\n", res);
-
printf("The %d elevator start run!\n", elevator.name);
-
close(sockfd);
-
}
-
}
-
-
struct tm * get_current_time()
-
{
-
time_t rawtime;
-
struct tm * timeinfo;
-
-
time(&rawtime); /* get the current time */
-
timeinfo = localtime(&rawtime);
-
-
printf("The current time is: %s\n", asctime(timeinfo));
-
printf("%4d-%02d-%02d %02d:%02d:%02d\n",
-
1900+timeinfo->tm_year, /* year */
-
1+timeinfo->tm_mon, /* month */
-
timeinfo->tm_mday, /* date */
-
timeinfo->tm_hour, /* hour */
-
timeinfo->tm_min, /* minute */
-
timeinfo->tm_sec); /* second */
-
-
return timeinfo;
-
}
-
int main()
-
{
-
pthread_t thread_Id;
-
printf("Elevator clients: begin Initializing ..... \n");
-
-
int client_num = record_elevator_client_num();
-
-
if (client_num == 0)
-
printf("Error get elevator client num to file!\n");
-
-
elevator.name = client_num;
-
elevator.status = idle;
-
elevator.open_time = get_current_time();
-
elevator.close_time = 0;
-
elevator.run_time = 0;
-
elevator.idle_time = 0;
-
-
if (!pthread_create(&thread_Id, NULL, (void *) &listen_user_request, NULL)) {
-
pthread_detach(thread_Id);
-
printf("Linste thread has been created!");
-
}
-
else
-
printf("could not create thread!");
-
-
while (1)
-
sleep(10);
-
-
}
需要添加的功能:server上的判断算法,client端的client的数量需要通过server传送过来,还有client的执行工作的顺序。以后有时间慢慢添加。
主要熟悉下stock的编程,顺便练练手再,老不写代码手的生疏了
阅读(1913) | 评论(0) | 转发(0) |