- 论坛徽章:
- 0
|
/* .............SERVER,............,..........(.......) */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define FIFOSERVER "./server_fifo"
#define FIFOCLIENT "./client_%d_fifo"
#define PORT 1234
#define BACKLOG 2
#define MAXDATASIZE 1000
void process_client(int, struct sockaddr_in);
void process_server(void);
struct dataform {
pid_t dpid;
char name[MAXDATASIZE];
char data[MAXDATASIZE];
};
struct nodeform {
pid_t pid;
int fifo_id;
};
int
main(int argc, char *argv[])
{
int listenfd, connectfd;
pid_t pid;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
//creat tcp socket
if ((listenfd = socket(AF_INET, SOCK_SREAM, 0)) == -1) {
} else {
}
//reuse addr
int opt;
opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
//set server addr
bzero(&server, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
//bind addr
if (bind(listenfd, (struct sockaddr *) & server, sizeof(struct sockaddr)) == -1) {
} //......, EXIT(1);
else
{
} //......, printf("....... ok!!");
//listen port
if (listen(listenfd, BACKLOG) == -1) {
} else {
}
sin_size = sizeof(strurt sockaddr_in);
//creat process_server
if ((pid = fork) == 0) {
process_server();
} else if (pid < 0) {
exit(1);
} else {
printf("fork ok!!");
}
while (1) {
//accept client
if ((connectfd = accept(listenfd, (struct sockaddr *) & client), &sin_size) == -1) {
} else {
}
//creat process_client
if ((pid = fork()) > 0) {
close(connectfd);
continue;
} else if (pid == 0) {
close(listenfd);
//call child process
process_client(connectfd, client);
exit(0);
} else {
exit(1);
}
}
close(listenfd);
return 0;
}
//child process client
void
process_client(int connectfd, struct sockaddr_in client)
{
int num;
char recvbuf[MAXDATASIZE];
char sendbuf[MAXDATASIZE];
char namebuf[MAXDATASIZE];
char *buf = "->:";
int server_fifo_id;
int client_fifo_id;
char clientfifo[50];
struct dataform my_data;
pid_t pid;
int nready, maxfd;
fd_set allset;
const char *Q = "/Q";
const char *QUIT = "/QUIT";
const char *q = "/q";
const char *quit = "/quit";
maxfd = connectfd;
//open serverfifo
if ((server_fifo_id = open(FIFOSERVER, O_WRONLY)) == -1) {
} else {
}
pid = getpid();
my_data.dpid = pid;
bzero(&my_data.data, strlen(my_data.data));
//write my_data into server fifo
if (write(server_fifo_id, &my_data, sizeof(struct dataform)) == -1) {
} else {
}
sprintf(clientfifo, FIFOCLIENT, pid);
//creat clientfifo
mkfifo(clientfifo, 0777);
//open clientfifo
if ((client_fifo_id = open(clientfifo, O_RDONLY)) == -1) {
} else {
if (client_fifo_id > maxfd)
maxfd = client_fifo_id;
}
printf("You got a connection from :%s", inet_ntoa(client.sin_addr));
//recv client name into namebuf
if ((num = recv(connectfd, namebuf, MAXDATASIZE, 0)) == 0) {
close(connectfd);
return;
}
namebuf[num - 1] = '\0';
printf("Client's name is :%", namebuf);
while (1) {
FD_ZERO(&allset);
FD_SET(connectfd, &allset);
FD_SET(client_fifo_id, &allset);
nready = select(maxfd + 1, &allset, NULL, NULL, NULL);
if (FD_ISSET(connectfd, &allset)) {
//recv data from client
num = recv(connectfd, recvbuf, MAXDATASIZE, 0);
recvbuf[num - 1] = '\0';
if (!strcmp(recvbuf, Q) || !strcmp(recvbuf, QUIT) || !strcmp(recvbuf, q) || !strcmp(recvbuf, quit)) {
close(connectfd);
}
printf("Received client(%s) message:%s", namebuf, recvbuf);
//copy data into my_data
my_data.dpid = pid;
strcpy(my_data.name, namebuf);
strcpy(my_data.data, recvbuf);
//write my_data into server fifo
if (write(server_fifo_id, &my_data, sizeof(struct dataform)) == -1) {
} else {
}
}
if (FD_ISSET(client_fifo_id, &allset)) {
//read my_data from client fifo
if (read(client_fifo_id, &my_data, sizeof(struct dataform)) == -1) {
} else {
strcpy(sendbuf, my_data.data);
//send data to client
send(connectfd, sendbuf, MAXDATASIZE, 0);
}
}
}
close(connectfd);
}
void
process_server(void)
{
struct dataform my_data;
struct nodeform node[255];
//255 client
int server_fifo_id;
int client_fifo_id;
int i;
pid_t pid;
const char *Q = "/Q";
const char *QUIT = "/QUIT";
const char *q = "/q";
const char *quit = "/quit";
const char *mm = "->:";
for (i = 0; i < 255; i++)
node.pid = 0;
//creat serverfifo
mkfifo(FIFOSERVER, 0777);
//open serverfifo
if ((server_fifo_id = open(FIFOSERVER, O_RDONLY)) == -1) {
} else {
}
while (1) {
//read data from serverfifo
// into my_data
bzero(&my_data, sizeof(struct dataform));
if (read(server_fifo_id, &my_data, sizeof(struct dataform)) == -1) {
} else {
}
if (strlen(my_data.data) == 0) {
//set clientfifo key from my_data.dpid
sprintf(clientfifo, FIFOCLIENT, my_data.dpid);
usleep(10000);
//open clientfifo(only write)
if ((client_fifo_id = open(clientfifo, O_WRONLY)) == -1) {
} else {
for (i = 0; i < 255; i++) {
if (node.pid == my_data.dpid) {
break;
}
if (node.pid == 0) {
node.pid = my_data.dpid;
node.fifo_id = client_fifo_id;
break;
}
}
}
continue;
} else {
strcpy(buf, my_data.data);
if (!strcmp(recvbuf, Q) || !strcmp(recvbuf, QUIT) || !strcmp(recvbuf, q) || !strcmp(recvbuf, quit)) {
printf("-------------------");
for (i = 0; i < 255; i++)
if (node.pid == my_data.dpid) {
node.pid = 0;
node.fifo_id = 0;
}
printf("kill %d", my_data.dpid);
kill(my_data.dpid);
continue;
}
my_data.dpid = 0;
strcpy(my_data.data, my_data.name);
strcat(my_data.data, mm);
strcat(my_data.data, buf);
for (i = 0; i < 255 i++)
if (node.pid) {
if (write(node.fifo_id, &my_data, sizeof(struct dataform)) == -1) {
} else {
}
}
bzero(&my_data, sizeof(struct dataform));
}
}
}
是这样吗?
[ 本帖最后由 langue 于 2007-1-12 19:25 编辑 ] |
|