在互联网上聊天对很多“网虫”来说已经是家常便饭了。聊天室程序可以说是网上最简单的多点通信程序。
花了很长时间用来联系掌握Linux 上Socket的一个聊天室程序,可以
/*
*i.h is a used for creating a library
*for server client
*Mar 18 2010
*
*/
#ifndef _I_H
#define _I_H
#include <math.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <signal.h>
#include <ncurses.h>
#include <math.h>
#define SEVR_IP "127.0.0.1"
#define SEVR_PORT 8081
#define CNTNT_LEN 150
#define MSG_LEN sizeof(struct msg)
#define ADDR_LEN sizeof(struct sockaddr)
#define USR_LEN sizeof(struct user)
#define PRT_LEN 8
#define HSTR_LEN sizeof(struct chat_history)
/* declare Global variables */
int mainfd;/* used as chat histroy file handle*/
int sockfd;/* used as socket local handle */
int count;
struct sockaddr_in server;
/* msg is used for communicating message */
struct msg
{
int flag; /* flag meaning:1,ordinary; 2,log msg; 3,reg msg, other,file*/
int id_from;
int id_to;
char content[CNTNT_LEN];
char append[10];
};
/* user is used information list */
struct user
{
int id;
char name[10];
char password[10];
char *p_chatlog;
struct sockaddr user_addr;
};
/* chat_history used for reading chat history */
struct chat_history
{
char content[CNTNT_LEN];
char time[25];
int to;
int from;
int count;
};
/* i_functions below is funtions needed by both client and sever */
extern int i_saveto_chat(struct msg *pmsg);
int i_clean_stdin ()
{
while ('\n' == getchar())
{
continue;
}
return(0);
}
int i_print(char *pmsg, int size)
{
int i = 1;
for (i; i<= size; i++)
{
if (*pmsg != '\n')
{
printf("%c", *pmsg);
pmsg ++;
}
else
{
return(0);
}
}
return(0);
}
int i_input(char *p_input)
{
char c = '\0';
int i;
for (i = 0; i < CNTNT_LEN; i++)
{
p_input[i] = getchar();
if (p_input[i] =='\n')
{
return(0);
}
}
printf("you have input long enough!\n");
return(0);
}
int i_socket(int domain, int type, int protocol)
{
int fd;
if ((fd = socket(domain, type, protocol)) == -1)
{
perror("creat socket error:");
exit(1);
}
return(fd);
}
int i_bind(int fd, const struct sockaddr *addr, int namelen)
{
if (-1 == bind(fd, addr, namelen))
{
perror("i_bind error:");
exit(1);
}
return (0);
}
int i_recvfrom(int fd, void *buf, size_t len, int flags,
struct sockaddr *addr, int *size)
{
if (-1 == recvfrom(fd, buf, len, flags, addr, size))
{
perror("i_recvfrom error:");
exit(1);
}
return(0);
}
int i_sendto(int fd, void *buf, size_t len, int flags,
struct sockaddr *addr, int size)
{
if (-1 == sendto(fd, buf, len, flags, addr, size))
{
perror("i_sendto error");
exit(1);
}
return (0);
}
int i_open(const char *pathname, int flags)
{
int fd;
if ((fd = open(pathname, flags)) == -1)
{
perror("open_failed");
exit(1);
}
return (fd);
}
int i_read(int fd, void *msg, int len)
{
if(-1 == read(fd, msg, len))
{
perror("i_read error");
exit(1);
}
return(0);
}
int i_write(int fd, void *msg, int len)
{
if (-1 == write(fd, msg, len))
{
perror("i_write error");
exit(0);
}
return(0);
}
/* init a socket,file and server addr */
int i_init()
{
mainfd = i_open("./chat_log", O_RDWR|O_CREAT);
sockfd = i_socket(AF_INET, SOCK_DGRAM, 0);