|
楼主 |
发表于 2004-5-19 09:00:30
|
显示全部楼层
while(1)
{
z=recvfrom(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&clntadr_inet,&len_inet); //user login!
dgram=dgram1;
if(!strcmp(dgram,"login"))
{
usercode++;
//发送用户登录信号"login":
sendto(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&bcadr_inet,len_inet);
//接收用户名:
z=recvfrom(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&clntadr_inet,&len_inet);
strcpy(userstrack[usercode],dgram1);
printf("the userstrack[%d] is: %s\n",usercode,userstrack[usercode]);
uname[usercode]=dgram1;
printf("the uname[%d] is : %s\n",usercode,username[usercode]);
user=usercode;
usernum1[0]=user;
//发送在线用户数:
sendto(s,usernum1,sizeof(usernum1),0,(struct sockaddr *)&bcadr_inet,len_inet);
z=recvfrom(s,dgram2,sizeof(dgram2),0,(struct sockaddr *)&clntadr_inet,&len_inet);
//发送所有在线用户的用户名:
for(r=1;r<=usercode;r++)
{
sendto(s,userstrack[r],sizeof(userstrack[r]),0,(struct sockaddr *)&bcadr_inet,len_inet);
z=recvfrom(s,dgram2,sizeof(dgram2),0,(struct sockaddr *)&clntadr_inet,&len_inet);
}
//将最新登录的用户显示
switch(usercode){
case 1:
gtk_button_set_label(GTK_BUTTON(button3),username[1]);
gtk_widget_show(button3);
break;
case 2:
gtk_button_set_label(GTK_BUTTON(button4),username[2]);
gtk_widget_show(button4);
break;
case 3:
gtk_button_set_label(GTK_BUTTON(button5),username[3]);
gtk_widget_show(button5);
break;
case 4:
gtk_button_set_label(button6,username[4]);
gtk_widget_show(button6);
break;
case 5:
gtk_button_set_label(button7,username[usercode]);
gtk_widget_show(button7);
break;
case 6:
gtk_button_set_label(button8,username[usercode]);
gtk_widget_show(button;
break;
case 7:
gtk_button_set_label(button9,username[usercode]);
gtk_widget_show(button9);
break;
case 8:
gtk_button_set_label(button10,username[usercode]);
gtk_widget_show(button10);
break;
case 9:
gtk_button_set_label(button11,username[usercode]);
gtk_widget_show(button11);
break;
} //the end of switch
} //the end of if
else{
gtk_text_buffer_get_end_iter(buffer,&iter);
gtk_text_buffer_insert(buffer,&iter,dgram,-1);
sendto(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&bcadr_inet,len_inet);
z=recvfrom(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&clntadr_inet,&len_inet); // 's massege is :
dgram=dgram1;
gtk_text_buffer_insert(buffer,&iter,dgram,-1);
sendto(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&bcadr_inet,len_inet);
z=recvfrom(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&clntadr_inet,&len_inet); // the user's massage
dgram=dgram1;
gtk_text_buffer_insert(buffer,&iter,dgram,-1);
sendto(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&bcadr_inet,len_inet);
z=recvfrom(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&clntadr_inet,&len_inet); // the face or picture
dgram=dgram1;
gtk_text_buffer_insert(buffer,&iter,dgram,-1);
sendto(s,dgram1,sizeof(dgram1),0,(struct sockaddr *)&bcadr_inet,len_inet);
gtk_text_buffer_insert(buffer,&iter,"\n",-1);
}
}
这是一个聊天室服务器程序的主要部分,程序中在线用户的用户名是写在按钮上,就是在显示按钮时,有时能成功,有时不能.不能显示时,通过在gtk_widget_show后面加一条printf语句,可以知道其实已经执行到了.请指教! |
|