该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
贴吧内经常有人问C语言是不是只能用于字符终端界面开发,不能用于图形界面。大家也都有回答,需要其他的库。MFC,GTK,QT。
本人近期刚用GTK库加上纯C写成了第一个LINUX实用程序。现在与大家分享:
主界面程序gmaxtrix.c
#include
#include"client_socket.h"
#define MAX_BUFFER 1024
static GtkWidget *IP_entry;
static GtkWidget *port_entry;
static gint sockfd;
static gint tag;
gchar buffer[MAX_BUFFER];
void g_show_alarm_dialog(const gchar *message){
GtkWidget *dialog;
dialog = gtk_message_dialog_new(NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"%s",message);
gtk_window_set_title(GTK_WINDOW(dialog),"警告");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
void g_client_send(GtkWidget *widget, gpointer textbuffer){
if (sockfd > 0){
client_send(sockfd, gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(textbuffer)), gtk_entry_buffer_get_bytes(GTK_ENTRY_BUFFER(textbuffer)));
}else{
g_show_alarm_dialog("未连接到中控主机");
}
}
void g_client_recv(gpointer data, gint source, GdkInputCondition condition){
gint count = client_recv(source, buffer, MAX_BUFFER);
buffer[count] = 0;
GtkTextIter iter;
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(data),&iter);
gtk_text_buffer_insert(GTK_TEXT_BUFFER(data),&iter,buffer,-1);
}
void g_client_connect(GtkWidget *widget, gpointer date){
if (sockfd > 0){
client_close(sockfd);
gtk_button_set_label(GTK_BUTTON(widget),"连接");
gdk_input_remove(tag);
}else{
sockfd = client_connect(gtk_entry_get_text(GTK_ENTRY(IP_entry)),atoi(gtk_entry_get_text(GTK_ENTRY(port_entry))));
gtk_button_set_label(GTK_BUTTON(widget),"断开");
tag = gdk_input_add(sockfd, GDK_INPUT_READ, g_client_recv,date);
}
}
int main(int argc, char *argv[]){
gtk_init(&argc, &argv);
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit),NULL);
gtk_widget_set_size_request(window, 260, 250);
gtk_container_set_border_width(GTK_CONTAINER(window),5);
GtkWidget *vbox;
vbox = gtk_vbox_new(FALSE,1);
gtk_container_add(GTK_CONTAINER(window),vbox);
GtkWidget *hbox1;
hbox1 = gtk_hbox_new(FALSE,1);
gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE ,0);
GtkWidget *table = gtk_table_new(2,2,FALSE);
gtk_box_pack_start(GTK_BOX(hbox1), table, TRUE , TRUE , 0);
GtkWidget *IP_label = gtk_label_new("地址:");
gtk_table_attach_defaults(GTK_TABLE(table),IP_label,0,1,0,1);
IP_entry = gtk_entry_new();
gtk_table_attach_defaults(GTK_TABLE(table),IP_entry,1,2,0,1);
GtkWidget *port_label = gtk_label_new("端口:");
gtk_table_attach_defaults(GTK_TABLE(table),port_label,0,1,1,2);
port_entry = gtk_entry_new();
gtk_table_attach_defaults(GTK_TABLE(table),port_entry,1,2,1,2);
GtkWidget *connect_button = gtk_button_new_with_label("连接");
gtk_box_pack_start(GTK_BOX(hbox1), connect_button, TRUE, TRUE ,0);
GtkWidget *hseparator = gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox), hseparator, FALSE, FALSE, 0);
GtkWidget *hbox2;
hbox2 = gtk_hbox_new(FALSE,1);
gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE ,0);
GtkWidget *send_entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(hbox2), send_entry, TRUE, TRUE ,0);
GtkWidget *send_button = gtk_button_new_with_label("发送");
gtk_box_pack_start(GTK_BOX(hbox2), send_button, TRUE, TRUE ,0);
g_signal_connect(send_button, "clicked", G_CALLBACK(g_client_send), gtk_entry_get_buffer(GTK_ENTRY(send_entry)));
GtkWidget *recv_text = gtk_text_view_new();
gtk_box_pack_start(GTK_BOX(vbox), recv_text, TRUE, TRUE, 0);
g_signal_connect(connect_button, "clicked", G_CALLBACK(g_client_connect), gtk_text_view_get_buffer(GTK_TEXT_VIEW(recv_text)));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
后台网络通迅模型接口声明client_socket.h
#ifndef CLIENT_SOCKET_H
#define CLIENT_SOCKET_H
int client_connect(const char *IP, const int port);
void client_close(int sockfd);
void client_send(int sockfd,const unsigned char *buffer, const size_t count);
size_t client_recv(int sockfd,unsigned char *buffer,size_t count);
#endif
后台网络通迅模型实现client_socket.c(采用POSIX接口,WIN下需改为win32API接口)
#include
#include
#include
#include
#include
#include
#include "client_socket.h"
int client_connect(const char *IP, const int port){
static struct sockaddr_in servaddr;
int sockfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, IP, &servaddr.sin_addr);
servaddr.sin_port = htons(port);
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
return sockfd;
}
void client_close(int sockfd){
close(sockfd);
}
void client_send(int sockfd,const unsigned char *buffer, const size_t count){
send(sockfd,buffer,count,0);
}
size_t client_recv(int sockfd,unsigned char *buffer,size_t count){
return recv(sockfd,buffer,count,0);
}
编译脚本makefile
gmaxtrix: gmaxtrix.o client_socket.o
gcc gmaxtrix.o client_socket.o -o gmaxtrix `pkg-config --cflags --libs gtk+-2.0`
gmaxtrix.o: gmaxtrix.c client_socket.h
gcc -c gmaxtrix.c `pkg-config --cflags --libs gtk+-2.0`
client_socket.o: client_socket.h client_socket.c
gcc -c client_socket.c `pkg-config --cflags --libs gtk+-2.0`
clean:
@echo "cleanning project"
-rm gmaxtrix *.o
@echo "clean completed"