Linux GTK 聊天室 MySql C语言聊天室

  • U6_288682
    了解作者
  • 32.1KB
    文件大小
  • gz
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-28 08:46
    上传日期
此资源是基于Linux 环境下的Socket通信编写的代码,里面融合了TCP/IP 通信,UDP通信,GTK,等知识,而且使用了I/O复用,在性能上有着自己的优势。这个作品是我在课程设计的时候参考一个代码做的,我在原作者的基础上做了自己的改善。也谢谢原作者对于我的帮助!如果你想学习Linux C 可以下载,希望对你有用。帮助我写在了client里面,你可以参考!
qq.tar.gz
  • mygtk.h
    34.9KB
  • qq1.ico
    9.4KB
  • qq.glade
    36.7KB
  • qq.ico
    9.4KB
  • client_qq.c
    18.7KB
  • server_qq.c
    15.9KB
  • wrap.h
    5KB
内容介绍
/*---------------xc_qq.c------------ gcc -Wall -o client_qq client_qq.c `pkg-config --cflags --libs gtk+-2.0 gthread-2.0` -export-dynamic -lpthread 第一行是运行的命令,我将图标放在了包里,你需要建立一个ico的文件夹,将图标放进去,或者更改程序里面的路径。 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <fcntl.h> #include <pthread.h> #include <gtk/gtk.h> #include <glib.h> #include <gtk/gtkmain.h> #include "wrap.h" #include "mygtk.h" #define MAXLINE 800 static char *fengefu = "+"; pthread_t ntid; static char list_name[MAXLINE][MAXLINE], receive_msg_buf_name[MAXLINE][MAXLINE]; static pthread_t tid; /*******************************/ static char msg_ip[MAXLINE],msg_port[MAXLINE]; /*********************************/ static char myname[MAXLINE], myip[MAXLINE], msg_buf[MAXLINE], myport[MAXLINE]; /**************文件传输相关变量开始*************/ static char file_from_name[MAXLINE], filename[MAXLINE], receive_ip[MAXLINE], receive_port[MAXLINE], disrecv_from_name[MAXLINE]; int new_file = -1, get_ip_statue = -1; /**************文件传输相关变量结束*************/ int group_send=-1; char group_message[MAXLINE]; int err; static int retvalue = 0, c_icon = -1; //static int ifshow=-1; char buf[MAXLINE]; int inet_pton(int af, const char *src, void *dst); static int sockfd, n; static int connect_ser(char server_ip[], int server_port); static void show_login(); void init_list_name() { int i = 0; for (i = 0; i < MAXLINE; i++) { bzero(list_name[i], MAXLINE); strcpy(list_name[i], "null"); } } void init_receive_msg_buf_name() { int i = 0; for (i = 0; i < MAXLINE; i++) { bzero(receive_msg_buf_name[i], MAXLINE); strcpy(receive_msg_buf_name[i], "null"); } } /* 列表代码开始 */ enum { COLUMN = 0, NUM_COLS }; GtkWidget *view; GtkWidget *label_myname; char count_online[100]; void on_changed(GtkWidget * widget, gpointer statusbar) { GtkTreeIter iter; GtkTreeModel *model; char *value; if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) { gtk_tree_model_get(model, &iter, COLUMN, &value, -1); gtk_statusbar_push(GTK_STATUSBAR(statusbar), gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), value), value); char win_name[MAXLINE]; sprintf(win_name, "%s+正在和+%s+聊天\n", myname, value); int i = 0; for (i = 0; i < MAXLINE; i++) { if (strcmp(receive_msg_buf_name[i], value) == 0) { return; } } if (strcmp(value, count_online) != 0 && strcmp(value, "离线好友(0)") != 0 && strcmp(value, "获取列表失败!请重新登录!") != 0) { for (i = 0; i < MAXLINE; i++) { if (strcmp(receive_msg_buf_name[i], "null")== 0) { strcpy(receive_msg_buf_name[i], value); break; } } //pthread_t show_chat_tid; //void *win_title = (void *)win_name; //类型转换 //void *sockfe = (void *)sockfd; //类型转换 //pthread_create(&ntid, NULL, thr_fn, sockfe); //pthread_create(&show_chat_tid, NULL, show_chat_window_thr, NULL); show_chat_window(win_name); } g_free(value); } } static void remove_all() { GtkTreeStore *treestore; treestore = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(view))); gtk_tree_store_clear(treestore); } static void append_item(char list_n[MAXLINE][MAXLINE]) { GtkTreeStore *treestore; GtkTreeIter toplevel, child; char myname_buf[MAXLINE]; if (strcmp(myname, "") == 0) { sprintf(myname_buf, "读取服务信息失败!请退出重新登录!\n"); } else { sprintf(myname_buf, "我的昵称:%s\n", myname); } gtk_label_set_text(GTK_LABEL(label_myname), myname_buf); remove_all(); int i = 0, count = 0; for (i = 0; i < MAXLINE; i++) { if (strcmp(list_n[i], "null") != 0 && strcmp(list_n[i], myname) != 0) count++; } sprintf(count_online, "在线好友(%d)", count); treestore = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(view))); if (strcmp(myname, "") != 0) { gtk_tree_store_append(treestore, &toplevel, NULL); gtk_tree_store_set(treestore, &toplevel, COLUMN, count_online, -1); for (i = 0; i < MAXLINE; i++) { if (strcmp(list_n[i], "null") != 0 && strcmp(list_n[i], myname) != 0) { gtk_tree_store_append(treestore, &child, &toplevel); gtk_tree_store_set(treestore, &child, COLUMN, list_n[i], -1); strcpy(list_n[i], "null"); } } gtk_tree_store_append(treestore, &toplevel, NULL); gtk_tree_store_set(treestore, &toplevel, COLUMN, "离线好友(0)", -1); } else { gtk_tree_store_append(treestore, &toplevel, NULL); gtk_tree_store_set(treestore, &toplevel, COLUMN, "获取列表失败!请重新登录!", -1); } } static GtkWidget *create_view_and_model(void) { GtkTreeViewColumn *col; GtkCellRenderer *renderer; GtkWidget *view; GtkTreeModel *model; GtkTreeStore *treestore; treestore = gtk_tree_store_new(NUM_COLS, G_TYPE_STRING); view = gtk_tree_view_new(); col = gtk_tree_view_column_new(); gtk_tree_view_column_set_title(col, "我的好友"); gtk_tree_view_append_column(GTK_TREE_VIEW(view), col); renderer = gtk_cell_renderer_text_new(); gtk_tree_view_column_pack_start(col, renderer, TRUE); gtk_tree_view_column_add_attribute(col, renderer, "text", COLUMN); model = GTK_TREE_MODEL(treestore); gtk_tree_view_set_model(GTK_TREE_VIEW(view), model); g_object_unref(model); return view; } int change_ico(GtkWidget * list_window) { change_icon(); return 1; } int get_friend_list(GtkWidget * list_window) { char get_list[] = "getlist+"; Write(sockfd, get_list, strlen(get_list)); return 1; } int show_recv_file(GtkWidget * list_window) { if (new_file == 1) { receive_file(filename, file_from_name); new_file = -1; } return 1; } int get_ip_port(GtkWidget * list_window) { if (get_ip_statue == 1) { if (send_file_as(receive_ip, receive_port) == 1) { show_info("文件发送完毕!"); } get_ip_statue = -1; } if (get_ip_statue == 2) { char disrecv_msg[MAXLINE]; sprintf(disrecv_msg, "你的好友%s拒绝接受文件", disrecv_from_name); show_info(disrecv_msg); get_ip_statue = -1; } return 1; } int show_list_window() { GtkWidget *list_window;//列表窗口 GtkTreeSelection *selection; GtkWidget *vbox; GtkWidget *statusbar; GtkWidget *g_button;//群聊 gtk_init(NULL, NULL); list_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_container_set_border_width(GTK_CONTAINER(list_window), 15); gtk_window_set_title(GTK_WINDOW(list_window), "在线列表"); gtk_widget_set_size_request(list_window, 250, 600); gtk_window_set_position(GTK_WINDOW(list_window), GTK_WIN_POS_MOUSE); gtk_window_set_icon_from_file(GTK_WINDOW(list_window), "ico/qq2.ico", NULL); gtk_window_set_skip_taskbar_hint(GTK_WINDOW(list_window), TRUE); //隐藏任务栏 char myname_buf[MAXLINE]; if (strlen(myname) == 0) { sprintf(myname_buf, "读取服务信息失败!请重新登录!\n"); } else { sprintf(myname_buf, "我的昵称:%s\n", myname); } label_myname = gtk_label_new(myname_buf); gtk_label_set_justify(GTK_LABEL(label_myname), GTK_JUSTIFY_LEFT); GtkWidget *image; image = gtk_image_new_from_file("ico/qq1.ico"); vbox = gtk_vbox_new(FALSE, 2); gtk_container_add(GTK_CONTAINER(list_window), vbox); gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 3); gtk_box_pack_start(GTK_BOX(vbox), label_myname, FALSE, FALSE, 3); view = create_view_and_model(); selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); gtk_box_pack_start(GTK_BOX(vbox), view, TRUE, TRUE, 1); statusbar = gtk_statusbar_new(); gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, TRUE, 1); g_button = gtk_button_new_with_label("群聊"); gtk_box_pack_start(GTK_BOX(vbox),g_button,FALSE,FALSE,1); g_signal_connect(G_OBJECT(g_button),"clicked",G_CALLBACK(on_group_chat),NULL); g_signal_connect(selection, "changed", G_CALLBACK(on_changed), statusbar); g_signal_connect(G_OBJECT(list_window), "destroy", G_CALLBACK(gtk_main_quit), NULL)
评论
    相关推荐