int res_title[81];
int res_content[501];
void search(char* key,char *title,char* content)
{
int i=0,j=0,k=0,t=0,c=0;
int len_k,len_t,len_c;
len_k=strlen(key);
len_t=strlen(title);
len_c=strlen(content);
for(i=0;i<len_k;i++){
for(j=0;j<len_t;j++){
if(key[i]==title[j]){
res_title[t]=j;
}
}
for(k=0;k<len_c;k++){
if(key[i]==content[k]){
res_content[c]=k;
}
}
}
}
zhujiemian1.c:
//#include <gdk/gdkkeysyms>
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#define i 10
/*全局变量****************************************************************************************/
char* host_name="127.0.0.1";//"192.168.100.1";
int port=8888;
GtkWidget *entry1;//收件人gg
GtkWidget *entry11;//密送gg
GtkWidget *entry2;//主题gg
char *text;
static GtkWidget *window_color;
static GdkColor color;
static GtkWidget *drawarea;
GtkWidget *view;//编辑窗口
char name[20]="hjlhsa";//邮件地址gggggggggggggggggg
const char *nickname="guosiyu";
const char *weidu_number="3";
//char weidu_number[10];
/*函数声明*****************************************************************************************/
int change_color ( );//改变颜色
void on_ok_clicked (GtkButton* button, gpointer data);//改变颜色
void cleanup();//清空重新编辑
int change_ziti();//改变字体
void on_font_select_ok (GtkWidget *button,GtkFontSelectionDialog *fs);
void on_button_clicked (GtkWidget *button,gpointer userdata);
/*得到收件人内容**********************************************************************gg*/
void get_recer(char* re_name)
{
strcpy(re_name,gtk_entry_get_text(GTK_ENTRY(entry1)));
}
/*得到密送内容************************************************************************gg*/
void get_misong(char* misong_name)
{
char misong[100];
int k;
int j=0;
strcpy(misong,gtk_entry_get_text(GTK_ENTRY(entry11)));
for(k=0;misong[k]!='\0';k++)
{
if(misong[k]==';')
{
misong_name[j]='*';
j++;
misong_name[j]=';';
j++;
}
else
{
misong_name[j]=misong[k];
j++;
}
}
misong_name[j]='\0';
}
/*得到信得主题************************************************************************gg*/
void get_title(char* title)
{
strcpy(title,gtk_entry_get_text(GTK_ENTRY(entry2)));
}
/*得到信主题内容**********************************************************************gg*/
void get_text()
{
GtkTextBuffer *pTextBuffer ;
GtkTextIter iterStart;
GtkTextIter iterEnd;
pTextBuffer=gtk_text_view_get_buffer(view);
gtk_text_buffer_get_start_iter(pTextBuffer,&iterStart);
gtk_text_buffer_get_end_iter(pTextBuffer,&iterEnd);
text=(gchar *)gtk_text_buffer_get_text(pTextBuffer,&iterStart,&iterEnd,FALSE);
printf("aaaaaaa\n");
printf("%s\n",text);
}
/*得到本地时间***********************************************************************gg*/
void get_time(char* time)
{
time=" ";
}
/*发送写信页面发送所有内容********************************************************************gggggggggggggggggggggg*/
int xiexin_cbd()
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
char buf[1024];
memset(buf,0,sizeof(buf));
/*传入数据库的各个字符串的拼接*********************************************************/
char str[1024];//str表示拼接后的字符串
memset(str,' ',sizeof(str));
char time[10]="fdf";
char re_name[100];
char misong_name[100];
char title[100];
//char text[200];
int count=0;
int k,j;
int status=2;
get_recer(re_name);
get_misong(misong_name);
get_title(title);
get_text();
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("%s",text);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
// get_time(time);
//printf("str0:%s\n",str);
str[0]='s';
str[1]=(char)status;
str[2]=(char)strlen(name);//放发件人
printf("name:%s\nstrlen(name):%d\n",name,strlen(name));
//printf("char c:%c\n",str[2]);
printf("sender:%s\n",name);
printf("sender:");
for(k=0;name[k]!='\0';k++)
{
str[k+3]=name[k];
printf("str[k+3]:%c",str[k+3]);
}
printf("\n");
//printf("str:%s\n",str);
str[15]=(char)strlen(time);//放本地时间
for(k=0;time[k]!='\0';k++)
{
str[k+16]=time[k];
}
//printf("str:%s\n",str);
str[42]=(char)strlen(title);//放标题
for(k=0;title[k]!='\0';k++)
{
str[k+43]=title[k];
}
printf("content:%s\n",text);
printf("content:");
str[123]=(char)strlen(text);//放信的内容
for(k=0;text[k]!='\0';k++)
{
str[k+124]=text[k];
printf("str[k+124]:%c",str[k+124]);
}
printf("\n");
printf("str:%s\n",str);
strcat(re_name,misong_name);
for(k=0;re_name[k]!='\0';k++)
{
if(re_name[k]==';')count++;
}
str[624]=(char)count;//收件人和密送人的总个数
for(k=0;re_name[k]!='\0';k++)
{
str[k+625]=re_name[k];
}
printf("str:%s\n",str);
printf("%d",count);
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
/*传入数据库的各个字符串的拼接结束*********************************************************/
int socket_descriptor;
int temp_sock_descriptor;
int size_of_addr;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent * server_host_name;
bzero(&pin, sizeof(pin));
pin.sin_family=AF_INET;
pin.sin_addr.s_addr = inet_addr("127.0.0.1");
pin.sin_port=htons(port);
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if((socket_descriptor=socket(AF_INET, SOCK_STREAM, 0))==-1)
{
perror("error opening socket\n");
exit(1);
}
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(connect(socket_descriptor, (void*)&pin, sizeof(pin))==-1)
{
perror("can not connecting to server\n");
exit(1);
}
if(send(socket_descriptor, str, strlen(str), 0)==-1)
{
perror("can not send message\n");
exit(1);
}
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memset(buf, 0, 1024);
if(recv(socket_descriptor, buf, sizeof(buf), 0)<0)//==-1)
{
perror("can not receive response\n");
exit(1);
}
else{
printf("message received from server:%s\n",buf);
}
close(socket_descriptor);
return 0;
}
/*草稿箱************************************************************************************/
int caogao_cbd()
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
char buf[1024];
memset(buf,0,sizeof(buf));
/*传入数据库的各个字符串的拼接*********************************************************/
char str[1024];//str表示拼接后的字符串
memset(str,' ',sizeof(str));
char time[10]="fdf";
char re_name[100];
char misong_name[100];
char title[100];
//char text[200];
int count=0;
int k,j;
get_recer(re_name);
get_misong(misong_name);
get_title(title);
get_text();
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("%s",text);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
// get_time(time);
//printf("str0:%s\n",str);
str[0]='s';
str[1]='1';//赋值1表示在草稿箱
str[2]=(char)strlen(name);//放发件人
//printf("char c:%c\n",str[2]);
for(k=0;name[k]!='\0';k++)
{
str[k+3]=name[k];
}
//printf("str:%s\n",str);
str[15]=(char)strlen(time);//放本地时间
for(k=0;time[k]!='\0';k++)
{
str[k+16]=time[k];
}
//printf("str:%s\n",str);
str[42]=(char)strlen(title);//放标题
for(k=0;title[k]!='\0';k++)
{
str[k+43]=title[k];
}
printf("str:%s\n",text);
str[123]=(char)strlen(text);//放信的内容
for(k=0;text[k]!='\0';k++)
{
str[k+124]=text[k];
}
printf("str:%s\n",str);
strcat(re_name,misong_name);
for(k=0;re_name[k]!='\0';k++)
{
if(re_name[k]==';')count++;
}
str[624]=(char)count;//收件人和密送人的总个数
for(k=0;re_name[k]!='\0';k++)
{
str[k+625]=re_name[k];
}
printf("str:%s\n",str);
printf("%d",count);
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
/*传入数据库的各个字符串的拼接结束*********************************************************/
/*if(argc < 2)
{
printf("send default string.\n");
}
else
{
str=argv[1];
}
if((server_host_name=gethostbyname(host_name))==0)
{
herror("can not resoving localhost\n");
exit(1);
}*/
int socket_descriptor;
int temp_sock_descriptor;
int size_of_addr;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent * server_host_name;
bzero(&pin, sizeof(pin));
pin.sin_family=AF_INET;
pin.sin_addr.s_addr = inet_addr("127.0.0.1");
pin.sin_port=htons(port);
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if((socket_descriptor=socket(AF_INET, SOCK_STREAM, 0))==-1)
{
perror("error opening socket\n");
exit(1);
}
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(connect(socket_descriptor, (void*)&pin, sizeof(pin))==-1)
{
perror("can not connecting to server\n");
exit(1);
}
if(send(socket_descriptor, str, strlen(str), 0)==-1)
{
perror("can not send message\n");
exit(1);
}
//printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memset(buf, 0, 1024);
if(recv(socket_descriptor, buf, sizeof(buf), 0)<0)//==-1)
{
perror("can not receive response\n");
exit(1);
}
else{
printf("message received from server:%s\n",buf);
}
close(socket_descriptor);
return 0;
}
/*改变颜色*****************************************************************************/
int change_color ( )
{
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *button;
color.red = 0;
color.blue = 65535;
color.green = 0;
window_color = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window_color),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
gtk_window_set_title(GTK_WINDOW(window_color),"选择颜色");
gtk_window_set_position(GTK_WINDOW(window_color),GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window_color),20);
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window_color),vbox);
frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(frame),GTK_SHADOW_IN);
gtk_box_pack_start(GTK_BOX(vbox),frame,TRUE,TRUE,5);
drawarea = gtk_drawing_area_new();
gtk_widget_set_size_request(drawarea,200,200);//kongjian daxiao
gtk_widget_modify_bg (drawarea, GTK_STATE_NORMAL,&color);//kongjian beijing
gtk_container_add(GTK_CONTAINER(frame),drawarea);
button = gtk_button_new_from_stock(GTK_STOCK_SELECT_COLOR);
gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_ok_clicked),NULL);
gtk_widget_show_all(window_color);
gtk_main();
return FALSE;
}
void on_ok_clicked (GtkButton* button, gpointer data)
{
GtkWidget *dialog;
GtkColorSelection *colorsel;
gint response;
dialog = gtk_color_selection_dialog_new("选择颜色");
gtk_window_set_transient_for (GTK_WINDOW (dialog),GTK_WINDOW (window_color));//注意:转换颜色选择对话框的颜色选择子控件
colorsel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dialog)->colorsel);
gtk_color_selection_set_has_opacity_control(colorsel,TRUE);
gtk_color_selection_set_has_palette(colorsel,TRUE);
gtk_signal_connect(GTK_OBJECT(dialog),"destroy",GTK_SIGNAL_FUNC(gtk_widget_destroy),&dialog);
gtk_color_selection_set_previous_color (colorsel, &color);
gtk_color_selection_set_current_color (colorsel, &color);
response = gtk_dialog_run(GTK_DIALOG(dialog));
//注意:此处需要用戶判断对话框的运行结果,如果是确定按钮则改变颜色
if(response == GTK_RESPONSE_OK)
{
gtk_color_selection_get_current_color (colorsel, &color);
gtk_widget_modify_bg (drawarea, GTK_STATE_NORMAL, &color);
GtkTextBuffer *pTextBuffer ;
GtkTextIter iterStart;
GtkTextIter iterEnd;
pTextBuffer=gtk_text_view_get_buffer(view);
gtk_text_buffer_get_start_iter(pTextBuffer,&iterStart);
gtk_text_buffer_get_end_iter(pTextBuffer,&iterEnd);
gtk_widget_modify_text(GTK_WIDGET(view),GTK_STATE_NORMAL,&color);//改变编辑框内文本颜色
}
gtk_widget_destroy(dialog);
}
/*****************************************************************************/
/*改变字体******************************************************************************/
void on_font_select_ok (GtkWidget *button,GtkFontSelectionDialog *fs)
{
PangoFontDescription *font_desc;///
GtkWidget *dialog;
gchar message[1024];
gchar* ziti = gtk_font_selection_dialog_get_font_name(fs);
font_desc = pango_font_description_from_string(ziti);
sprintf(message,"你选择的字体是:%s \n",ziti);
dialog = gtk_message_dialog_new(NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
message,
NULL);
gtk_widget_destroy(GTK_WIDGET(fs));
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
GtkTextBuffer *pTextBuffer ;
GtkTextIter iterStart;
GtkTextIter iterEnd;
pTextBuffer=gtk_text_view_get_buffer(view);
gtk_text_buffer_get_start_iter(pTextBuffer,&iterStart);
gtk_text_buffer_get_end_iter(pTextBuffer,&iterEnd);
gtk_widget_modify_font(GTK_WIDGET(view), font_desc);/*/
g_free(ziti);
}
void on_button_clicked (GtkWidget *button,gpointer userdata)
{
GtkWidget* dialog;
dialog = gtk_font_selection_dialog_new("请选择一种字体:");
gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER);
gtk_signal_connect(GTK_OBJECT(dialog),"destroy",GTK_SIGNAL_FUNC(gtk_widget_destroy),&dialog);
gtk_signal_connect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(dialog)->ok_button),"clicked",
GTK_SIGNAL_FUNC(on_font_select_ok),GTK_FONT_SELECTION_DIALOG(dialog));
gtk_signal_connect_object(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(dialog)->cancel_button),"clicked",GTK_SIGNAL_FUNC(gtk_widget_destroy),GTK_OBJECT(dialog));
gtk_widget_show(dialog);
}
int change_ziti ( )
{
GtkWidget *window;
GtkWidget *button;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
gtk_window_set_title(GTK_WINDOW(window),"字体选择功能实现");
//gtk_window_set_default_size(GTK_WINDOW(window),500,100);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window),40);
button = gtk_button_new_with_label("按下此按钮来选择字体");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),NULL);
gtk_container_add(GTK_CONTAINER(window),button);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main();
return FALSE;
}
/*****************************************************************************************/
/*清除重新编辑****************************************************************************/
void cleanup()//GtkWidget *widget, gpointer view1)
{
char *text;/传递到数据库时使用
GtkTextBuffer *pTextBuffer ;
GtkTextIter iterStart;
GtkTextIter iterEnd;
pTextBuffer=gtk_text_view_get_buffer(view);
gtk_text_buffer_get_start_iter(pTextBuffer,&iterStart);
gtk_text_buffer_get_end_iter(pTextBuffer,&iterEnd);
/*text=(gchar *)gtk_text_buffer_get_text(pTextBuffer,&iterStart,&iterEnd,FALSE);
printf("%s\n",text);*/
gtk_text_buffer_set_text(pTextBuffer,"\0",-1);
}
/*发送,存草稿,关闭的横盒******************************************************************/
GtkWidget *make_box1( gboolean homogeneous,
gint spacing,
gboolean expand,
gboolean fill,
guint padding )
{
GtkWidget *box;
GtkWidget *button;
char padstr[80];
/* 以合适的 homogeneous 和 spacing 设置创建一个新的横向盒 */
box = gtk_hbox_new (homogeneous, spacing);
/* 以合适的设置创建一系列的按钮 */
button = gtk_button_new_with_label ("发送");
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
gtk_widget_show (button);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(xiexin_cbd),NULL);
button = gtk_button_new_with_label ("存草稿");
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
gtk_widget_show (button);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(caogao_cbd),NULL);
button = gtk_button_new_with_label ("关閉");
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
gtk_widget_show (button);
return box;
}
/*notebook部分函数**********************************************************************/
void tabsborder_book( GtkButton *button,
GtkNotebook *notebook )
{
gint bval = FALSE;
if (notebook->show_border == 0)
bval = TRUE;
gtk_notebook_set_show_border (notebook, bval);
}
void delete(GtkWidget *widget,GtkWidget *event,gpointer data)
{
gtk_main_quit();
}
/*工具栏的函数******************************************************************/
static void onDestroy( GtkWidget *widget, gpointer user_data)
{
gtk_main_quit();
}
// tianjia kucun ID gongju anniu
GtkWidget * addStockToolItem(GtkWidget *toolbar, const gchar *stockid, const gchar *tooltip)
{
GtkToolItem *toolitem;
toolitem = gtk_tool_button_new_from_stock(stockid);
gtk_tool_item_set_tooltip_text(toolitem,tooltip);
//jiang gong ju xiang cha ru gongju lan wei bu
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),toolitem,-1);
gtk_widget_show(GTK_WIDGET(toolitem));
return GTK_WIDGET(toolitem);
}
//tian jia fen ge gong ju xiang
GtkWidget *addSeparatorToolItem(GtkWidget* toolbar)
{
GtkToolItem *toolitem;
//chuang jian fen ge gong ju xiang
toolitem = gtk_separator_tool_item_new();
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),toolitem,-1);
gtk_widget_show(GTK_WIDGET(toolitem));
return GTK_WIDGET(toolitem);
}
//tian jia gong ju xiang
GtkWidget * addToolItem(GtkWidget* toolbar, const gchar *pLabel, const gchar *tooltip, const gchar *pImageFile)
{
GtkWidget *image = NULL;
GtkToolItem *toolitem = NULL;
//chuang jian tu pian gou jian
image = gtk_image_new_from_file(pImageFile);
gtk_widget_show(image);
//chuang jian gong ju an niu
toolitem = gtk_tool_button_new(image,pLabel);
gtk_tool_item_set_tooltip_text(toolitem,tooltip);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),toolitem,-1);
gtk_widget_show(GTK_WIDGET(toolitem));
return GTK_WIDGET(toolitem);
}
int main(int argc, char*argv[])
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *notebook;
GtkWidget *label;
GtkWidget *checkbutton;
char bufferf[32];
char bufferl[32];
GtkWidget *vbox0;//整个窗口纵盒
GtkWidget *vbox;//整个窗口纵盒
GtkWidget *vbox2;//整个窗口纵盒
GtkWidget *vbox3;//整个窗口纵盒
GtkWidget *vbox4;//整个窗口纵盒
GtkWidget *vbox5;//整个窗口纵盒
GtkWidget *vbox6;//整个窗口纵盒
GtkWidget *vbox7;//整个窗口纵盒
GtkWidget *bbox1;//收件人横盒
GtkWidget *bbox11;//密送横盒
GtkWidget *bbox2;//主题横盒
GtkWidget *bbox3;//正文标签横盒
GtkWidget *label1;
GtkWidget *label11;//密送
GtkWidget *label2;
GtkWidget *label3;
GtkWidget *box1;//发送,存草稿,关闭的横盒
// GtkWidget *entry1;//收件人
// GtkWidget *entry11;//密送
// GtkWidget *entry2;//主题
GtkWidget *toolbar;
GtkWidget *toolitem;
GtkWidget *handlebox;
GtkWidget *frame;
GtkWidget *tvbox;/
GtkWidget *shouyehbox1;
GtkWidget *shouyehbox2;
GtkWidget *nullbox1;
GtkWidget *nullbox2;
GtkWidget *nullbox3;
GtkWidget *weidu_eventbox;
GtkWidget *shouye_label1;//标签晚上好
GtkWidget *shouye_label2;//标签昵称
GtkWidget *shouye_label3;//标签您有
GtkWidget *shouye_label4;//标签未读邮件个数
GtkWidget *shouye_label5;//封未读邮件
//char greeting1[50];
//char greeting2[50];
char *greeting1=" 晚上好,";
char *greeting2=" 您有";
char* greeting3="封未读邮件";
// strcpy(greeting1,"晚上好,");
//strcpy(greeting2,"晚上好,");
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(GTK_WIDGET(window),550,500);
gtk_window_set_resizable(GTK_WINDOW(window),FALSE);
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
gtk_window_set_title(GTK_WINDOW(window),"写信");
vbox0 = gtk_vbox_new(FALSE,0);
vbox = gtk_vbox_new(FALSE,0);
vbox2 = gtk_vbox_new(FALSE,0);
vbox3 = gtk_vbox_new(FALSE,0);
vbox4= gtk_vbox_new(FALSE,0);
vbox5 = gtk_vbox_new(FALSE,0);
vbox6 = gtk_vbox_new(FALSE,0);
vbox7 = gtk_vbox_new(FALSE,0);
box1 = make_box1(FALSE,0,FALSE,FALSE,3);
gtk_box_pack_start(GTK_BOX(vbox),box1,FALSE,FALSE,2);
bbox1 = gtk_hbox_new(FALSE,0);
bbox11 = gtk_hbox_new(FALSE,0);
bbox2 = gtk_hbox_new(FALSE,0);
bbox3 = gtk_hbox_new(FALSE,0);
label1 = gtk_label_new("收件人");
//gtk_misc_set_alignment(GTK_MISC(label1),0,0);/?????
label11 = gtk_label_new("密送 ");
label2 = gtk_label_new("主题 ");
entry1 = gtk_entry_new();
entry11 = gtk_entry_new();
entry2 = gtk_entry_new();
shouyehbox1=gtk_hbox_new(FALSE,0);
shouyehbox2=gtk_hbox_new(FALSE,0);
nullbox1=gtk_hbox_new(FALSE,10);
nullbox2=gtk_hbox_new(FALSE,10);
nullbox3=gtk_hbox_new(FALSE,10);
weidu_eventbox= gtk_event_box_new();
//sprintf(greeting1,nickname);
//strcat(nickname,greeting1);
//sprintf(greeting2,weidu_number);
view = gtk_text_view_new();//涉及全局变量和环境变量
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view),GTK_WRAP_WORD_CHAR);
/*首页盒子的排列************************************************************************************************/
PangoFontDescription *font_desc = pango_font_description_from_string ("Serif 20");
shouye_label1= gtk_label_new(greeting1);
gtk_widget_modify_font(GTK_WIDGET(shouye_label1),font_desc );
//gtk_label_set_markup(GTK_LABEL(shouye_label1),"<span foreground='black'font_desc='59'>(&greeting1) </span>");///
shouye_label2= gtk_label_new(nickname);
gtk_widget_modify_font(GTK_WIDGET(shouye_label2),font_desc );
shouye_label3= gtk_label_new(greeting2);
gtk_widget_modify_font(GTK_WIDGET(shouye_label3),font_desc );
shouye_label4= gtk_label_new(weidu_number);
gtk_widget_modify_font(GTK_WIDGET(shouye_label4),font_desc );
shouye_label5= gtk_label_new(greeting3);
gtk_widget_modify_font(GTK_WIDGET(shouye_label5),font_desc );
/****************************************************************************************************************/
gtk_container_add (GTK_CONTAINER (weidu_eventbox), shouye_label4);
gtk_widget_set_events (weidu_eventbox, GDK_BUTTON_PRESS_MASK);
//g_signal_connect(G_OBJECT (weidu_eventbox), "button_press_event",
// G_CALLBACK (), vbox0 ); ///
//gtk_widget_realize (weidu_eventbox);
gdk_window_set_cursor (weidu_eventbox->window, gdk_cursor_new (GDK_HAND1));
gtk_box_pack_start(GTK_BOX(bbox1),label1,FALSE,FALSE,0);//2
gtk_box_pack_start(GTK_BOX(bbox1),entry1,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(bbox11),label11,FALSE,FALSE,2);//3
gtk_box_pack_start(GTK_BOX(bbox11),entry11,TRUE,TRUE,2);
gtk_box_pack_start(GTK_BOX(vbox),bbox1,FALSE,FALSE,2);//2fangru zonghe
gtk_box_pack_start(GTK_BOX(vbox),bbox11,FALSE,FALSE,2);//3fangru zonghe
gtk_box_pack_start(GTK_BOX(bbox2),label2,FALSE,FALSE,2);//4
gtk_box_pack_start(GTK_BOX(bbox2),entry2,TRUE,TRUE,2);
gtk_box_pack_start(GTK_BOX(vbox),bbox2,FALSE,FALSE,2);//4fangru zonghe
/*首页盒子的排列************************************************************************************************/
gtk_box_pack_start(GTK_BOX(shouyehbox1),shouye_label1,FALSE,FALSE,2);//晚上好 昵称放横盒
gtk_box_pack_start(GTK_BOX(shouyehbox1),shouye_label2,FALSE,FALSE,2);
gtk_box_pack_start(GTK_BOX(shouyehbox2),shouye_label3,FALSE,FALSE,2);//您有n封未读邮件放横盒
gtk_box_pack_start(GTK_BOX(shouyehbox2),weidu_eventbox,FALSE,FALSE,2);//数字n放事件盒
gtk_box_pack_start(GTK_BOX(shouyehbox2),shouye_label5,FALSE,FALSE,2);
gtk_box_pack_start(GTK_BOX(vbox0),nullbox1,FALSE,FALSE,10);
gtk_box_pack_start(GTK_BOX(vbox0),nullbox2,FALSE,FALSE,10);
gtk_box_pack_start(GTK_BOX(vbox0),nullbox3,FALSE,FALSE,10);
gtk_box_pack_start(GTK_BOX(vbox0),shouyehbox1,FALSE,FALSE,2);//两个横盒放纵盒
gtk_box_pack_start(GTK_BOX(vbox0),shouyehbox2,FALSE,FALSE,2);
/***********************************************************************************************************/
toolbar = gtk_toolbar_new();
//shezhi gongju lan feng ge
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
//shezhi gongjulan tubiao daxiao
gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar),GTK_ICON_SIZE_SMALL_TOOLBAR);
handlebox = gtk_handle_box_new();
gtk_container_add(GTK_CONTAINER(handlebox),toolbar);
toolitem = addToolItem(toolbar, "font","清空重新编辑","xinjian.bmp");
g_signal_connect(toolitem,"clicked",G_CALLBACK(cleanup),NULL);
toolitem = addToolItem(toolbar, "font","附件","fujian.bmp");
// g_signal_connect(toolitem,"clicked",G_CALLBACK(onCommand),(gpointer)CMD_OPEN);
toolitem = addToolItem(toolbar, "font","字体","ziti.bmp");
g_signal_connect(toolitem,"clicked",G_CALLBACK(change_ziti),NULL);
toolitem = addToolItem(toolbar, "color","顏色","yanse.bmp");
g_signal_connect(toolitem,"clicked",G_CALLBACK(change_color),NULL);
toolitem = addToolItem(toolbar, "paper","信纸","xinzhi.bmp");
// g_signal_connect(toolitem,"clicked",G_CALLBACK(onCommand),(gpointer)CMD_QUIT);
// GtkWidget *vbox;//整个窗口纵盒
frame = gtk_frame_new("正文");
gtk_widget_set_size_request(frame,50,500);
tvbox = gtk_vbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),handlebox,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),frame,TRUE,TRUE,0);
gtk_container_add(GTK_CONTAINER(frame),view);
gtk_box_pack_start(GTK_BOX(vbox),tvbox,FALSE,FALSE,2);// gongjulan fang zonghe999999
gtk_box_pack_start(GTK_BOX(vbox),bbox3,TRUE,TRUE,2);//zhengwen henghe fang zonghe
//gtk_container_add(GTK_CONTAINER(window),vbox);
//gtk_widget_show_all(vbox);
//初始化GTK
gtk_init(&argc,&argv);
//创建最上层主窗口
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//连接信号"delete_event",使得窗口关闭时发生
g_signal_connect(G_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(delete),NULL);
//设置窗口标题
gtk_window_set_title(GTK_WINDOW(window),"wingmail");
//设定窗口的默认宽高
gtk_window_set_default_size(GTK_WINDOW(window),800,400);
//设定窗口的位置,让窗口总在最前端
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
//设定容器边框的宽度
gtk_container_set_border_width(GTK_CONTAINER(window),15);
notebook = gtk_notebook_new();
gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook),GTK_POS_LEFT);
gtk_container_add(GTK_CONTAINER(window),notebook);
gtk_widget_show(notebook);
sprintf(bufferl,"已删除");
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox7,label);
sprintf(bufferl,"已发送");
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox6,label);
sprintf(bufferl,"草稿箱(%d)",i);
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox5,label);
sprintf(bufferl,"收件箱(%d)",i);
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox4,label);
sprintf(bufferl,"通讯录");
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox3,label);
sprintf(bufferl,"收信");
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox2,label);
sprintf(bufferl,"写信");
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox,label);
sprintf(bufferl,"首页");
label = gtk_label_new(bufferl);
gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook),vbox0,label);
gtk_notebook_set_page(GTK_NOTEBOOK(notebook),0);
gtk_widget_show(notebook);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include "sqlite3.h"
char s_callback[1024] = {0};
int callback_flag=0;
int legal_user_flag=1;
int callback(void* p ,int nCount,char** pValue,char** pName);
int port=8888;
int main()
{
struct sockaddr_in sin;
struct sockaddr_in pin;
int sock_descriptor;
int temp_sock_descriptor;
int size_of_addr;
char buf[1024]={0};
int i, length;
sock_descriptor=socket(AF_INET, SOCK_STREAM, 0);
if(sock_descriptor == -1)
{
perror("socket error!");
exit(1);
}
bzero(&sin, sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port=htons(port);
if(bind(sock_descriptor, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("bind error");
exit(1);
}
if(listen(sock_descriptor, 20) == -1)
{
perror("listen1 error");
exit(1);
}
printf("waiting for accepting!\n");
while(1)
{
temp_sock_descriptor = accept(sock_descriptor, (struct sockaddr*)&pin, &size_of_addr);
if(temp_sock_descriptor == -1)
{
perror("accept error!");
exit(1);
}
else{
printf("accept client request\n");
}
memset(buf, ' ', sizeof(buf));
if(recv(temp_sock_descriptor, buf, sizeof(buf), 0) == -1)
{
perror("recv()!");
exit(1);
}
else{
/**********************************打开mail数据库*********************************/
printf("%c,%c,%c,%c\n",buf[3],buf[16],buf[33],buf[54]);
printf("message received from client: %s\n", buf);
sqlite3 *db = NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open ("mail.db", &db);
if (rc){
fprintf (stderr, "Can't open database: %s\n", sqlite3_errmsg (db));
sqlite3_close (db);
exit (EXIT_FAILURE);
}
else{
printf ("You have opened a sqlite3 database named mail.db successfully!\n");
}
char sql[600]={0};
/**********************************打开mail数据库*********************************/
/**插入user表的内容**/
char username[13];
char password[17];
char nickname[21];
char telnum[12];
int user_num=0;
char* new_usr_check_msg='\0';
/**插入user表的内容**/
/**插入send表的内容**/
int status_sender=0;
char sender[13];
char date_time[27];
char title[81];
char content[501];
/**插入send表的内容**/
/**插入receive表的内容**/
char receiver[13]={'\0'};
int status_recer=0;
int secret=0;
/**插入receive表的内容**/
/**插入contact表中的内容**/
char usrname[13];
char contactor[13];
/**插入contact表中的内容**/
int j=0,k=0;
int recer_st=0;
int tmp_recer_st;
char tmp_sender[13];
char tmp_date_time[27];
switch (buf[0]){//确定要进行数据操作的表
/**********************************************************向user表插入数据**********************************************************/
case 'u':/*向user表插入数据*/
{
memset(username,0,sizeof(username));
memset(password, 0, sizeof(password));
memset(nickname, 0, sizeof(nickname));
memset(telnum, 0, sizeof(telnum));
printf("in case1");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memcpy(username,buf+3,(int)buf[2]);
printf("username:%s\n",username);
printf("password_c:");
for(i=16;i<(int)buf[15]+16;i++){
printf("%c",buf[i]);
}
printf("\n");
memcpy(password,buf+16,(int)buf[15]);
printf("password:%s\n",password);
if(buf[1]=='1'){//检查用户登录信息是否正确
callback_flag=0;
sprintf(sql,"SELECT usrname,password FROM user WHERE usrname='%s' AND password='%s';",username,password);
sqlite3_exec (db, sql, callback, 0, &zErrMsg);
memset(sql,'\0',sizeof(sql));
if(callback_flag==1){
new_usr_check_msg="login_success";
printf("login_success\n");
legal_user_flag=1;
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)==-1)
{
perror("can not send message\n");
exit(1);
}
}
if(callback_flag==0){
new_usr_check_msg="login_error";
printf("login_error\n");
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)==-1)
{
printf("send()\n");
perror("can not send message\n");
exit(1);
}
}
}
if(buf[1]=='0'){//检验新注册用户是否重名
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
sprintf(sql, "SELECT usrname FROM user WHERE usrname = '%s'", username);
memset(s_callback,0,sizeof(s_callback));
sqlite3_exec(db,sql,callback,0,&zErrMsg);
if(strcmp(s_callback,username)==0){
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
new_usr_check_msg="usrname_repeat";
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)==-1)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("send success!\n");
}
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memcpy(nickname,buf+33,(int)buf[32]);
printf("nickname:%s\n",nickname);
memcpy(telnum,buf+54,11);
printf("telnum:%s\n",telnum);
sprintf(sql,"INSERT INTO user(ID_U,usrname,password,nickname,telnum) VALUES(NULL,'%s','%s','%s','%s');",username,password,nickname,telnum);
printf("sql:%s\n",sql);
if(sqlite3_exec(db, sql, 0, 0, &zErrMsg)==0){
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
user_num++;
printf("usernum:%d\n",user_num);
memset(sql,'\0',sizeof(sql));
} new_usr_check_msg="insert_newuser_success";//向user表插入新用户成功
printf("new_usr_check_msg:%s\n",new_usr_check_msg);
printf("sock_descriptor:%d",sock_descriptor);
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)<0)//==-1)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("send success!\n");
}
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memset(s_callback,'\0',sizeof(s_callback));
memset(sql,'\0',sizeof(sql));
}
memset(buf, 0, 1024);
memset(username,0,sizeof(username));
memset(password, 0, sizeof(password));
memset(nickname, 0, sizeof(nickname));
memset(telnum, 0, sizeof(telnum));
new_usr_check_msg="0";
break;
}
/**********************************************************向user表插入数据**********************************************************/
/**********************************************************向send表插入数据**********************************************************/
case 's':
{
printf("in case2");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(legal_user_flag==1){
printf("legal_user_flag:%d\n",legal_user_flag);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
status_sender=(int)buf[1];
printf("status_sender:%d\n",status_sender);
memset(sender,0,sizeof(sender));
memcpy(sender,buf+3,(int)buf[2]);
printf("sender:%s\n(int)buf[2]:%d\n",sender,(int)buf[2]);
memset(date_time,0,sizeof(date_time));
memcpy(date_time,buf+16,(int)buf[15]);
printf("date_time:%s\n",date_time);
memset(title,0,sizeof(title));
memcpy(title,buf+43,(int)buf[42]);
printf("title:%s\n",title);
memset(content,0,sizeof(content));
memcpy(content,buf+124,(int)buf[123]);
printf("content:%s\n(int)buf[123]:%d\n",content,(int)buf[123]);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
sprintf(sql,"INSERT INTO send(ID_M,status_sender,sender,date_time,title,content) VALUES(NULL,%d,'%s','%s','%s','%s');",status_sender,sender,date_time,title,content);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(sqlite3_exec(db, sql, 0, 0, &zErrMsg)<0){
perror("insert send error!\n");
memset(sql,'\0',sizeof(sql));
}
else{
printf("insert into send success\n");
}
memset(sql,'\0',sizeof(sql));
if(status_sender==2||status_sender==5||status_sender==8){//信件在发信人发件箱中时,可以向receive表中插入数据
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("(int)buf[624]:%d\n",(int)buf[624]);
recer_st=625;
tmp_recer_st=625;
for(i=0;i<(int)buf[624];i++){
j=0;secret=0;
recer_st=tmp_recer_st+1;
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memset(receiver,0,sizeof(receiver));
printf("k:%d\nbuf[k]:%c",recer_st,buf[recer_st]);
for(k=recer_st;buf[k]!=';';k++){
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("k:%d\nbuf[k]:%c\n",k,buf[k]);
if(buf[k]=='*'){
secret=1;
}
else{
receiver[j]=buf[k];
j++;
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("tmp_recer_st:%d\n",tmp_recer_st);
tmp_recer_st++;
}
printf("tmp_recer_st:%d\n",tmp_recer_st);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
sprintf(sql,"INSERT INTO receive(sender,receiver,date_time,status_recer,secret) VALUES('%s','%s','%s',%d,%d);",sender,receiver,date_time,status_recer,secret);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
sprintf(sql,"INSERT INTO contact(usrname,contactor) VALUES('%s','%s');",sender,receiver);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
memset(receiver, 0, sizeof(receiver));
}
}
memset(buf, 0, 1024);
memset(sender,0,sizeof(sender));
memset(date_time,0,sizeof(date_time));
memset(title, 0, sizeof(title));
memset(content, 0, sizeof(content));
memset(receiver, 0, sizeof(receiver));
}
break;
}
/**********************************************************向send表插入数据**********************************************************/
/*****************************************收件箱标为已读、标为未读、删除已读、删除未读、彻底删除********************************************/
case 'r':
{
printf("in case3");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(legal_user_flag==1){
if(buf[1]=='9'){//彻底删除
memcpy(tmp_sender,buf+2,(int)buf[2]);
memcpy(tmp_date_time,buf+15,(int)buf[15]);
/*sqlite3_exec(db,"SELECT sender FROM receiver WHERE status_recer=4 OR status_recer=7;",callback,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
strcpy(tmp_sender,s_callback);
memset(s_callback,'\0',sizeof(s_callback));
sqlite3_exec(db,"SELECT date_time FROM receiver WHERE status_recer=4 OR status_recer=7;",callback,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
strcpy(tmp_date_time,s_callback);
memset(s_callback,'\0',sizeof(s_callback));*/
sprintf(sql,"DELETE FROM receive WHERE sender='%s' AND date_time='%s' AND status_recer=4 OR status_recer=7;",tmp_sender,tmp_date_time);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
sprintf(sql,"DELETE FROM send WHERE sender='%s' AND date_time='%s';",tmp_sender,tmp_date_time);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
}
memcpy(sender,buf+2,(int)buf[2]);
memcpy(date_time,buf+15,(int)buf[15]);
sprintf(sql,"UPDATE receive SET status_receiver='%d' WHERE sender='%s' AND date_time='%s';",(int)buf[1],sender,date_time);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
memset(buf, 0, 1024);
memset(sender,0,sizeof(sender));
status_recer=0;
}
break;
}
/*****************************************收件箱标为已读、标为未读、删除已读、删除未读、彻底删除********************************************/
/*****************************************************向contact表中插入新数据********************************************************/
case 'c':/*向contact表中插入新数据*/
{
printf("in case4");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memcpy(usrname,buf+2,buf[2]);
memcpy(contactor,buf+15,buf[15]);
if(buf[1]=='1'){
sprintf(sql,"INSERT INTO contact(usrname,contactor) VALUES(%s,%s);",usrname,contactor);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
}
if(buf[1]=='2'){
sprintf(sql,"UPDATE contact SET usrname='%s',contactor='%s';",usrname,contactor);
sqlite3_exec(db,sql,0,0,&zErrMsg);
memset(sql,'\0',sizeof(sql));
}
if(buf[1]=='3'){
sprintf(sql,"DELETE FROM contact WHERE usrname='%s',contactor='%s';",usrname,contactor);
}
memset(buf, 0, 1024);
memset(usrname,0,sizeof(usrname));
memset(usrname,0,sizeof(usrname));
break;
}
/*****************************************************向contact表中插入新数据****************************************************/
/******************************************************向客户端发"草稿箱"内容******************************************************/
/*case '2':
{
printf("in case2");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(buf[2]=='0'){
sqlite3_exec(db,"SELECT * FROM sender WHERE sender_status=1;")
}
sqlite3_exec(db,sql,callback,0,&zErrMsg);
break;
}*/
/******************************************************向客户端发"草稿箱"内容******************************************************/
}
}
memset(buf, 0, 1024);
/* length = strlen(buf);
for(i = 0 ; i < length; i++)
{
buf[i] = toupper(buf[i]);`
}
if(send(temp_sock_descriptor, buf, length, 0) == -1)
{
perror("send()!");
exit(1);
}
*/
close(temp_sock_descriptor);
}
}
int callback(void* p ,int nCount,char** pValue,char** pName)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
int i = 0;
callback_flag=1;
for(i=0;i<nCount;i++)
{
strcat(s_callback, pValue[i]);
}
printf("s_callback:%s\n", s_callback);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include "sqlite3.h"
char s_callback1[1024] = {0};
char s_callback2[100][1024];
int record_num=0;
memset(s_callback2,' ',sizeof(s_callback2));
int callback1_flag=0;
int legal_user_flag=0;
int port=7878;
int callback1(void* p ,int nCount,char** pValue,char** pName);
int callback2(void* p ,int nCount,char** pValue,char** pName);
int main()
{
struct sockaddr_in sin;
struct sockaddr_in pin;
int sock_descriptor;
int temp_sock_descriptor;
int size_of_addr;
char buf[1024]={0};
int i, length;
sock_descriptor=socket(AF_INET, SOCK_STREAM, 0);
if(sock_descriptor == -1)
{
perror("socket error!");
exit(1);
}
bzero(&sin, sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port=htons(port);
if(bind(sock_descriptor, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("bind error");
exit(1);
}
if(listen(sock_descriptor, 20) == -1)
{
perror("listen1 error");
exit(1);
}
printf("waiting for accepting!\n");
while(1)
{
temp_sock_descriptor = accept(sock_descriptor, (struct sockaddr*)&pin, &size_of_addr);
if(temp_sock_descriptor == -1)
{
perror("accept error!");
exit(1);
}
else{
printf("accept client request\n");
}
memset(buf, ' ', sizeof(buf));
if(recv(temp_sock_descriptor, buf, sizeof(buf), 0) == -1)
{
perror("recv()!");
exit(1);
}
else{
/**********************************打开mail数据库*********************************/
printf("message received from client: %s\n", buf);
sqlite3 *db = NULL;
char *ErrMsg = 0;
int rc;
rc = sqlite3_open ("mail.db", &db);
if (rc){
fprintf (stderr, "Can't open database: %s\n", sqlite3_errmsg (db));
sqlite3_close (db);
exit (EXIT_FAILURE);
}
else{
printf ("You have opened a sqlite3 database named mail.db successfully!\n");
}
char sql[600]={0};
/**********************************打开mail数据库*********************************/
/**插入user表的内容**/
char username[13];
char password[17];
char nickname[21];
char telnum[12];
int user_num=0;
char* new_usr_check_msg='\0';
/**插入user表的内容**/
/**插入send表的内容**/
int status_sender=0;
char sender[13];
char date_time[27];
char title[81];
char content[501];
/**插入send表的内容**/
/**插入receive表的内容**/
char receiver[13]={'\0'};
int status_recer=0;
int secret=0;
/**插入receive表的内容**/
/**插入contact表中的内容**/
char usrname[13];
char contactor[13];
/**插入contact表中的内容**/
int j=0,k=0,st=0;
char tmp_sender[13];
char tmp_date_time[27];
int col_num=0;
int line_num=0;
char line_num_buf[10];
memset(line_num_buf,' ',sizeof(line_num_buf));
switch (buf[0]){//确定要进行数据操作的表
/**********************************************************向user表插入数据**********************************************************/
case 'u':/*向user表插入数据*/
{
memset(username,0,sizeof(username));
memset(password, 0, sizeof(password));
memset(nickname, 0, sizeof(nickname));
memset(telnum, 0, sizeof(telnum));
printf("in case1");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memcpy(username,buf+3,(int)buf[2]);
printf("username:%s\n",username);
printf("password_c:");
for(i=16;i<(int)buf[15]+16;i++){
printf("%c",buf[i]);
}
printf("\n");
memcpy(password,buf+16,(int)buf[15]);
printf("password:%s\n",password);
if(buf[1]=='1'){//检查用户登录信息是否正确
callback1_flag=0;
sprintf(sql,"SELECT usrname,password FROM user WHERE usrname='%s' AND password='%s';",username,password);
sqlite3_exec (db, sql, callback1, 0, &ErrMsg);
memset(sql,'\0',sizeof(sql));
if(callback1_flag==1){
new_usr_check_msg="login_success";
printf("login_success\n");
legal_user_flag=1;
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)==-1)
{
perror("can not send message\n");
exit(1);
}
}
if(callback1_flag==0){
new_usr_check_msg="login_error";
printf("login_error\n");
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)==-1)
{
printf("send()\n");
perror("can not send message\n");
exit(1);
}
}
}
if(buf[1]=='0'){//检验新注册用户是否重名
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
sprintf(sql, "SELECT usrname FROM user WHERE usrname = '%s'", username);
memset(s_callback1,0,sizeof(s_callback1));
sqlite3_exec(db,sql,callback1,0,&ErrMsg);
if(strcmp(s_callback1,username)==0){
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
new_usr_check_msg="usrname_repeat";
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)==-1)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("send success!\n");
}
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memcpy(nickname,buf+33,(int)buf[32]);
printf("nickname:%s\n",nickname);
memcpy(telnum,buf+54,11);
printf("telnum:%s\n",telnum);
sprintf(sql,"INSERT INTO user(ID_U,usrname,password,nickname,telnum) VALUES(NULL,'%s','%s','%s','%s');",username,password,nickname,telnum);
printf("sql:%s\n",sql);
if(sqlite3_exec(db, sql, 0, 0, &ErrMsg)==0){
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
user_num++;
printf("usernum:%d\n",user_num);
memset(sql,'\0',sizeof(sql));
} new_usr_check_msg="insert_newuser_success";//向user表插入新用户成功
printf("new_usr_check_msg:%s\n",new_usr_check_msg);
printf("sock_descriptor:%d",sock_descriptor);
if(send(temp_sock_descriptor, new_usr_check_msg, strlen(new_usr_check_msg), 0)<0)//==-1)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("send success!\n");
}
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memset(s_callback1,'\0',sizeof(s_callback1));
memset(sql,'\0',sizeof(sql));
}
memset(buf, 0, 1024);
memset(username,0,sizeof(username));
memset(password, 0, sizeof(password));
memset(nickname, 0, sizeof(nickname));
memset(telnum, 0, sizeof(telnum));
new_usr_check_msg="0";
break;
}
/**********************************************************向user表插入数据**********************************************************/
/**********************************************************向send表插入数据**********************************************************/
case 's':
{
printf("in case2");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(legal_user_flag==1){
memcpy(status_sender,buf+1,1);
memcpy(sender,buf+2,(int)buf[2]);
memcpy(date_time,buf+15,(int)buf[15]);
memcpy(title,buf+42,(int)buf[42]);
memcpy(content,buf+123,(int)buf[123]);
sprintf(sql,"INSERT INTO send(ID_M,status_sender,sender,date_time,title,content) VALUES(NULL,%d,%s,%s,%s,%s);",status_sender,sender,date_time,title,content);
if(sqlite3_exec (db, sql, 0, 0, &ErrMsg)<0){
perror("insert send error!\n");
memset(sql,'\0',sizeof(sql));
}
memset(sql,'\0',sizeof(sql));
if(status_sender==2||status_sender==5||status_sender==8){//信件在发信人发件箱中时,可以向receive表中插入数据
for(i=0;i<(int)buf[624];i++){
j=0;secret=0;st=625;
for(k=st;buf[k]!=';';k++){
if(buf[k]!='*'){
receiver[j]=buf[k];
j++;
}
else{
secret=1;
}
}
st=k+1;
sprintf(sql,"INSERT INTO receive(sender,receiver,date_time,status_recer,secret) VALUES(%s,%s,%s,%d.%d);",sender,receiver,date_time,status_recer,secret);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
sprintf(sql,"INSERT INTO contact(usrname,contactor) VALUES(%s,%s);",sender,receiver);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
memset(receiver, 0, sizeof(receiver));
}
}
memset(buf, 0, 1024);
memset(sender,0,sizeof(sender));
memset(date_time,0,sizeof(date_time));
memset(title, 0, sizeof(title));
memset(content, 0, sizeof(content));
memset(receiver, 0, sizeof(receiver));
}
break;
}
/**********************************************************向send表插入数据**********************************************************/
/*****************************************收件箱标为已读、标为未读、删除已读、删除未读、彻底删除********************************************/
case 'r':
{
printf("in case3");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(legal_user_flag==1){
if(buf[1]=='9'){//彻底删除
memcpy(tmp_sender,buf+2,(int)buf[2]);
memcpy(tmp_date_time,buf+15,(int)buf[15]);
/*sqlite3_exec(db,"SELECT sender FROM receiver WHERE status_recer=4 OR status_recer=7;",callback1,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
strcpy(tmp_sender,s_callback1);
memset(s_callback1,'\0',sizeof(s_callback1));
sqlite3_exec(db,"SELECT date_time FROM receiver WHERE status_recer=4 OR status_recer=7;",callback1,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
strcpy(tmp_date_time,s_callback1);
memset(s_callback1,'\0',sizeof(s_callback1));*/
sprintf(sql,"DELETE FROM receive WHERE sender='%s' AND date_time='%s' AND status_recer=4 OR status_recer=7;",tmp_sender,tmp_date_time);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
sprintf(sql,"DELETE FROM send WHERE sender='%s' AND date_time='%s';",tmp_sender,tmp_date_time);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
}
memcpy(sender,buf+2,(int)buf[2]);
memcpy(date_time,buf+15,(int)buf[15]);
sprintf(sql,"UPDATE receive SET status_receiver='%d' WHERE sender='%s' AND date_time='%s';",(int)buf[1],sender,date_time);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
memset(buf, 0, 1024);
memset(sender,0,sizeof(sender));
status_recer=0;
}
break;
}
/*****************************************收件箱标为已读、标为未读、删除已读、删除未读、彻底删除********************************************/
/*****************************************************向contact表中插入新数据********************************************************/
case 'c':/*向contact表中插入新数据*/
{
printf("in case4");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
memcpy(usrname,buf+2,buf[2]);
memcpy(contactor,buf+15,buf[15]);
if(buf[1]=='1'){
sprintf(sql,"INSERT INTO contact(usrname,contactor) VALUES(%s,%s);",usrname,contactor);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
}
if(buf[1]=='2'){
sprintf(sql,"UPDATE contact SET usrname='%s',contactor='%s';",usrname,contactor);
sqlite3_exec(db,sql,0,0,&ErrMsg);
memset(sql,'\0',sizeof(sql));
}
if(buf[1]=='3'){
sprintf(sql,"DELETE FROM contact WHERE usrname='%s',contactor='%s';",usrname,contactor);
}
memset(buf, 0, 1024);
memset(usrname,0,sizeof(usrname));
memset(usrname,0,sizeof(usrname));
break;
}
/*****************************************************向contact表中插入新数据****************************************************/
case '2':
{
printf("in case2");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(buf[1]=='0'){//向客户端发"草稿箱"内容
memset(sender,0,sizeof(sender));//客户端传sender字段给服务器
memcpy(sender,buf+3,(int)buf[2]);
memset(sql,'\0',sizeof(sql));
sprintf(sql,"SELECT title,date_time FROM sender WHERE sender_status=1 AND sender='%s'LIMIT 1;",sender);
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
line_num=record_num/col_num;
line_num_buf[0]=(char)line_num;
if(send(temp_sock_descriptor, ine_num_buf, strlen(ine_num_buf), 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send success!\n");
}
for(i=0;i<record_num;i++){
if(send(temp_sock_descriptor,s_callback2[i],strlen(s_callback2[i]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[%d] success\n",i);
}
} record_num=0;
}
if(buf[1]=='1'){//向客户端发送"已发送"内容
memset(sender,0,sizeof(sender));
memcpy(sender,buf+3,(int)buf[2]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT title,date_time FROM send WHERE sender='%s'AND status_sender=2 OR status_sender=5 OR status_sender=8;",sender);
col_num=2;
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
line_num=record_num/col_num;
line_num_buf[0]=(char)line_num;
if(send(temp_sock_descriptor, ine_num_buf, strlen(ine_num_buf), 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send success!\n");
}
for(i=0;i<record_num;i++){
if(send(temp_sock_descriptor,s_callback2[i],strlen(s_callback2[i]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[%d] success\n",i);
}
}
record_num=0;
}
if(buf[1]=='2'){//向客户端发送一封邮件的具体内容
memset(sender,0,sizeof(sender));
memcpy(sender,buf+3,(int)buf[2]);
memset(date_time,0,sizeof(date_time));
memcpy(date_time,buf+15,(int)buf[14]);
memset(recer,0,sizeof(recer));
memcpy(recer,buf+42,(int)buf[41]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT content FROM send WHERE date_time='%s' AND sender IN (SELECT sender FROM receive WHERE sender='%s' AND date_time='%s' AND recer='%s');",date_time,sender,date_time,recer);
col_num=1;
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
if(send(temp_sock_descriptor,s_callback2[0],strlen(s_callback2[0]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[0] success\n");
}
record_num=0;
}
if(buf[1]=='3'){//向客户端发送一封草稿的内容,若有收信人写在content内
memset(sender,0,sizeof(sender));
memcpy(sender,buf+3,(int)buf[2]);
memset(date_time,0,sizeof(date_time));
memcpy(date_time,buf+15,(int)buf[14]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT content FROM send WHERE sender='%s' AND date_time='%s';",sender,date_time);
colnum=1;
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
if(send(temp_sock_descriptor,s_callback2[0],strlen(s_callback2[0]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[0] success\n");
}
record_num=0;
}
break;
}
case '3':
{
if(buf[1]=='0'){//向客户端发收信箱内容
memset(recer,0,sizeof(recer));
memcpy(recer,buf+3,(int)buf[2]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT s.sender,s.title,s.date_time FROM send s,receive r WHERE s.sender=r.sender AND s.date_time=r.date_time AND recer='%d' AND status_recer=3;",recer);
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
line_num=record_num/col_num;
line_num_buf[0]=(char)line_num;
if(send(temp_sock_descriptor, ine_num_buf, strlen(ine_num_buf), 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send success!\n");
}
for(i=0;i<record_num;i++){
if(send(temp_sock_descriptor,s_callback2[i],strlen(s_callback2[0]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[%d] success\n",i);
}
}
record_num=0;
}
if(buf[1]=='1'){//向客户端发收件箱内容
memset(rec,0,sizeof(recer));
memcpy(recer,buf+3,(int)buf[2]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT s.sender,s.title,s.date_time FROM send s,receive r WHERE s.sender=r.sender AND s.date_time=r.date_time AND recer='%d' AND status_recer=3 OR status_recer=6;",recer);
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
line_num=record_num/col_num;
line_num_buf[0]=(char)line_num;
if(send(temp_sock_descriptor, ine_num_buf, strlen(ine_num_buf), 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send success!\n");
}
for(i=0;i<record_num;i++){
if(send(temp_sock_descriptor,s_callback2[i],strlen(s_callback2[0]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[%d] success\n",i);
}
}
record_num=0;
}
if(buf[1]=='2'){//向客户端发已删除箱内容
memset(rec,0,sizeof(recer));
memcpy(recer,buf+3,(int)buf[2]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT s.sender,s.title,s.date_time FROM send s,receive r WHERE s.sender=r.sender AND s.date_time=r.date_time AND recer='%d' AND status_recer=4 OR status_recer=7;",recer);
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
line_num=record_num/col_num;
line_num_buf[0]=(char)line_num;
if(send(temp_sock_descriptor, ine_num_buf, strlen(ine_num_buf), 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send success!\n");
}
for(i=0;i<record_num;i++){
if(send(temp_sock_descriptor,s_callback2[i],strlen(s_callback2[i]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[%d] success\n",i);
}
}
record_num=0;
}
break;
}
case '4':
{
if(buf[1]=='0'){
memset(usrname,0,sizeof(usrname));
memcpy(usrname,buf[3],(int)buf[2]);
memset(sql,0,sizeof(sql));
memset(s_callback2,0,sizeof(s_callback2));
sprintf(sql,"SELECT contactor FROM contact WHERE uarname='%s';",usrname);
sqlite3_exec(db,sql,callback2,0,&ErrMsg);
line_num=record_num/col_num;
line_num_buf[0]=(char)line_num;
if(send(temp_sock_descriptor, ine_num_buf, strlen(ine_num_buf), 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send success!\n");
}
for(i=0;i<record_num;i++){
if(send(temp_sock_descriptor,s_callback2[i],strlen(s_callback2[i]),0)<0)
{
perror("write");
return 1;
}
else{
printf("send s_callback2[%d] success\n",i);
}
}
record_num=0;
}
break;
}
}
}
memset(buf, 0, 1024);
/* length = strlen(buf);
for(i = 0 ; i < length; i++)
{
buf[i] = toupper(buf[i]);`
}
if(send(temp_sock_descriptor, buf, length, 0) == -1)
{
perror("send()!");
exit(1);
}
*/
close(temp_sock_descriptor);
}
}
int callback1(void* p ,int nCount,char** pValue,char** pName)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
int i = 0;
callback1_flag=1;
for(i=0;i<nCount;i++)
{
strcat(s_callback1, pValue[i]);
}
printf("s_callback1:%s\n", s_callback1);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static int callback2(void *NotUsed, int argc, char **argv, char **azColName)
{
int i=0,k=0;
record_num=argc;
for(i=0; i<argc; i++){
strcpy(s_callback2[i],argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}