来看看:Linux下用GTK和socket完成复杂的谈天室
RedHatCentOS等等.学习linux不是逛自由市场,选定版本就要静下心来学习.不要今天换版本明天要升级.这样对你没有好处。用GTK和socket完成复杂的谈天室,呈现“段毛病”。是那里的成绩??
贫苦看看我的程序,我要用GTK图形界面做一个复杂的谈天室,在两台呆板上完成通讯。如今程序能够编译已往,可是运转时,当客户段弹出登录界面时,输出用户名再点击断定的时分,就在终端显现“段毛病”。我是老手,方才进门,向先辈们就教,盼复兴。
服务器端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#defineOUTPORT3333
#defineMAX_USERS8
struct_client{
gintsd;
gbooleanin_use;
gcharname;
gcharbuf;
};
typedefstruct_clientclient;
clientuser;
voiddo_service(gpointerid)
{
gintj;
chartobuf;
while(read(user.sd,
user.buf,1024)!=-1)
{
sprintf(tobuf,"%s:%s
",user.name,
user.buf);
for(j=0;j{
if(user.in_use)
{
write(user.sd,tobuf,1024);
g_printf("%s",tobuf);
}
}
}
user.in_use=FALSE;
close(user.sd);
//exit(0);
}
intmain(intagrv,char*argv[])
{
gintsd,newsd;
structsockaddr_in*my_addr;/*本机地点信息*/
structsockaddr_in*remote_addr;/*客户端地点信息*/
gintslen;
gintcount=0;
gintflags;
gcharbuf;
gchartobuf;
gintlength,i,j;
if(!g_thread_supported())
g_thread_init(NULL);
else
g_print("threadnotsupport
");
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd==-1)
{
g_print("Creatsocketerror!
");
return-1;
}
my_addr=g_new(structsockaddr_in,1);
my_addr->sin_family=AF_INET;
my_addr->sin_port=htons(OUTPORT);
my_addr->sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr->sin_zero),8);
slen=sizeof(structsockaddr_in);
if(bind(sd,(structsockaddr*)my_addr,slen)<0)
{
g_print("binderror
!");
return-1;
}
if(listen(sd,8)<0)
{
g_print("listenerror!
");
}
for(i=0;iuser.in_use=FALSE;
flags=fcntl(sd,F_GETFL);
fcntl(sd,F_GETFL,flags&~O_NDELAY);
for(;;)
{
newsd=accept(sd,(structsockaddr*)remote_addr,&slen);
if(newsd==-1)
{
g_print("accepterror
");
break;
}
else
{
if(count>=MAX_USERS)
{
sprintf(buf,"用户数目过量,服务器不克不及毗连!
");
write(newsd,buf,1024);
close(newsd);
}
else
{
flags=fcntl(user.sd,F_GETFL);
fcntl(user.sd,F_SETFL,O_NONBLOCK);
user.sd=newsd;
user.in_use=TRUE;
read(newsd,user.name,64);
g_thread_create((GThreadFunc)do_service,
(gpointer)count,TRUE,NULL);
count++;
}
}
}//for(;;)
close(sd);
g_free(my_addr);
}
服务器端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#defineOUTPORT3333
gintsd;
structsockaddr_ins_in;
structhostent*host;
gcharusername;
gcharbuf;//read
gcharget_buf;//write
gbooleanisconnected=FALSE;
staticGtkWidget*text;
staticGtkTextBuffer*buffer;
staticGtkWidget*message_entry;
staticGtkWidget*name_entry;
staticGtkWidget*login_button;
voidget_message(void)
{
GtkTextIteriter;
gcharget_buf;
gcharbuf;
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();
}
}
voidon_destroy(GtkWidget*widget,GdkEvent*event,gpointerdata)
{
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);
}
voidon_button_clicked(GtkButton*button,gpointerdata)
{
constgchar*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);
}
voidcreat_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);
}
gbooleando_connect(void)
{
GtkTextIteriter;
gintslen;
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);
returnFALSE;
}
s_in.sin_family=AF_INET;
s_in.sin_port=htons(OUTPORT);
s_in.sin_addr=*((structin_addr*)host->h_addr);
bzero(&(s_in.sin_zero),8);
slen=sizeof(s_in);
if(connect(sd,(structsockaddr*)&s_in,slen)<0)
{
gtk_text_buffer_get_end_iter(buffer,&iter);
gtk_text_buffer_insert(buffer,&iter,"毗连服务器时堕落!
",-1);
returnFALSE;
}
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;
returnTRUE;
}
}
voidon_send(GtkButton*button,gpointerdata)
{
constchar*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),"");
}
voidon_login(GtkWidget*widget,GdkEvent*weent,gpointerdata)
{
creat_win();
}
voidon_delete_event(GtkWidget*widget,GdkEvent*event,gpointerdata)
{
close(sd);
gtk_main_quit();
}
intmain(intargc,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_set_policy(GTK_SCROLLED_WINDOW(view),
GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
text=gtk_text_view_new();
gtk_box_pack_start(GTK_BOX(vbox),view,TRUE,TRUE,5);
gtk_container_add(GTK_CONTAINER(view),text);
buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
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);
message_entry=gtk_entry_new();
gtk_box_pack_start(GTK_BOX(hbox),message_entry,FALSE,FALSE,5);
button=gtk_button_new_with_label("发送");
gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(on_send),NULL);
gtk_widget_show_all(window);
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
returnFALSE;
}
</p>
学习linux,就意味着更快的开发效率,等更多关于软件本身或者说操作系统本身的理解。 目前全球有超过一百多个Linux发行版本,在国内也能找到十几个常见版本。如何选择请根据你的需求和能力,RedhatLinux和DebianLinux是网络管理员的理想选择。 这也正是有别的OS得以存在的原因,每个系统都有其自身的优点。? 上课传授的不仅仅是知识,更重要的是一些道理,包括一些做人的道理,讲课时也抓住重点,循序渐进,让同学理解很快;更可贵的是不以你过去的成绩看问题. 学习Linux,应该怎样学,主要学些什么,一位Linux热心学习者,一段学习Linux的风云经验,历时十二个小时的思考总结,近十位网络Linux学习者权威肯定,为您学习Linux指明方向。 linux鸟哥的私房菜,第三版,基础篇,网上有pdf下的,看它的目录和每章的介绍就行了,这个绝对原创! 尽我能力帮助他人,在帮助他人的同时你会深刻巩固知识。 我想即使Linux高手也很难快速准确精练的回答你。
页:
[1]