当前位置: 技术问答>linux和unix
liunx 套接字编程 数据写入文件问题
来源: 互联网 发布时间:2016-06-23
本文导语: 服务器 #include #include #include #include #include #include #include #include #define SERVERPORT 7788 #define BACKLOG 10 typedef struct user { char u_name[20]; char u_pwd[16]; }USER; typedef struct Reg { int ncount; // 有多少用户 ...
服务器
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVERPORT 7788
#define BACKLOG 10
typedef struct user
{
char u_name[20];
char u_pwd[16];
}USER;
typedef struct Reg
{
int ncount; // 有多少用户
USER usr[1024]; //最大用户人数
}REGISTER;
int regi(char type_ch[]);
int Login(char type_ch[]);
char type_ch[36]={''}; //保存客户端发送过来的用户信息
int main(void)
{
int sockfd, client_fd,flag;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
char buf[30]={''};
char cmd[BACKLOG]={''};
int sin_size,recvtype,type;
USER users;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket:");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVERPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("Bind:");
exit(1);
}
if(listen(sockfd, BACKLOG) == -1)
{
perror("Listen:");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
{
perror("Accept:");
exit(1);
}
printf("Received a connection from %sn", inet_ntoa(remote_addr.sin_addr));
/* if(send(client_fd, cmd,strlen(cmd),0) == -1)
{
perror("Send:");
}
*/
if(!fork())
{
if( (recvtype = recv(client_fd, cmd,BACKLOG,0)) == -1 )
{
perror("recv");
}
cmd[recvtype]='';
printf("%s",cmd);
if( strcmp(cmd,"rege") == 0 )
{
printf("命令接受成功n");
if(type =recv(client_fd,type_ch,36,0) == -1)
{
}
// users = split(type_ch); //拆分
flag = regi(type_ch);
}
else if( strcmp(cmd,"login") == 0)
{
printf("命令接受成功n");
if(type =recv(client_fd,type_ch,36,0) == -1)
{
}
type_ch[type]='';
// users = split(type_ch);
flag = Login(type_ch);
}else
{
printf("命令错误 n");
}
close(client_fd);
}
close(client_fd);
}
}
int regi(char type_ch[]) //注册
{
FILE *rfp,*ufp;
int i,j,flag = 1;
REGISTER regd;
regd.ncount=0;
USER file_usr,usr;
for(i=0,j=0; type_ch[i] != ''; i++)
{
if(flag)
{
usr.u_name[i] = type_ch[i];
if(type_ch[i] == ' ' )
{
flag=0;
usr.u_name[i] ='';
}
}
else
{
usr.u_pwd[j] = type_ch[i];
j++;
}
}
rfp = fopen("user.txt" , "r+");
ufp = fopen("reg.txt", "w+");
if( rfp == NULL)
{
write(rfp, usr,sizeof(usr));
regd.ncount++;
write(ufp,regd,sizeof(regd));
printf(" 帐号密码写入成功n");
close(rfp);
close(ufp);
return 1;
}
else while( !feof(rfp) )
{
read(rfp,file_usr,20);
if(strcmp(file_usr.u_name,usr.u_name) == 0)
{
printf("用户已经存在n");
close(rfp);
close(ufp);
return 0;
}
}
regd.ncount++;
write(rfp,usr,sizeof(usr));
write(ufp, regd,sizeof(regd));
printf("帐号写入成功");
close(rfp);
close(ufp);
return 1;
}
int Login(char type_ch[]) //登陆
{
FILE *rfd, *ufd;
int i ,j,flag =1;
REGISTER reg;
USER usr;
for(i=0,j=0; type_ch[i] != ''; i++)
{
if(flag)
{
usr.u_name[i] = type_ch[i];
if(type_ch[i] != ' ' )
{
flag=0;
}
}
else
{
usr.u_pwd[j]=type_ch[i];
j++;
}
}
rfd =fopen("reg.txt", "w+");
ufd = fopen("user.txt","r+");
if(rfd == NULL || ufd == NULL)
{
perror("Cann't find the user information!!");
return 0;
}
if( !feof(rfd) )
{
fscanf(rfd,"%d",®.ncount);
}
fclose(rfd);
for( i = 0; i
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVERPORT 7788
#define BACKLOG 10
typedef struct user
{
char u_name[20];
char u_pwd[16];
}USER;
typedef struct Reg
{
int ncount; // 有多少用户
USER usr[1024]; //最大用户人数
}REGISTER;
int regi(char type_ch[]);
int Login(char type_ch[]);
char type_ch[36]={''}; //保存客户端发送过来的用户信息
int main(void)
{
int sockfd, client_fd,flag;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
char buf[30]={''};
char cmd[BACKLOG]={''};
int sin_size,recvtype,type;
USER users;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket:");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVERPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("Bind:");
exit(1);
}
if(listen(sockfd, BACKLOG) == -1)
{
perror("Listen:");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
{
perror("Accept:");
exit(1);
}
printf("Received a connection from %sn", inet_ntoa(remote_addr.sin_addr));
/* if(send(client_fd, cmd,strlen(cmd),0) == -1)
{
perror("Send:");
}
*/
if(!fork())
{
if( (recvtype = recv(client_fd, cmd,BACKLOG,0)) == -1 )
{
perror("recv");
}
cmd[recvtype]='';
printf("%s",cmd);
if( strcmp(cmd,"rege") == 0 )
{
printf("命令接受成功n");
if(type =recv(client_fd,type_ch,36,0) == -1)
{
}
// users = split(type_ch); //拆分
flag = regi(type_ch);
}
else if( strcmp(cmd,"login") == 0)
{
printf("命令接受成功n");
if(type =recv(client_fd,type_ch,36,0) == -1)
{
}
type_ch[type]='';
// users = split(type_ch);
flag = Login(type_ch);
}else
{
printf("命令错误 n");
}
close(client_fd);
}
close(client_fd);
}
}
int regi(char type_ch[]) //注册
{
FILE *rfp,*ufp;
int i,j,flag = 1;
REGISTER regd;
regd.ncount=0;
USER file_usr,usr;
for(i=0,j=0; type_ch[i] != ''; i++)
{
if(flag)
{
usr.u_name[i] = type_ch[i];
if(type_ch[i] == ' ' )
{
flag=0;
usr.u_name[i] ='';
}
}
else
{
usr.u_pwd[j] = type_ch[i];
j++;
}
}
rfp = fopen("user.txt" , "r+");
ufp = fopen("reg.txt", "w+");
if( rfp == NULL)
{
write(rfp, usr,sizeof(usr));
regd.ncount++;
write(ufp,regd,sizeof(regd));
printf(" 帐号密码写入成功n");
close(rfp);
close(ufp);
return 1;
}
else while( !feof(rfp) )
{
read(rfp,file_usr,20);
if(strcmp(file_usr.u_name,usr.u_name) == 0)
{
printf("用户已经存在n");
close(rfp);
close(ufp);
return 0;
}
}
regd.ncount++;
write(rfp,usr,sizeof(usr));
write(ufp, regd,sizeof(regd));
printf("帐号写入成功");
close(rfp);
close(ufp);
return 1;
}
int Login(char type_ch[]) //登陆
{
FILE *rfd, *ufd;
int i ,j,flag =1;
REGISTER reg;
USER usr;
for(i=0,j=0; type_ch[i] != ''; i++)
{
if(flag)
{
usr.u_name[i] = type_ch[i];
if(type_ch[i] != ' ' )
{
flag=0;
}
}
else
{
usr.u_pwd[j]=type_ch[i];
j++;
}
}
rfd =fopen("reg.txt", "w+");
ufd = fopen("user.txt","r+");
if(rfd == NULL || ufd == NULL)
{
perror("Cann't find the user information!!");
return 0;
}
if( !feof(rfd) )
{
fscanf(rfd,"%d",®.ncount);
}
fclose(rfd);
for( i = 0; i
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!