本文主要是介绍linux-socket聊天程序-服务器(支持私聊),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
说明,编译的时候加上 -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;i<N;i++)
cfdlist[i].life=0;
caddrlen=sizeof(caddr);
}
int CreateNet() //-------------创建主网络
{
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1){
//printf("创建sfd失败\n");
meslistadd("错误提示","创建sfd失败");
showmes();
wrefresh(wgame);
return 0;}
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
saddr.sin_port=htons(11111);
value=bind(sfd,(struct sockaddr*)&saddr,sizeof saddr);
if(value==-1){
//printf("网络梆定失败\n");
meslistadd("错误提示","网络梆定失败");
showmes();
wrefresh(wgame);
return 0;}
value=listen(sfd,10);
if(value==-1){
//printf("网络监听失败\n");
meslistadd("错误提示","网络监听失败");
showmes();
wrefresh(wgame);
return 0;}
//printf("正在监听...\n");
meslistadd("提示","正在监听...");
showmes();
showcfd();
wrefresh(wgame);
//value=accept(sfd,(struct sockaddr*)&caddr,&caddrlen);
listenrun();
return 1;}int CloseNet() //--------------关闭所有资源
{
int i;
close(sfd);
for(i=0;i<N;i++)
{
if(cfdlist[i].life==1)
close(cfdlist[i].cfd);
cfdlist[i].life=0;
}
return 0;}
void readrun(void* t)
{
int value=0;
struct cfd* r=(struct cfd*)t;
//printf("%s转发进程\n",inet_ntoa(r->caddr.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;i<N;i++){if(cfdlist[i].life==1)write(cfdlist[i].cfd,&mes,sizeof mes);}break;}default:{r->sencfd=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;i<N;i++){if(cfdlist[i].life==1)write(cfdlist[i].cfd,&mes,sizeof mes);}DelList(*r);
showmes();
showcfd();
wrefresh(wgame);
pthread_exit(&r->sendpid);
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<N;i++)
{
if(cfdlist[i].life==1)
write(cfdlist[i].cfd,&mes,sizeof mes);
}
}
}void showcfd()
{
clearcfd();
int i;
int x=sendPoint.x;
int y=sendPoint.y;
gotoxy(x,y);
printf("\033[40;36m在线人数:%d\033[0m",GetListN());
y++;
for(i=0;i<N;i++){
if(cfdlist[i].life==1){
gotoxy(x,y);
printf("\033[41;36m%s\033[0m",inet_ntoa(cfdlist[i].caddr.sin_addr));
y++;
}
}}int GetListN(){
int i;
int n=0;
for(i=0;i<N;i++){
if(cfdlist[i].life==1){n++;}
}
return n;
}int DelList(struct cfd mcfd){
int i;
for(i=0;i<N;i++){
if(cfdlist[i].cfd==mcfd.cfd){cfdlist[i].life=0;return 1;}
}}int AddList(struct cfd mcfd) //添加到cfd到cfdlist中
{int i;
for(i=0;i<N;i++){
if(cfdlist[i].life==0){cfdlist[i]=mcfd;return 1;}}
return 0;
}//------------------------------------------------
int getn(){
int i;
int n=0;
for(i=0;i<100;i++)
{
if(mymeslist[i].is==0)break;
else n++;
}
if(n==0)
return 0;
else return n;
}
void clearcfd(){
int x=25;
int y=2;
for(y;y<20;y++)
for(x=25;x<38;x++)
Sky(x,y);}
void clearmes(){
int x=3;
int y=2;
for(y;y<20;y++)
for(x=3;x<23;x++)
Sky(x,y);}void showmes(){
int x=mesPoint.x;
int y=mesPoint.y;
int n=getn();
if(n==0)return ;
int i;
if(n<=18){
for(i=0;i<n;i++){
gotoxy(x,y);
printf("\033[45;36m%s:%s\033[0m",mymeslist[i].name,mymeslist[i].mes);
y++;
}}
else{
clearmes();
for(i=n-18;i<n;i++){
gotoxy(x,y);
printf("\033[45;36m%s:%s\033[0m",mymeslist[i].name,mymeslist[i].mes);
y++;}
gotoxy(23,y-1);
printf("\033[44;36m \033[0m");
}
gotoxy(sendPoint.x,sendPoint.y);}void meslistadd(char name[],char mes[]){
int n=getn();
mymeslist[n].is=1;
strcpy(mymeslist[n].name,name);
strcpy(mymeslist[n].mes,mes);}void init(){
sendPoint.x=25;
sendPoint.y=2;
mesPoint.x=3;
mesPoint.y=2;
initdate();
CreateNet();
}void clearPoint(int x,int y){
int i;
for(i=x;i<38;i++)
Sky(i,y);}void gotoxy(int x,int y){ //----------------------------------------光标定位
printf("%c[%d;%df",0x1B,y,x);
}void mclear(){ //-------------------------------------------清屏
system("clear");
}void Hide(){ //------------------------------------------隐藏光标
printf("\033[?25l\033[0m");
}void start_map(){ //------------------------------------------聊天开始界面
int i=0;
int j=0;
for(i=1;i<21;i++){for(j=1;j<40;j++){if(i==1 ||j==24|| j==1 || j==2 || j==39||i==20)Brick(j,i);elseSky(j,i);}
}
gotoxy(3,1);printf("\033[44;36mMyQQ服务器 在线人(限10人)\033[0m");// gotoxy(3,20);//printf("\033[45;36m请输入数据在下方\033[0m");
gotoxy(sendPoint.x,sendPoint.y);}void Brick(int x,int y){ //----------------------------------------打印砖头gotoxy(x,y);printf("\033[46;36m \033[0m");
}
void Sky(int x,int y){ //----------------------------------------天空砖头gotoxy(x,y);printf("\033[47;36m \033[0m");
}void lookmes(int n)
{
int i=n;
int x=mesPoint.x;
int y=mesPoint.y;
for(i;i<(getn()+n-getn()+18);i++)
{
gotoxy(x,y);
printf("\033[45;36m%s:%s\033[0m",mymeslist[i].name,mymeslist[i].mes);
y++;}}void ting(int s)
{
CloseNet();
endwin();//销毁窗口
exit(0);
}
int main(){
signal(2,ting);initscr(); wgame = derwin(stdscr,19,40,0,0);box(wgame,0,0);refresh();wrefresh(wgame);
Hide();
start_map();
init();while(1);
//pthread(th,NULL); //线程结束
return 0;
}
这篇关于linux-socket聊天程序-服务器(支持私聊)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!