#include #include #include #include #include #include #include #include #include #define OUTPORT 3333gint sd;struct sockaddr_in s_in;struct hostent *host;gchar username[64];gchar buf[1024];//readgchar get_buf[1048];//writegboolean isconnected=FALSE;static GtkWidget *text;static GtkTextBuffer *buffer;static GtkWidget *message_entry;static GtkWidget *name_entry;static GtkWidget *login_button;void get_message(void){GtkTextIter iter;gchar get_buf[1024];gchar buf[1024];while(read(sd,buf,1024)!=-1){sprintf(get_buf,"%s",buf);gdk_threads_enter();gtk_text_buffer_get_end_iter(buffer,&iter);gtk_text_buffer_insert(buffer,&iter,get_buf,-1);gdk_threads_leave();}}void on_destroy(GtkWidget *widget,GdkEvent *event,gpointer data){sprintf(username,"guest");if(do_connect()==TRUE){gtk_widget_set_sensitive(login_button,FALSE);g_thread_create((GThreadFunc)get_message,NULL,FALSE,NULL);}gtk_widget_destroy(widget);}void on_button_clicked(GtkButton *button,gpointer data){const gchar *name;name=gtk_entry_get_text(GTK_ENTRY(name_entry));sprintf(username,"%s",name);if(do_connect()){gtk_widget_set_sensitive(login_button,FALSE);g_thread_create((GThreadFunc)get_message,NULL,FALSE,NULL);}gtk_widget_destroy(data);}void creat_win(void){GtkWidget *win,*vbox;GtkWidget *button;win=gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_title(GTK_WINDOW(win),"输入用户名");gtk_container_set_border_width(GTK_CONTAINER(win),10);g_signal_connect(G_OBJECT(win),"delete_event",G_CALLBACK(on_destroy),NULL);gtk_window_set_modal(GTK_WINDOW(win),TRUE);gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);vbox= gtk_vbox_new(FALSE,0);gtk_container_add(GTK_CONTAINER(win),vbox);name_entry=gtk_entry_new();gtk_box_pack_start(GTK_BOX(vbox),name_entry,TRUE,TRUE,5);button=gtk_button_new_from_stock(GTK_STOCK_OK);g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),win);gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,5);gtk_widget_show_all(win);}gboolean do_connect(void){GtkTextIter iter;gint slen;sd=socket(AF_INET,SOCK_STREAM,0);if(sd<0){gtk_text_buffer_get_end_iter(buffer,&iter);gtk_text_buffer_insert(buffer,&iter,"打开套接字时出错!",-1);return FALSE;}s_in.sin_family=AF_INET;s_in.sin_port=htons(OUTPORT);s_in.sin_addr=*((struct in_addr *)host->h_addr);bzero(&(s_in.sin_zero),8);slen=sizeof(s_in);if(connect(sd,(struct sockaddr *) &s_in,slen)<0){gtk_text_buffer_get_end_iter(buffer,&iter);gtk_text_buffer_insert(buffer,&iter,"连接服务器时出错!",-1);return FALSE;}else{gtk_text_buffer_get_end_iter(buffer,&iter);gtk_text_buffer_insert(buffer,&iter,username,-1);gtk_text_buffer_get_end_iter(buffer,&iter);gtk_text_buffer_insert(buffer,&iter,"成功于服务器连接!",-1);//write(sd,username,64);//isconnected=TRUE;return TRUE;}}void on_send(GtkButton *button,gpointer data){const char *message;if(isconnected==FALSE) return;message=gtk_entry_get_text(GTK_ENTRY(message_entry));sprintf(buf,"%s",message);write(sd,buf,1024);gtk_entry_set_text(GTK_ENTRY(message_entry),"");}void on_login(GtkWidget *widget,GdkEvent *weent,gpointer data){creat_win();}void on_delete_event(GtkWidget *widget,GdkEvent *event,gpointer data){close(sd);gtk_main_quit();}int main(int argc,char *argv[]){GtkWidget *window;GtkWidget *vbox,*hbox,*button,*label,*view;if(!g_thread_supported())g_thread_init(NULL);gtk_init(&argc,&argv);window=gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_title(GTK_WINDOW(window),"客户端");g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(on_delete_event),NULL);gtk_container_set_border_width(GTK_CONTAINER(window),10);vbox=gtk_vbox_new(FALSE,0);gtk_container_add(GTK_CONTAINER(window),vbox);hbox=gtk_hbox_new(FALSE,0);gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,5);label=gtk_label_new("点击登录按钮连接服务器");gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);login_button=gtk_button_new_with_label("登录");gtk_box_pack_start(GTK_BOX(hbox),login_button,FALSE,FALSE,5);g_signal_connect(G_OBJECT(login_button),"clicked",G_CALLBACK(on_login),NULL);view=gtk_scrolled_window_new(NULL,NULL);gtk_scrolled_window_se (免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
-
1
1
专为国人订制!Linux Deepin新版发布
-
2
-
3
3
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
-
4
-
5
5
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
-
6
6
FEDORA 17中设置TIGERVNC远程访问
-
7
7
StartOS 5.0相关介绍,新型的Linux系统!
-
8
8
解决vSphere Client登录linux版vCenter失败
-
9
9
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
-
10
-
1
1
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
-
2
2
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
-
3
3
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
-
4
4
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
-
5
5
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
-
6
6
Linux基本命令有哪些 Linux系统常用操作命令有哪些
-
7
7
Linux必学的网络操作命令 linux网络操作相关命令汇总
-
8
8
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
-
9
9
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
-
10
10
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
-
1
|