#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "/usr/include/mysql/mysql.h"
#define MAX_BUF_SIZE 1024
#define UDPSERVER_PORT 8003
void login_info(char *Name,char *Password);
int land_info(char *Name,char *Password);
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW row;
struct message { //user to recieve the message
char from[20];
char passwd[20];
char to[20];
char recvmsg[MAX_BUF_SIZE];
};
struct user_file{
char user_name[20];
char passwd[20];
};
struct u_addr{ //save the online users
char user_name[20];
struct sockaddr_in user_addr;
};
char name[20];
void login_info(char *Name,char *Password){ //注册
int res;
char is[100];
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"localhost","qq","123456","infomation",0,NULL,0)){
printf("Connection success\n");
printf("%s\t%s",Name,Password);
sprintf(is,"INSERT INTO infomation(name,password) VALUES('%s','%s')",Name,Password);
res=mysql_query(&my_connection, is);
if(!res)
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
else
fprintf(stderr,"Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
mysql_free_result(res_ptr);
}
mysql_close(&my_connection);
}
int land_info(char *Name,char *Password){ //登录
int res,i,r,t;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"localhost","qq","123456","infomation",0,NULL,0)){
printf("Connection success\n");
res=mysql_query(&my_connection,"SELECT name,password FROM infomation");
if(res)
printf("Error making query:%s\n",mysql_error(&my_connection));
else{
res_ptr=mysql_store_result(&my_connection);
while((row=mysql_fetch_row(res_ptr))!=NULL){
if(row<0) break;
if(!strcmp(row[0],Name)&&!strcmp(row[1],Password)){
printf("登录成功!\n");
return 1;
}
}
}
}
mysql_free_result(res_ptr);
mysql_close(&my_connection);
return 0;
}
void udp_server(int sockfd, struct sockaddr_in *addr)
{
int n,addrlen,i,j;
struct message msg;
struct user_file user;
struct u_addr user2[30];
FILE *fp;
for(j=0;j<30;j++)
memset(&user2[j],0,sizeof(struct u_addr)); //blank the struct whitch use to save the address
addrlen = sizeof(struct sockaddr);
i=0;
printf("the service start....\n");
while(1)
{
memset(&msg, 0, sizeof(struct message));
memset(&user,0,sizeof(struct user_file));
n = recvfrom(sockfd, &msg, sizeof(struct message), 0,(struct sockaddr*)addr,&addrlen);
system("date");
printf("\nuser %s\n",msg.recvmsg);
if(!strcmp(msg.recvmsg,"register")) //registe a new user
{
/*strcpy(user.user_name,msg.from);
strcpy(user.passwd,msg.passwd);
fp=fopen("user_f","a");
if(fwrite(&user,sizeof(user),1,fp)!=1)
printf("file write error\n");
fclose(fp);
*/
login_info(msg.from,msg.passwd);
strcpy(msg.recvmsg,"注册成功!");
n=sendto(sockfd, (struct message *)&msg, sizeof(struct message), 0, (struct sockaddr *)addr, sizeof(struct sockaddr));
}
else if(!strcmp(msg.recvmsg,"login")) //login the system
{
/* fp=fopen("user_f","rb");
while(1)
{
for(j=0;j<30;j++)
if(!strcmp(msg.from,user2[j].user_name))
{
strcpy(msg.recvmsg,"此用户已登录!");
break;
}
fread(&user,sizeof(user),1,fp);
if(!strcmp(user.user_name,msg.from)&&!strcmp(user.passwd,msg.passwd))
{
memcpy(&user2[i].user_name, &msg.from,sizeof(msg.from));
memset(&msg.recvmsg,0,sizeof(msg,recvmsg));
memcpy(&user2[i++].user_addr, addr,sizeof(struct sockaddr_in));
strcpy(msg.recvmsg,"登陆成功");
break;
}
else if(feof(fp))
{strcpy(msg.recvmsg,"用户名和密码不匹配");
break;}
}
fclose(fp);*/
if(land_info(msg.from,msg.passwd))
{
memcpy(&user2[i].user_name, &msg.from,sizeof(msg.from));
memset(&msg.recvmsg,0,sizeof(msg,recvmsg));
memcpy(&user2[i++].user_addr, addr,sizeof(struct sockaddr_in));
strcpy(msg.recvmsg,"登陆成功");
}
n=sendto(sockfd, (struct message *)&msg, sizeof(struct message), 0, (struct sockaddr *)addr, sizeof(struct sockaddr));
for(j=0;j<i-1;j++)
{
n=sendto(sockfd, (struct message *)&msg, sizeof(struct message), 0, &user2[j].user_addr, sizeof(struct sockaddr));
}
continue;
}
else
{
j=0;
memset(&name,0,sizeof(name));
while(j<20)
{
if(msg.recvmsg[j]==',')
break;
name[j]=msg.recvmsg[j];
j++;
}
printf("%s",name);
fflush(stdout);
if(!strcmp(name,"online")) //display the online users
{
memset(msg.recvmsg, 0, sizeof(msg.recvmsg));
strcat(msg.recvmsg,"online user:\n");
for(j=0;j<i;j++)
{
strcat(msg.recvmsg,user2[j].user_name);
strcat(msg.recvmsg,"\n");
}
printf("%s",msg.recvmsg);
n=sendto(sockfd, (struct message *)&msg, sizeof(struct message), 0, (struct sockaddr*)addr, sizeof(struct sockaddr));
}
else if(!strcmp(name,"all")) //send to all users
for(j=0;j<i;j++)
{
n=sendto(sockfd, (struct message *)&msg, sizeof(struct message), 0, &user2[j].user_addr, sizeof(struct sockaddr));
}
else if(!strcmp(name,"exit")) // delete the user who has quit
{
for(j=0;j<i;j++)
n=sendto(sockfd, (struct message *)&msg, sizeof(struct message), 0, &user2[j].user_addr, sizeof(struct sockaddr));
for(j=0;j<i;j++)
{
if(!strcmp(user2[j].user_name,msg.from))
break;
}
int k;
for(k=j;k<i;k++){
memcpy(&user2[k],&user2[k+1],sizeof(struct u_addr));
}
i--;
}
else //s
- 1
- 2
前往页