亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3518 | 回復(fù): 5
打印 上一主題 下一主題

gtk2.0 + socket編程求教 內(nèi)有代碼 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2003-07-08 17:31 |只看該作者 |倒序瀏覽
下面的代碼是我更改 gtk教材中的代碼 想實現(xiàn)本機的udp通信的

編譯通過 但是 sendto 和 recvfrom都沒有什么反應(yīng) 請各位幫幫忙看看

我覺得主要就是
void *recvbuf()
void *sendbuffer()

OnRun這三個函數(shù) 其實沒有什么的 怎么會不行呢
  1. #include <stdio.h>;
  2. #include <string.h>;
  3. #include <sys/types.h>;
  4. #include <stdlib.h>;
  5. #include <unistd.h>;
  6. #include <sys/socket.h>;
  7. #include <netinet/in.h>;
  8. #include <arpa/inet.h>;
  9. #include <pthread.h>;
  10. #include <time.h>;
  11. #include <gtk/gtk.h>;

  12. /* Our new improved callback.  The data passed to this function
  13. * is printed to stdout. */
  14. int socketid;
  15. void *recvbuf()
  16. {
  17.      struct sockaddr_in my_addr;
  18.         int addrlen;
  19.         char buffer[10];
  20.         int periodic;
  21.   memset(&my_addr,0,sizeof(struct sockaddr_in));
  22.    my_addr.sin_family = AF_INET;
  23.    my_addr.sin_port   = htons(7802);
  24.    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  25.    addrlen = sizeof(my_addr);
  26.    periodic = 0;
  27. for(;;)
  28.    {
  29.            sprintf(buffer,"%d",periodic);
  30.            sendto(socketid, buffer, sizeof(int), 0,(struct sockaddr *)&my_addr, addrlen);
  31.            periodic++;
  32.    }
  33. }
  34. void *sendbuffer()
  35. {
  36. struct sockaddr_in my_addr;
  37.   char buffer[10];
  38.   int addrlen;
  39.                 printf("test!!!");
  40.   memset(&my_addr,0,sizeof(struct sockaddr_in));
  41.   my_addr.sin_family = AF_INET;
  42.   my_addr.sin_port   = htons(7802);
  43.   my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  44.   addrlen = sizeof(my_addr);
  45.     for(;;)
  46.         {
  47.                 printf("test!!!");
  48.           recvfrom(socketid, buffer, sizeof(int), 0,(struct sockaddr *)&my_addr, &addrlen);
  49.           
  50.                   printf("%s\n", buffer);
  51.          
  52.           
  53.   }

  54. }
  55. void OnRun( GtkWidget *widget,
  56.                gpointer   data )
  57. {
  58.         int rc;
  59.         pthread_t threadid;
  60.         socketid = socket(AF_INET, SOCK_DGRAM, 0);
  61.         if (socketid < 0)
  62.         {
  63.                 perror("create socket error");
  64.                 exit (1);
  65.         }

  66.    rc =  pthread_create(&threadid, NULL, sendbuffer, NULL);
  67.    pthread_create(&threadid, NULL, recvbuf,NULL);
  68.    for(;;);
  69.           
  70.    pthread_exit(NULL);


  71. }

  72. /* another callback */
  73. gint delete_event( GtkWidget *widget,
  74.                    GdkEvent  *event,
  75.                    gpointer   data )
  76. {
  77.     gtk_main_quit ();
  78.     return FALSE;
  79. }

  80. int main( int   argc,
  81.           char *argv[] )
  82. {
  83.     /* GtkWidget is the storage type for widgets */
  84.     GtkWidget *window;
  85.     GtkWidget *button;
  86.         GtkWidget *stopbutton;
  87.     GtkWidget *box1;

  88.     /* This is called in all GTK applications. Arguments are parsed
  89.      * from the command line and are returned to the application. */
  90.     gtk_init (&argc, &argv);

  91.     /* Create a new window */
  92.     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  93.     /* This is a new call, which just sets the title of our
  94.      * new window to "Hello Buttons!" */
  95.     gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");

  96.     /* Here we just set a handler for delete_event that immediately
  97.      * exits GTK. */
  98.     g_signal_connect (G_OBJECT (window), "delete_event",
  99.                       G_CALLBACK (delete_event), NULL);

  100.     /* Sets the border width of the window. */
  101.     gtk_container_set_border_width (GTK_CONTAINER (window), 10);

  102.     /* We create a box to pack widgets into.  This is described in detail
  103.      * in the "packing" section. The box is not really visible, it
  104.      * is just used as a tool to arrange widgets. */
  105.     box1 = gtk_hbox_new (FALSE, 0);

  106.     /* Put the box into the main window. */
  107.     gtk_container_add (GTK_CONTAINER (window), box1);

  108.     /* Creates a new button with the label "Button 1". */
  109.     button = gtk_button_new_with_label ("start");
  110.    
  111.     /* Now when the button is clicked, we call the "callback" function
  112.      * with a pointer to "button 1" as its argument */
  113.     g_signal_connect (G_OBJECT (button), "clicked",
  114.                       G_CALLBACK (OnRun), NULL);

  115.     /* Instead of gtk_container_add, we pack this button into the invisible
  116.      * box, which has been packed into the window. */
  117.     gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);

  118.     /* Always remember this step, this tells GTK that our preparation for
  119.      * this button is complete, and it can now be displayed. */
  120.     gtk_widget_show (button);

  121.     /* Do these same steps again to create a second button */
  122.     stopbutton = gtk_button_new_with_label ("stop");

  123.     /* Call the same callback function with a different argument,
  124.      * passing a pointer to "button 2" instead. */
  125.     g_signal_connect (G_OBJECT (stopbutton), "clicked",
  126.                        G_CALLBACK (OnRun), NULL);

  127.     gtk_box_pack_start(GTK_BOX (box1), stopbutton, TRUE, TRUE, 0);

  128.     /* The order in which we show the buttons is not really important, but I
  129.      * recommend showing the window last, so it all pops up at once. */
  130.     gtk_widget_show (stopbutton);

  131.     gtk_widget_show (box1);

  132.     gtk_widget_show (window);
  133.    
  134.     /* Rest in gtk_main and wait for the fun to begin! */
  135.     gtk_main ();

  136.     return 0;
  137. }
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2003-07-08 19:38 |只看該作者

