分享
 
 
 

Linux下用GTK和socket实现简单的聊天室

王朝system·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

用GTK和socket实现简单的聊天室,出现“段错误”。是哪里的问题??

麻烦看看我的程序,我要用GTK图形界面做一个简单的聊天室,在两台机器上实现通信。现在程序可以编译过去,但是运行时,当客户段弹出登录界面时,输入用户名再点击确定的时候,就在终端显示“段错误”。我是新手,刚刚入门,向前辈们请教,盼回复。

服务器端:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define OUTPORT 3333

#define MAX_USERS 8

struct _client{

gint sd;

gboolean in_use;

gchar name[64];

gchar buf[1024];

};

typedef struct _client client;

client user[MAX_USERS];

void do_service(gpointer id)

{

gint j;

char tobuf[1024];

while(read(user[GPOINTER_TO_INT(id)].sd,

user[GPOINTER_TO_INT(id)].buf,1024)!=-1)

{

sprintf(tobuf,"%s: %s\n",user[GPOINTER_TO_INT(id)].name,

user[GPOINTER_TO_INT(id)].buf);

for(j=0;j {

if(user[j].in_use)

{

write(user[j].sd,tobuf,1024);

g_printf("%s",tobuf);

}

}

}

user[GPOINTER_TO_INT(id)].in_use=FALSE;

close(user[GPOINTER_TO_INT(id)].sd);

//exit(0);

}

int main(int agrv,char *argv[])

{

gint sd,newsd;

struct sockaddr_in *my_addr; /* 本机地址信息 */

struct sockaddr_in *remote_addr; /* 客户端地址信息 */

gint slen;

gint count=0;

gint flags;

gchar buf[1024];

gchar tobuf[1024];

gint length,i,j;

if(!g_thread_supported())

g_thread_init(NULL);

else

g_print("thread not support\n");

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd==-1)

{

g_print("Creat socket error!\n");

return -1;

}

my_addr=g_new(struct sockaddr_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(struct sockaddr_in);

if(bind(sd,(struct sockaddr *) my_addr,slen)<0)

{

g_print("bind error\n!");

return -1;

}

if(listen(sd,8)<0)

{

g_print("listen error!\n");

}

for(i=0;i user.in_use=FALSE;

flags=fcntl(sd,F_GETFL);

fcntl(sd,F_GETFL,flags&~O_NDELAY);

for(;;)

{

newsd=accept(sd,(struct sockaddr *) remote_addr, &slen);

if(newsd==-1)

{

g_print("accept error\n");

break;

}

else

{

if(count>=MAX_USERS)

{

sprintf(buf,"用户数量过多,服务器不能连接!\n");

write(newsd,buf,1024);

close(newsd);

}

else

{

flags=fcntl(user.sd,F_GETFL);

fcntl(user.sd,F_SETFL,O_NONBLOCK);

user[count].sd=newsd;

user[count].in_use=TRUE;

read(newsd,user[count].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

#define OUTPORT 3333

gint sd;

struct sockaddr_in s_in;

struct hostent *host;

gchar username[64];

gchar buf[1024];//read

gchar get_buf[1048];//write

gboolean 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,"打开套接字时出错!\n",-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,"连接服务器时出错!\n",-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,"\n成功于服务器连接!\n",-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\n",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_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();

return FALSE;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有