#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <mysql.h>
static GdkColor cyan = { 0, 0 , 0xffff, 0xffff };
PangoFontDescription *desc;
gint sockfd,connfd;
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t addrlen;
gchar get_buf[1024];
gboolean isconnected = FALSE;
GtkWidget *checkbutton1;
GtkWidget *checkbutton2;
gchar buf[1024];
static GtkTextBuffer *buffer;
static GtkWidget *message_entry;
gboolean do_connect(void)
{
GtkTextIter iter;
//初始化Socket,创建socket
if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Create socket failed\n");
perror("socket()");
return -1;
}
// 设置服务器端地址协议为ip4
server.sin_family=AF_INET;
// 设置端口号为1234
server.sin_port=htons(1234);
// 设置ip地址为127.0.0.1
server.sin_addr.s_addr=inet_addr("127.0.0.1");
printf("begin bind\n");
// 绑定端口号和IP地址
int bind_id=bind(sockfd,(struct sockaddr *)&server,sizeof(server));
if (bind_id==-1)
{
printf("Bind failed\n");
perror("bind()");
return -1;
}
printf("begin listen\n");
int listen_id;
// 监听端口号
if ((listen_id=listen(sockfd,1))==-1)
{
printf("listen failed\n");
perror("listen()");
return -1;
}
// 赋值客户端结构体长度
addrlen=sizeof(client);
printf("begin accept\n");
// 阻塞主进程,直到有客户端连接
connfd=accept(sockfd,(struct sockaddr *)&client,&addrlen);
printf("connfd = %d\n",connfd);
if (connfd==-1)
{
printf("accept failed\n");
perror("accept()");
return -1;
}
else
{
// 获取buffer内容,并迭代
gtk_text_buffer_get_end_iter(buffer,&iter);
// 将迭代的数据加入字符串加入buffer中
gtk_text_buffer_insert(buffer,&iter,"connect succes\n",-1);
isconnected=TRUE;
printf("connect success\n");
return TRUE;
}
}
// editable关闭按钮回调函数
void on_editable_checked(GtkWidget *widget, GdkEvent* event,gpointer data)
{
GtkWidget *text;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton1))==TRUE)
{
gtk_text_view_set_editable(GTK_TEXT_VIEW(text),FALSE);
}
else{
gtk_text_view_set_editable(GTK_TEXT_VIEW(text),TRUE);
}
}
// separated关闭按钮回调函数
void on_separated_checked(GtkWidget *widget, GdkEvent* event,gpointer data)
{
GtkWidget *text;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton2))==TRUE)
{
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text),GTK_WRAP_WORD);
}
else{
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text),GTK_WRAP_CHAR);
}
}
// 关闭按钮回调函数
void on_delete_event(GtkWidget *widget,GdkEvent* event,gpointer data)
{
// 关闭Socket连接
close(sockfd);
// 退出窗口
gtk_main_quit();
}
void on_send(GtkButton* button,gpointer data)
{
char query[256];
MYSQL *mysql;
const char* message;
GtkTextIter iter;
gint write_id;
printf("begin server send\n");
// 初始化buf
bzero(buf,1024);
// 获取输入框的值赋值
message = gtk_entry_get_text(GTK_ENTRY(message_entry));
// 将字符串和message值相连接赋值给buf
sprintf(buf,"server: %s\n",message);
printf("server send buf=%s\n",(gchar*)buf);
// 将buf写入socket连接中
write_id = write(connfd,buf,strlen(buf));
if (write_id==-1)
{
perror("write()");
}
if(!(mysql=mysql_init(NULL))){
printf("mysql_init wrong!");
mysql_close(mysql);
exit(0);
}
if(!mysql_real_connect(mysql,"localhost","root","","ChatLog",0,NULL,0)){
printf("connect wrong!");
mysql_close(mysql);
exit(0);
}
strcpy(query,"use ChatLog;");
if(mysql_query(mysql,query)){
printf("no database ChatLog");
mysql_close(mysql);
exit(0);
}
strcpy(query," create table if not exists ChatInfo(name varchar(10) not null,history varchar(500) not null);
");
if(mysql_query(mysql,query)){
printf("create_table wrong!");
mysql_close(mysql);
exit(0);
}
sprintf(query,"insert into ChatInfo values('server:',\'%s\');",message);
if(mysql_query(mysql,query)){
printf("insert error!");
mysql_close(mysql);
exit(0);
}
// 将输入框值清空
gtk_entry_set_text(GTK_ENTRY(message_entry),"");
// 将buffer内容的值,进行迭代
gtk_text_buffer_get_end_iter(buffer,&iter);
// 将迭代值与buf连接放入buffer中
gtk_text_buffer_insert(buffer,&iter,buf,-1);
}
void *tcp_read(void* a)
{
GtkTextIter iter;
gint num=1;
printf("server begin read\n");
while (num>0)
{
// 初始化get_buf
bzero(get_buf,1024);
printf("num = %d\n", num);
// 读取Socket中的值赋给get_buf
num=read(*(int*)a,get_buf,1024);
printf ("num = %d\n", num);
printf ("get_buf=%s\n",(gchar*)get_buf);
// 获取buffer值,迭代
gtk_text_buffer_get_end_iter(buffer,&iter);
// 将迭代的值与get_buf值相连接,赋给buffer
gtk_text_buffer_insert(buffer,&iter,get_buf,-1);
}
printf ("tcp_read buffer = %s\n",(gchar*)buffer);
return 0;
}
void on_history(int argc,char *argv[])
{
gchar buf1[1024];
GtkTextBuffer *buffer1;
char query[256];
MYSQL *mysql;
const char* message;
GtkTextIter iter;
int rows;
int count;
MYSQL_RES *mysql_res;
MYSQL_ROW mysql_row;
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *view;
GtkWidget *text;
GdkPixbuf *pixbuf = NULL;
GdkPixmap *pixmap = NULL;
GdkBitmap *bitmap = NULL;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"ChatLog");
gtk_container_set_border_width(GTK_CONTAINER(window),2);
g_signal_connect(G_OBJECT(window),"delete_event", G_CALLBACK(gtk_main_quit),NULL);
gtk_widget_set_events(window, GDK_SCROLL_MASK);
gtk_widget_set_app_paintable(window,TRUE);
gtk_widget_realize (window);
pixbuf = gdk_pixbuf_new_from_file ("./bgcolor.jpg",NULL);
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
gtk_widget_shape_combine_mask(window, bitmap, 0, 0);
GtkStyle *style = gtk_style_copy(window->style);
if(style->bg_pixmap[GTK_STATE_NORMAL])
g_object_unref(style->bg_pixmap[GTK_STATE_NORMAL]);
style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref(pixmap);
gtk_widget_set_style(window, style);
hbox=gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),hbox);
view=gtk_scrolled_window_new(NULL,NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(view),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
text=gtk_text_view_new();
gtk_box_pack_start(GTK_BOX(hbox),view,TRUE,TRUE,5);
gtk_container_add(GTK_CONTAINER(view),text);
buffer1=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
gtk_widget_show_all(hbox);
gtk_widget_show_all(window);
if(!(mysql=mysql_init(NULL))){
printf("mysql_init wrong!");
mysql_close(mysql);
exit(0);
}
if(!mysql_real_connect(mysql,"localhost","root","","ChatLog",0,NULL,0)){
printf("connect wrong!");
mysql_close(mysql);
exit(0);
}
strcpy(query,"use ChatLog;");
if(mysql_query(mysql,query)){
printf("no database ChatLog");
mysql_close(mysql);
exit(0);
}
strcpy(query,"select * from ChatInfo;");
if(mysql_query(mysql,query)){
printf("display wrong!");
mysql_close(mysql);
exit(0);
}
mysql_res=mysql_store_result(mysql);
rows=mysql_num_ro