gtk2.0 + socket編程求教 內(nèi)有代碼

為什么要寫那么多


看看udp的例子吧
然后檢查自己哪里有錯

論壇徽章:
0
3 [報告]
發(fā)表于 2003-07-08 21:22 |只看該作者

gtk2.0 + socket編程求教 內(nèi)有代碼

我只是想左右一個可以交互的而已啊。

其實里面只有三個函數(shù)是我自己寫的下面的那個是gtk教程的例子


剛才在 linuxsir中有人說見到 gtk就暈 我想問一下用c作界面的話 有什么

是和unix下面通用的? 因為我的程序最終要用在 unix系統(tǒng)中 (TRU64-alpha)

論壇徽章:
0
4 [報告]
發(fā)表于 2003-07-08 21:37 |只看該作者

gtk2.0 + socket編程求教 內(nèi)有代碼

很難保證
先到true 64版上面有沒有g(shù)tk庫吧

這種如果不復(fù)雜的話可以使用java做

論壇徽章:
0
5 [報告]
發(fā)表于 2003-07-09 02:40 |只看該作者

gtk2.0 + socket編程求教 內(nèi)有代碼

tru64 有cde環(huán)境, 還可以用motif來處理

論壇徽章:
1
榮譽版主
日期:2011-11-23 16:44:17
6 [報告]
發(fā)表于 2003-07-10 13:24 |只看該作者

gtk2.0 + socket編程求教 內(nèi)有代碼

如果是解決sendto和recvfrom的話,F(xiàn)AQ中提過了不是討論信息。

如果是為了界面的話,還是建議不用UDP,本機用socket域,非本地用其它的方法,如http。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP