日期:2014-05-16  浏览次数:20660 次

一个基于linux的聊天软件(并发服务器,sqlite3数据库,有注释)

SC.h:

#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <sqlite3.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define ADMIN_SHOW 1
#define ADMIN_BAN 2
#define ADMIN_LIFT 3
#define ADMIN_KICK 4
#define ADMIN_EXIT 5

typedef struct myinfo
{
    int flt;
    char name[20];
    char sec[20];
}MYINFO;

typedef struct message
{
    int flt;
    char myname[20];
    char name[20];
    char message[200];
}MES;

typedef struct rev
{
    char myname[20];
    char message[200];
    int fd;
}REV;

typedef struct online
{
    int num;
    char name[8][20];
}ONLINE;


extern void s_chat(int,sqlite3 *,MES *,REV *);
extern void s_view(int,sqlite3 *,MES *);
extern void s_file(int,sqlite3 *,MES *,REV *);
extern void s_getfile(int);


packet.c:

#include "../../include/SC.h"

int Socket(int family,int type,int protocol)
{
    int sockfd;
    if((sockfd = socket(family,type,protocol)) < 0)
    {
        perror("socket error!\n");
	exit(-1);
    }
    return sockfd;
}

int Bind(int sockfd,const struct sockaddr * myaddr,socklen_t addrlen)
{
    int ret;
    if((ret = bind(sockfd,myaddr,addrlen)) < 0)
    {
        perror("bind error!\n");
	exit(-1);
    }
    return ret;
}

int Accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen)
{
    int ret;
    return (ret = accept(sockfd,cliaddr,addrlen));
}

int Connect(int sockfd,const struct sockaddr *servaddr,socklen_t addrlen)
{
    int ret;
    if((ret = connect(sockfd,servaddr,addrlen)) == -1)
    {
        perror("connect error!\n");
	exit(-1);
    }
    return ret;
}

pid_t Fork(void)
{
    pid_t pid;
    if((pid = fork()) < 0)
    {
        perror("fork error!\n");
	exit(-1);
    }
    return pid;
}

int Shmget(key_t key,int size,int shmflg)
{
    int ret;
    if((ret = shmget(key,size,shmflg)) < 0)
    {
        perror("shmget error!\n");
	exit(-1);
    }
    return ret;
}

void *Shmat(int shmid,const void *shmaddr,int shmflg)
{
    void *shmptr = NULL;
    if((shmptr = shmat(shmid,shmaddr,shmflg)) == (void *)(-1))
    {
        perror("shmat error!\n");
	exit(-1);
    }
    return (void *)shmptr;
}

int Open(const char *pathname,int flags,mode_t mode)
{
    int fd;
    if((fd = open(pathname,flags,mode)) == -1)
    {
        perror("open error!\n");
    }
    return fd;
}

int Close(int fildes)
{
    int ret;
    if((ret = close(fildes)) == -1)
    {
        perror("close error!\n");
	exit(-1);
    }
    return ret;
}

ssize_t Write(int fd,const void *buf,size_t count)
{
    ssize_t ret;
    if((ret = write(fd,buf,count)) == -1)
    {
        perror("write error!\n");
	exit(-1);
    }
    return ret;
}

ssize_t Read(int fd,void *buf,size_t count)
{
    ssize_t ret;
    if((ret = read(fd,buf,count)) == -1)
    {
        perror("read error!\n");
	exit(-1);
    }
    return ret;
}

void *Malloc(size_t size)
{
    void *ret;
    if((ret = malloc(size)) == NULL)
    {
        perror("malloc error!\n");
	exit(-1);
    }
    return ret;
}

void Free(void *memblock)
{
    free(memblock);
}


server.c:

/**********************************************************
                    ZYP Instant Messager
Filename:server.c
Author:zyp
Description:server main
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:int main()
Return:int
Argument:null
Description:main
*************