linux-socket聊天程序-服务器(支持私聊)

linux-socket聊天程序-服务器(支持私聊)_第1张图片

说明,编译的时候加上 -lcurses -lpthread 库
代码下载http://download.csdn.net/detail/hes_c/9851906
服务器
#include "stdio.h"
#include "pthread.h"
#include "string.h"
#include "signal.h"
#include "curses.h"
#include "fcntl.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"
//-----------------------------------------------------
#define N 10

                                          //清除信息区域   
struct Point{
	int x;
	int y;
};
struct meslist{
int is;
int x;
int y;
char name[20];
char mes[256];
};
struct mesbuff{
int flag;
char buff[256];
char ip[32];
};
struct cfd{
int cfd;
int sencfd;
int life;
struct sockaddr_in caddr;
pthread_t sendpid;
pthread_t recvpid;
};

      WINDOW *wgame=NULL;
      struct Point sendPoint;
	struct Point mesPoint;
      struct meslist mymeslist[100]={0};
        
      char putbuff[256]={0};
	char getbuff[256];



int sfd;
int value;
struct sockaddr_in saddr;
struct sockaddr_in caddr;
int caddrlen;
struct cfd cfdlist[10]={0};
struct cfd mcfd;
pthread_t listent;
//------------------------------------------------
//-----------------------------------------------------
void initdate();
int CreateNet();
int CloseNet();
void listenrun();
void readrun();
int AddList(struct cfd mcfd);
int GetListN(); 
int DelList(struct cfd mcfd);
void gotoxy(int x,int y);						//光标定位
void mclear(); 									//清屏
void Hide();									//隐藏光标
void start_map();								//聊天开始界面
void Brick(int x,int y);						//打印一个砖头
void Sky(int x,int y);							//天空砖头
void init();                                               //初始化
void clearPoint(int x,int y);                              //清除光标一行
void meslistadd(char name[],char mes[]);                  //列表信息   
void showmes(); 
void showcfd();                                           //显示信息
int getn();                                                 //得到数量
void clearmes();
void clearcfd(); 
//------------------------------------------------
void initdate()
{int i;
for(i=0;icaddr.sin_addr));
struct mesbuff mes={0};
int len;
while(1){
//printf("正在接受\n");
value=read(r->cfd,&mes,sizeof mes);
//sprintf(mes.ip,"%s",inet_ntoa(r->caddr.sin_addr));
if(value>0){
//printf("接受到:%d,%s\n",mes.flag,mes.buff);
           switch(mes.flag){
                          case 0:{
     					int i;
					for(i=0;isencfd=mes.flag;
						write(mes.flag,&mes,sizeof mes);
						break;}
				     }

}
else{
char tempbuff[256]={0};
sprintf(tempbuff,"%s",inet_ntoa(r->caddr.sin_addr));
meslistadd("退出提示",tempbuff);

//printf("客户%s已经退出\n",inet_ntoa(r->caddr.sin_addr));
int i;
char buff[256]={0};
mes.flag=r->cfd;
sprintf(buff,"%s","退出");
strcpy(mes.buff,buff);
sprintf(mes.ip,"%s",inet_ntoa(r->caddr.sin_addr));
	for(i=0;isendpid);
return;}    //发生错误就关闭线程

}

}
void listenrun()
{
int i;
while(1){

value=accept(sfd,(struct sockaddr*)&caddr,&caddrlen);
//if(value=-1){printf("网络接收失败\n");break;}
struct cfd mcfd;
mcfd.cfd=value;
mcfd.life=1;
mcfd.caddr=caddr;
AddList(mcfd);
char tempbuff[256]={0};
sprintf(tempbuff,"%s",inet_ntoa(caddr.sin_addr));
meslistadd("登录提示",tempbuff);
showmes();
showcfd();
wrefresh(wgame);
//printf("客户端%s进入\n",inet_ntoa(mcfd.caddr.sin_addr));
pthread_create(&mcfd.sendpid,NULL,(void*)readrun,&mcfd);
struct mesbuff mes={0};
mes.flag=value;
sprintf(mes.ip,"%s",inet_ntoa(caddr.sin_addr));
sprintf(mes.buff,"%s","来了");
for(i=0;i

你可能感兴趣的:(开源项目)