C语言 接收聊天程序消息时遇到问题



我正在使用 C 套接字编程和 GUI 的 gtk+ 开发一个 C 聊天程序。 我在 2 台独立的计算机上运行了 2 种程序变体,1 种变体具有客户端代码,另一种变体具有服务器端代码,两者都具有相同的 GUI 和相同的用户功能选项。

当客户端是 GUI 并且服务器在没有 GUI 的情况下运行时,我的程序可以工作,但是当我尝试为服务器启用 GUI 时,客户端仍然可以连接到服务器,但由于某种原因消息似乎没有通过。

我使用glade,这是一个可以轻松创建GUI的软件,因此语法与gtk略有不同,但它使用gtk,因此应该相同。 我的服务器 GUI 的主要功能如下所示:

int main(int argc, char *argv[])
{
//GtkBuilder      *builder;
GtkWidget       *window;
//creating a widgets from type app_widgets
app_widgets     *widgets = g_slice_new(app_widgets);
//GtkTextIter iter;
//creating gtk_init
gtk_init(&argc, &argv);
printf("creating gtk_init successfully");
//using gtk_builder to create the graphics for the gui using glade
builder = gtk_builder_new_from_file("glade/window_main.glade");
//binding the builder to the main container
window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));

// Get pointers to widgets
//pointer to the main text view where chat is shown
widgets->w_txtvw_main = GTK_WIDGET(gtk_builder_get_object(builder, "txtview_main"));
//pointer to main text buffer that displays the chat
widgets->textbuffer_main = GTK_TEXT_BUFFER(gtk_builder_get_object(builder, "textbuffer_main"));
//pointer to text buffer where user can type msgs for server
widgets->textbuffer_type = GTK_TEXT_BUFFER(gtk_builder_get_object(builder, "textbuffer_type"));

//connecting the widgets to signal to the builder
gtk_builder_connect_signals(builder, widgets);
//gtk_widget_show is called once done setting up widget settings
gtk_widget_show(window);
//start client side
//connecting to sock
sock = startServer();
printf("sock: %d", sock);
if(sock < 0) {
printf("PROGRAM ERROR: Client did not launch!n");
exit(0);
}
int valListen = listenToSock(sock);
printf("valListen: %dn", valListen);
if(valListen == -1) {
printf("PROGRAM ERROR: Server failed listening to socket");
printf("Please Relaunch the Application!");
exit(0);
}
printf("sock: %d", sock);
/*
//validating key generation
int valKey = getKey(keyP);
//once password was entered successfully, check will be changed to 1
int check = 0;
if(valKey < 0) {
printf("PROGRAM ERROR: password hashed did not hash successfully");
exit(0);
}
*/

//currReceive is the status of the received data, if equal to 1 than data received,
int currReceive = 0;
//bufOutput is the data received
char bufOutput[] = "";
//pointer to bufOutput
char  * bOP = bufOutput;
//using a while loop for gtk_main_iteration
//using gtk_main_iteration() instead of gtk_main() because i need to receive msgs while gui works - need to handle a few events
while (1==1) {
//runs 1 iteration of main loop
gtk_main_iteration();
//checking if received data
currReceive = receive(bOP, sock, key, valQ);
//currReceive = 1 if data was received
if(currReceive == 1) {
//printing msg in cmd
printf("nMessage: %s<n", bOP);
//updating the main buffer for viewing chat with the new msg received
update_main_viewedit(widgets, bOP, "Server: ");
}
//if receiving failed, quit
if(currReceive == -1) {
printf("PROGRAM ERROR: Receiving from server failed");
on_window_main_destroy();
return -1;
}
}

//free widgets memory
g_slice_free(app_widgets, widgets);
return 0;
}

接收函数工作正常,因为我已经在 GUI 外部对其进行了测试,并且还运行一个简单的函数来测试接收和发送函数,它们无缝工作: 简单函数如下所示:


void communicateUsingFunctions(int sock) {

int valid;
char info[MAX];
char * send = info;
while(1 == 1) {
valid = receive(send, sock, 3, 0);
if(valid == 1) {
printf("Message From Functions: %s", send);
sendToClient(send, sock, "dsad");
}
}
}

我希望得到一些帮助,因为我不知道为什么它不起作用,接收和发送功能都可以在另一端的 GUI 上正常工作。

感谢任何做出贡献的人!

你真的不应该尝试通过调用gtk_main_iteration()来组合你自己的手动滚动while (TRUE)循环而不是GTK正在使用的事件循环(又名GMainLoop(。另请注意,文档甚至明确提到这是一个阻塞调用:

如果没有事件等待处理,GTK 将阻塞,直到注意到下一个事件。

试图做这样时髦的事情的人真的是为什么gtk_main*()从 GTK4 中删除(赞成只使用GtkApplication(。所以这个:

//using gtk_main_iteration() instead of gtk_main() because i need to receive msgs while gui works - need to handle a few events

并不是必须同时运行东西的好方法。

关于如何实际解决这个问题:有很多方法可以在 GLib 中调用异步代码,这样你就不会阻塞主线程(UI 运行的地方(,它实际上也很好地集成到主循环中。例如,请参阅:

  • GIOStream(及其几个子类(
  • GSocket
  • GSource(如果你真的想降低级别并集成一个自定义主循环,但这似乎对你的用例来说是矫枉过正(

相关内容

最新更新