资源简介

实现多人通信,使用GTK编写界面,采用socket网络编程,和多线程实现客户端之间的通信,包含服务器和客户端代码,可以直接运行.

资源截图

代码片段和文件信息

/*
 Client.c          
 功能: 创建客户端窗口     
*/

#include “client.h“
#include 
#include 
/*声明全局变量*/
GtkWidget *window
  *rec_text*snd_text*ip_text
  *space_label
  *link_button*snd_button*quit_button
  *hbox*vbox
  *rec_scrolled*snd_scrolled;
GtkTextBuffer *rec_buffer*snd_buffer;

int issucceed=-1;// build socked with server or not
void get_ip(GtkWidget *gpointer);
void quit_win(GtkWidget *gpointer);

int main(int argcchar **argv)
{


gtk_init(&argc&argv);

window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window)“client“);/*设置主窗口标题*/
        gtk_container_set_border_width(GTK_CONTAINER(window)5);/*设置主窗口边框*/
gtk_window_set_position(GTK_WINDOW(window)GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window)400300);

space_label=gtk_label_new(“                  “);
        

/* 设置按钮 */
link_button=gtk_button_new_with_label(“连接“);
snd_button=gtk_button_new_with_label(“发送“);
quit_button=gtk_button_new_with_label(“退出“);


        /* 设置接收文本框区*/
rec_text=gtk_text_view_new();
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(rec_text)FALSE);//光标不可见

        rec_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(rec_text));
        gtk_text_view_set_editable(GTK_TEXT_VIEW(rec_text)FALSE);//不可编辑
        
        /* 设置发送文本框区*/
snd_text=gtk_text_view_new();
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(snd_text)TRUE);

        snd_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(snd_text));
        gtk_text_view_set_editable(GTK_TEXT_VIEW(snd_text)TRUE);
          
/*创建滚动窗口盒构件*/
rec_scrolled=gtk_scrolled_window_new(NULLNULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(rec_scrolled)GTK_POLICY_AUTOMATICGTK_POLICY_AUTOMATIC);
snd_scrolled=gtk_scrolled_window_new(NULLNULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(snd_scrolled)GTK_POLICY_AUTOMATICGTK_POLICY_AUTOMATIC);

/* 创建文本盒构件*/
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(rec_scrolled)rec_text);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(snd_scrolled)snd_text);

hbox=gtk_hbox_new(FALSE2);/*生成一个水平排布的盒子*/
vbox=gtk_vbox_new(FALSE2);/*生成一个垂直排布的盒子*/

/*包装所有盒构件*/

gtk_box_pack_start(GTK_BOX(hbox)link_buttonFALSEFALSE2);
gtk_box_pack_start(GTK_BOX(hbox)space_labelTRUETRUE2);
        gtk_box_pack_start(GTK_BOX(hbox)quit_buttonFALSEFALSE2);
gtk_box_pack_start(GTK_BOX(hbox)snd_buttonFALSEFALSE2);

gtk_box_pack_start(GTK_BOX(vbox)rec_scrolledTRUETRUE2);
gtk_box_pack_start(GTK_BOX(vbox)snd_scrolledTRUETRUE2);
gtk_box_pack_start(GTK_BOX(vbox)hboxFALSEFALSE2);

gtk_container_add(GTK_CONTAINER(window)vbox);/*将垂直盒构件包装到主盒中*/

/*连接回调函数*/
g_signal_connect(GTK_object(link_button)“clicked“GTK_SIGNAL_FUNC(get_ip)NULL);/* 点击“连接“回调函数get_ip */
g_signal_connect(GTK_object(snd_button)“clicked“GTK_SIGNAL_FUNC(send_text)NULL);/*点击“发送”回调函数send_text*/
g_signal_connect(GTK_object(quit_button)“clicked“GTK_SIG

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件         82  2015-07-09 12:06  chat-new\client\chatlog.db

     文件      18415  2015-07-09 12:01  chat-new\client\client

     文件       6477  2015-07-09 12:00  chat-new\client\client.c

     文件       6477  2015-07-09 12:00  chat-new\client\client.c~

     文件        456  2015-07-09 11:33  chat-new\client\client.h

     文件        456  2015-07-09 11:33  chat-new\client\client.h~

     文件       2254  2015-07-09 11:28  chat-new\client\cl_sock.c

     文件       2254  2015-07-09 11:28  chat-new\client\cl_sock.c~

     文件        142  2015-07-09 11:33  chat-new\client\makefile

     文件        142  2015-07-09 11:33  chat-new\client\makefile~

     文件         82  2015-07-09 12:06  chat-new\client1\chatlog.db

     文件      18415  2015-07-09 12:01  chat-new\client1\client

     文件         75  2015-07-09 12:06  chat-new\client2\chatlog.db

     文件      18415  2015-07-09 12:01  chat-new\client2\client

     文件         80  2015-07-09 11:40  chat-new\server\makefile

     文件         80  2015-07-09 11:40  chat-new\server\makefile~

     文件      15347  2015-07-09 11:57  chat-new\server\server

     文件       3903  2015-07-09 11:57  chat-new\server\server.c

     文件       3903  2015-07-09 11:56  chat-new\server\server.c~

     文件        582  2015-07-09 10:31  chat-new\server\server.h

     目录          0  2015-07-09 14:13  chat-new\client

     目录          0  2015-07-09 13:18  chat-new\client1

     目录          0  2015-07-09 13:18  chat-new\client2

     目录          0  2015-07-09 13:18  chat-new\server

     目录          0  2015-07-09 14:12  chat-new

----------- ---------  ---------- -----  ----

                98037                    25


评论

共有 条评论