|
发表于 2006-4-21 11:25:12
|
显示全部楼层
看了上火呀,没看懂呀。
版主别生气了,先给给说说吧。
我替他改中不?
[code:1]
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h> /*aid -lpthread while compling*/
//#include <mysql/mysql.h> /*aid -lmysqlclient while compling*/
#include <errno.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
//#include "motley_talk.h"
#define SERVER_PORT 9527
#define MAXDATASIZE 4096
#define BACKLOG 10
#define STDIN 0
//Message constant
char *SERVER_CLIENT_FIRST = "Login motley_talk server OK! Welcome!";
char *SERVER_QUIT_MSG = "server quit";
//pthread_mutex variable
pthread_mutex_t file_mutex;// = PTHREAD_ERRORCHECK_MUTEX_INITIALZER_NP;
//pthread_mutex_t db_mutex;
//database variable
/*char *host = "localhost";
char *username = "root";
char *password = "root";
char *database = "motley_talk_db";*/
//file variable
int offset_write = 0;
int last_offset_r = 0;//file read offset
int msg_id_count = 3;
//variable values for insert database
//int last_id_file;
char *message_file;
//functions,running in pthread
static void *client_pthread(void *);/*client_pthread for each user*/
//static void *msg_file_read(void *temp);/*read file for database*/
//file read and write functions,running in pthread functions
int msg_file_rw(char rw, int last_msg_id, char *message_msg);
//static void *db_pthread(void *temp);/*insert database message for all users*/
int main(int argc, char *argv[])
{
int server_sockfd, *client_temp;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int size_of_addr;
pthread_t db_pthread_id;
pthread_t client_pthread_id;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_TIMED_NP);
pthread_mutex_init(&file_mutex, &attr);//PTHREAD_MUTEX_ERRORCHECK_NP);
system("rm -f msg_file");
// system("vi msg_file");
if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("create server_sockfd error!");
exit(-1);
}
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
server_address.sin_addr.s_addr = INADDR_ANY;/*server's address is local address*/
if(bind(server_sockfd, (struct sockaddr *)&server_address,sizeof(struct sockaddr)) == -1)
{
perror("bind error!");
exit(-1);
}
if(listen(server_sockfd, BACKLOG) == -1)
{
perror("listen error!");
exit(-1);
}
// pthread_create(&db_pthread_id, NULL, &db_pthread, NULL);
int t;
for(t=0; t<5; t++)
{
size_of_addr = sizeof(client_address);
client_temp = malloc(sizeof(int));
*client_temp = accept(server_sockfd, (struct sockaddr *)&client_address, &size_of_addr);
pthread_create(&client_pthread_id, NULL, &client_pthread, client_temp);
}
pthread_mutex_destroy(&file_mutex);
close(server_sockfd);
exit(0);
}
/*
* Description: client's pthread
* Input: client's sockfd from Main
* Output: Message
* Returns: 0 for correct, -1 for error
*/
static void *client_pthread(void *client_temp)
{
int client_sockfd;
int ret;
fd_set rfd_set, wfd_set, efd_set;
client_sockfd = *((int *)client_temp);
free(client_temp);
struct timeval time_out;
int send_len=0;
int numb_recv;
char recv_buf[MAXDATASIZE];
char send_buf[MAXDATASIZE];
char server_name[9] = "server ";
char msg_server[9] = "server ";
msg_server[7] = ':';
msg_server[9] = '\0';
int i;
if(send(client_sockfd, SERVER_CLIENT_FIRST, strlen(SERVER_CLIENT_FIRST), 0) == -1)
{
perror("send error!");
exit(-1);
}
fcntl(client_sockfd, F_SETFD, O_NONBLOCK);
while(1)
{
FD_ZERO(&rfd_set);
FD_ZERO(&wfd_set);
FD_ZERO(&efd_set);
FD_SET(STDIN, &rfd_set);
FD_SET(client_sockfd, &rfd_set);
FD_SET(client_sockfd, &wfd_set);
FD_SET(client_sockfd, &efd_set);
for(i=0; i<10; i++)
{
msg_server[i] = server_name[i];
}
msg_server[7] = ':';
time_out.tv_sec = 2;
time_out.tv_usec = 0;
ret = select(client_sockfd+1, &rfd_set, &wfd_set, &efd_set, &time_out);
if(ret < 0)
{
perror("select error!");
exit(-1);
}
/**/ if(FD_ISSET(STDIN, &rfd_set))
{
fgets(send_buf, MAXDATASIZE, stdin);
send_buf[strlen(send_buf)-1] = '\0';
if(strncmp("quit", send_buf, 4) == 0)
{
if(send(client_sockfd, SERVER_QUIT_MSG, 11, 0) == -1)
{
perror("send error!");
exit(-1);
}
pthread_mutex_lock(&file_mutex);
msg_id_count = msg_id_count + 1;
offset_write = offset_write + 11;
msg_file_rw('w', msg_id_count, SERVER_QUIT_MSG);
pthread_mutex_unlock(&file_mutex);
usleep(50000);
break;
}
strcat(msg_server, send_buf);
if((send_len = strlen(msg_server) + strlen(send_buf)) >= MAXDATASIZE)
{
send_len = MAXDATASIZE;
}
if(send(client_sockfd, msg_server, send_len, 0) == -1)
{
perror("send error!");
exit(-1);
}
pthread_mutex_lock(&file_mutex);
msg_id_count = msg_id_count + 1;
offset_write = offset_write + send_len;
msg_file_rw('w', msg_id_count, msg_server);
pthread_mutex_unlock(&file_mutex);
bzero(msg_server,sizeof(msg_server));
bzero(send_buf, sizeof(send_buf));
/**/ }
if(FD_ISSET(client_sockfd, &rfd_set))
{
if((numb_recv = recv(client_sockfd, recv_buf, MAXDATASIZE, 0)) == -1)
{
perror("recv error!");
exit(-1);
}
recv_buf[numb_recv] = '\0';
pthread_mutex_lock(&file_mutex);
msg_id_count =msg_id_count + 1;
offset_write = offset_write + numb_recv;
msg_file_rw('w', msg_id_count, recv_buf);
pthread_mutex_unlock(&file_mutex);
if(strncmp("user logout", recv_buf, 11) == 0)
{
usleep(50000);
break;
}
printf("%s\n", recv_buf);
bzero(recv_buf, sizeof(recv_buf));
// fflush(stdout);
}
if(FD_ISSET(client_sockfd, &efd_set))
{
close(client_sockfd);
exit(-1);
}
}
close(client_sockfd);
return 0;
}
/*
* Description: use hte last message_id and message_msg, write them into the msg_file
* Input: last message_id and the message
* Output: NULL
* Returns: -1 for error, 0 for read one message, 1 for file end
* Attention:
*/
int msg_file_rw(char rw, int last_msg_id, char *message_msg)
{
FILE *msg_file_fp;
int bytes_read;
size_t nbytes =0;
char *strings_line = NULL;
if(rw == 'w')
{
msg_file_fp = fopen("msg_file","ab");
if(msg_file_fp == NULL)
{
printf("fail to open msg_file while writing!\n");
return -1;
}
// fseek(msg_file_fp, last_offset, SEEK_SET);
fprintf(msg_file_fp, "(%d, \"%s\")\n", last_msg_id, message_msg);
fflush(msg_file_fp);
// fsync(msg_file_fp);
fclose(msg_file_fp);
return 0;
}
else
{
msg_file_fp = fopen("msg_file","r");
if(msg_file_fp == NULL)
{
printf("fail to open msg_file while reading!\n");
return -1;
}
fseek(msg_file_fp, last_offset_r, SEEK_SET);
if((bytes_read = getline(&strings_line, &nbytes, msg_file_fp)) != -1)
{
if(strings_line[strlen(strings_line) -1] == '\n')
{
strings_line[strlen(strings_line)-1] = '\0';
}
message_file = strings_line;
last_offset_r = last_offset_r + strlen(strings_line) + 1;
fclose(msg_file_fp);
return 0;//file not end
}
else
{
fclose(msg_file_fp);
return 1;//file end
}
}
}
[/code:1] |
|