基于Linux 文件IO的聊天室开发

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{  
    char a[20]={0};
    FILE *fp;
    fp=fopen("a.txt","a");//rb,rb+且均为作出修改//wb,wb+都不行,不能输入且清除了文件信息,准备输出到文件//注意在程序中的"a"文件在此处必须用a.txt引用
    fscanf(fp,"%s",a);
    printf("%s\n",a);
return 0 ;

}


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>   
#include <sys/types.h>  
#include <sys/stat.h>  
#include <errno.h>  
#include <string.h> 
/*


struct user{
    char name[10];
//    int num;
//    int age;
    char msg_send[80];
char msg_recv[80];
}boya,boyb,*pp,*qq;
*/
int main(void){
    int fp1,fp2;
int name_a[20],name_b[20] ;
char buf_recv[30];
char buf_send[30];
//  char ch;
   // int i;
int m,n;
 //   pp=boya;
  //  qq=boyb;
    


    printf("\n please input your name and your friend name \n");
scanf("%s %s",name_a,name_b) ;
//printf("your name is %s", boya->name");
while(1)
{
//open the file recv_b
if((fp1=open("note_recv_b.txt",O_RDWR|O_CREAT))==NULL){
        printf("Cannot open file note_recv_a any key exit!");
        getchar();
        exit(1);
}

//if new message comes


//read message from B
m = read(fp1,buf_recv,30);//read from file note_recv_b
//
if(m > 0)
{
//print message from B
printf("hello %s,your get message from %s: \n ",name_a,name_b)
// print message on screen
write(STDOUT_FILENO,buf_recv,m);//print to screen
}

//move fp to the end of file
//lseek(fp1,30, SEEK_SET);



//close file
//close(fp1) ;
//open note_recv_a to send message to B
if((fp2=open("note_recv_a.txt",O_RDWR|O_CREAT))==NULL){
        printf("Cannot open file note_recv_a any key exit!");
        getchar();
        exit(1);
}
printf("\n you can send message to your friend %s :\n",name_b);
scanf("%s",buf_send);
//n = read(STDIN_FILENO,buf_send,30);//read send msg 
//move fp2 back 30 offset
lseek(fp2,30,SEEK_SET);
write(fp2,buf_send,sizeof(buf_send));//write to note_recv_a
//close file
printf("you have send message to B:%s \n",buf_send);
//fclose(fp2);



}
close(fp1);
close(fp2);


/*
    for(i=0;i<1;i++,pp++)
        scanf("%s %s",boya->name,boya->,);
    pp=boya;
    fwrite(pp,sizeof(struct user),2,fp);
    rewind(fp);
    fread(qq,sizeof(struct user),2,fp);
    printf("\n\nname\tnumber      age      addr\n");
    for(i=0;i<2;i++,qq++)
printf("%s\t%5d%7d     %s\n",qq->name,qq->num,qq->age,qq->addr);
    fclose(fp);
*/
return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
客户端,采用多线程。一个接收服务器消息,一个发送消息给服务器。 服务器,采用select()进行IO复用。 编译文件是Makefile。 (1)用户登录: 【1】client端接收用户名和密码->存于结构体中->将结构体发送给server端。 【2】server端接收client发送的结构体->打开存储用户名密码的文件->文件写入链表中->遍历链表验证用户信息。 【3】server端验证正确发送“登陆成功”消息,错误发回“登陆失败”消息。client端接收,“登陆成功”则进入聊天,“登陆失败”则跳出。 【4】若验证成功,server端产生一个新的套接字newfd,将它与用户名封装于同一个结构体中,存储在线用户的信息。 消息、存储在线用户信息结构体: typedef struct message { int type; //服务器用于判断该执行的功能 int fd; int mode; //标志位,表示用户的发言权限,1为正常,0为禁言 char name[NAMELEN]; char mima[NAMELEN]; char from[20]; char to[20]; //聊天时的收信人 char file_name[20]; //发送文件时的文件名 char mtext[100]; //聊天时发送的消息内容 struct message *next; }Mess; (2)一对多聊天: 【1】client端发送欲发送的信息给server端。 【2】server端遍历在线人信息链表,找到每个在线人的套接字描述符,将消息发送给链表中的每个人。 【3】可以通过输入“:)”, “:(”, “bye”来发送笑脸,悲伤脸和退出聊天;检测敏感词汇“fuck”、“shit”,禁止发送。 (3)一对一聊天: 【1】client端发送欲发送的信息和信息的接收者给server端。 【2】server端根据收到的接收者名字在在线人链表中查找该接收者的套接字描述符,找到后就将消息发送给该接收者。 【3】可以通过输入“:)”, “:(”, “bye”来发送笑脸,悲伤脸和退出聊天;检测敏感词汇“fuck”、“shit”,禁止发送。 (4)文件传输 【1】client端发送预发送的文件名和接收者名字到server端。 先打开(不存在则创建)一个文件,将文件内容读到缓冲区buffer,再将buffer的内容复制到结构体Mess中,最后将结构体发送给server端。 【2】server端先将接收到的文件重命名(因为相同文件目录下不能存在同名文件),再将收到的文件和新的文件名一同放入tab1中(并且在tab1开头写“#”)发送给client端。 【3】当client端收到以“#”开头的消息,执行文件接收,先创建一个文件,再写入相应内容。 (5)管理员模式 【1】禁言 【2】解禁

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值