|
- #include <gtk/gtk.h>
- #include <glib.h>
- #include <gio/gio.h>
- #include <stdlib.h>
- #include <string.h>
- #define PORT 5000
- #define IP "172.21.128.219"
- #define BUFMAX 1024
- GtkBuilder *builder;
- GtkWidget *ImageCon;
- GtkWidget *ImageDis;
-
- GList *ip_list;
- GThread *thread_id;
-
- GSocket *socket;
- GSocketClient *client_socket;
- GInetAddress *client_addr;
- GSocketAddress * sock_addr;
- GSocketConnection *sock_con;
- GSocketConnectable *ser_sock;
- GError *sock_err;
-
- ///////////////////////////////////////////////////////////
- void Display(gchar * buf)
- {
- GtkTextBuffer *buf_rcv;
- GtkTextIter buf_rcv_start;
- GtkTextIter buf_rcv_end;
- buf_rcv=GTK_TEXT_BUFFER(gtk_builder_get_object(builder,"textbuffer_rcv"));
- gtk_text_buffer_get_start_iter(buf_rcv,&buf_rcv_start);
- gtk_text_buffer_get_end_iter(buf_rcv,&buf_rcv_end);
- gtk_text_buffer_insert(buf_rcv,&buf_rcv_end,"服务器:\t",-1);
- // buf_rcv_end=0;
- // gtk_text_buffer_get_end_iter(buf_rcv,&buf_rcv_end);
- gtk_text_buffer_insert(buf_rcv,&buf_rcv_end,buf,-1);
- g_free(buf_rcv);
- }
-
- ///////////////////////////////////////////////////////////
- void Receive()
- {
- gchar buf[BUFMAX]={0};
- gint size;
- while (1)
- {
- size=g_socket_receive(socket,buf,BUFMAX,NULL,NULL);
- if (size!=-1)
- {
- g_print("%d\n",size);
- g_print("%s\n",buf);
- Display(buf);
- memset(buf,0,BUFMAX);
- }
- }
- }
- //////////////////////////////////////////////////////////
- gboolean Connect(char * ip,char *port )
- {
- /* address=socket_address_from_string(G_SOCKET_FAMILY_IPV4);
- client.sa_family=AF_INET;
- client.sin_port=htons(PORT);//1234ÊǶ˿ںÅ
- client.sin_addr.s_addr=inet_addr("192.168.0.1");
- socket=g_socket_new(G_SOCKET_FAMILY_IPV4,G_SOCKET_TYPE_STREAM,G_SOCKET_PROTOCOL_TCP,&sock_err);
- g_socket_bind(socket,);*/
-
-
- client_socket=g_socket_client_new();
- client_addr=g_inet_address_new_from_string(IP);
- sock_addr=g_inet_socket_address_new(client_addr, PORT);
- ser_sock=g_network_address_new(ip,atoi(port));
- sock_con=g_socket_client_connect(client_socket,ser_sock,NULL,&sock_err);
- socket=g_socket_connection_get_socket(sock_con);
- g_socket_set_timeout(socket,1);
-
-
- g_object_unref(client_socket);
- g_object_unref(client_addr);
- g_object_unref(sock_addr);
- g_object_unref(ser_sock);
-
- thread_id=g_thread_create((GThreadFunc)Receive,NULL,TRUE,NULL);
-
- if (sock_err==NULL)
- {
- g_print("%s!\n","连接成功");
- return TRUE;
- }
- else
- {
- g_print("%s\n",sock_err->message);
- g_error_free(sock_err);
- return FALSE;
- }
- }
-
- //////////////////////////////////////////////////////////////
- void DisCon()
- {
- while (g_socket_is_closed (socket))
- {
- g_socket_close(socket,NULL);
- g_object_unref(socket);
- }
- g_thread_join(thread_id);
- g_print("%s!\n","断开连接");
- }
-
- ////////////////////////////////////////////////////////////
- void on_button_quit_clicked (GtkObject *object, gpointer user_data)
- {
- g_object_unref (G_OBJECT (builder));
- gtk_main_quit();
- }
-
- ///////////////////////////////////////////////////////////
- void on_togglebutton_con_toggled(GtkObject *object,gpointer user_data)
- {
- gchar * temp;
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (object)))
- {
- g_print("%s\n","pressed!");
- temp=gtk_entry_get_text(GTK_ENTRY(gtk_builder_get_object (builder,"comboboxentry-entry")));//GTK_COMBO(user_data)->
- Connect(strtok(temp,":"),temp+strlen(strtok(temp,":"))+1);
- gtk_button_set_label (GTK_BUTTON(object),"断开");
- ImageDis=gtk_image_new_from_stock("gtk-disconnect",GTK_ICON_SIZE_BUTTON);
- gtk_button_set_image(GTK_BUTTON(object),ImageDis);
- g_free(ImageDis);
- }
- else
- {
- g_print("%s\n","release!");
- DisCon();
- gtk_button_set_label (GTK_BUTTON(object), "连接");
- ImageCon=gtk_image_new_from_stock("gtk-connect",GTK_ICON_SIZE_BUTTON);
- gtk_button_set_image(GTK_BUTTON(object),ImageCon);
- g_free(ImageCon);
- }
- g_free(temp);
- }
- void on_button_snd_clicked(GtkObject *object,gpointer user_data)
- {
- /* buf_snd_count++;
- gtk_text_buffer_get_iter_at_line_index (buf_snd,&buf_snd_start,buf_snd_line,0);
- gtk_text_buffer_get_end_iter(buf_snd,&buf_snd_end);
- if (gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,TRUE))
- {
- if (0==buf_snd_count)
- str_snd=g_string_new("");
- g_assert (str_snd != NULL);
- g_string_append_printf(str_snd,"%d:%s\n",buf_snd_count,gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,TRUE));
- g_print("%s\n",str_snd->str);
- gtk_text_buffer_set_text (buf_snd,str_snd->str,-1);
- }
-
- buf_snd_line=gtk_text_buffer_get_line_count(buf_snd);*/
- gint size;
- GtkTextBuffer *buf_snd;
- GtkTextBuffer *buf_rcv;
- GtkTextIter buf_snd_start;
- GtkTextIter buf_snd_end;
- GtkTextIter buf_rcv_start;
- GtkTextIter buf_rcv_end;
- gchar *temp;
- GSocketAddress *ser_addr;
- ser_addr=g_socket_get_remote_address (socket,NULL);
-
- buf_snd=GTK_TEXT_BUFFER(gtk_builder_get_object(builder,"textbuffer_snd"));
- buf_rcv=GTK_TEXT_BUFFER(gtk_builder_get_object(builder,"textbuffer_rcv"));
-
- gtk_text_buffer_get_start_iter(buf_rcv,&buf_rcv_start);
- gtk_text_buffer_get_end_iter(buf_rcv,&buf_rcv_end);
- gtk_text_buffer_get_start_iter(buf_snd,&buf_snd_start);
- gtk_text_buffer_get_end_iter(buf_snd,&buf_snd_end);
-
- temp=gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,FALSE);
- temp=strcat(temp,"\n");
- temp=strcat("客户端:\t",temp);
- gtk_text_buffer_insert(buf_rcv,&buf_rcv_end,temp,-1);
-
- size=g_socket_send_to(socket,ser_addr,gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,FALSE),strlen(gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,FALSE)),NULL,NULL);
- g_print("%d\n",size);
- gtk_text_buffer_set_text(buf_snd,"",-1);
- gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(builder,"button_snd")),FALSE);
- g_free(buf_snd);
- g_free(buf_rcv);
- g_free(temp);
- }
- ////////////////////////////////////////////////////////////////////////
- void on_textbuffer_changed(GtkObject *object,gpointer user_data)
- {
- gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(builder,"button_snd")),TRUE);
- }
- ///////////////////////////////////////////////////////////////////////
- int main (int argc, char *argv[])
- {
- GtkWidget *window;
- gtk_init (&argc, &argv);
- builder = gtk_builder_new ();
- gtk_builder_add_from_file (builder, "client.xml", NULL);
- window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
-
- // buf_snd=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk_builder_get_object(builder,"textview_snd")));
- // buf_rcv=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk_builder_get_object(builder,"textview_rcv")));
- gtk_builder_connect_signals (builder, NULL);
-
- gtk_widget_show(window);
- gtk_main ();
- return 0;
- }
复制代码 编译命令:
file:///C:/Program%20Files/Tencent/QQ/Users/337927455/Image/V{UP2B$MWI@M6~4IFNG2G0O.jpg
单步:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?马上加入
x
|