首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gtkmm3的套接字服务器

使用gtkmm3的套接字服务器
EN

Stack Overflow用户
提问于 2016-06-01 23:01:03
回答 1查看 331关注 0票数 0

我正在尝试制作一个GUI,当用户按下按钮时,它将连接到网络。我在尝试编译gui_ex.cpp文件时遇到编译错误。

代码语言:javascript
复制
gui_ex.cpp: In member function ‘void Gui_Ex::on_connect()’:
gui_ex.cpp:110:19: error: no matching function for call to ‘Gui_Ex::close(int&)’
         close(sock);
                   ^
gui_ex.cpp:110:19: note: candidate is:
In file included from /usr/include/gtkmm-3.0/gtkmm/dialog.h:30:0,
                 from /usr/include/gtkmm-3.0/gtkmm/aboutdialog.h:33,
                 from /usr/include/gtkmm-3.0/gtkmm.h:99,
                 from gui_ex.h:4,
                 from gui_ex.cpp:1:
/usr/include/gtkmm-3.0/gtkmm/window.h:2026:8: note: void Gtk::Window::close()
   void close();
        ^
/usr/include/gtkmm-3.0/gtkmm/window.h:2026:8: note:   candidate expects 0 arguments, 1 provided
gui_ex.cpp:117:19: error: no matching function for call to ‘Gui_Ex::close(int&)’
         close(sock);
                   ^
gui_ex.cpp:117:19: note: candidate is:
In file included from /usr/include/gtkmm-3.0/gtkmm/dialog.h:30:0,
                 from /usr/include/gtkmm-3.0/gtkmm/aboutdialog.h:33,
                 from /usr/include/gtkmm-3.0/gtkmm.h:99,
                 from gui_ex.h:4,
                 from gui_ex.cpp:1:
/usr/include/gtkmm-3.0/gtkmm/window.h:2026:8: note: void Gtk::Window::close()
   void close();
        ^
/usr/include/gtkmm-3.0/gtkmm/window.h:2026:8: note:   candidate expects 0 arguments, 1 provided

看起来程序把套接字的close命令和Gtk::Window的close命令搞混了。有没有可能调整代码,以便编译器能够区分这两个?

下面是我的代码: gui_ex.h文件

代码语言:javascript
复制
#ifndef GTKMM_EX_GUI_H
#define GTKMM_EX_GUI_H

#include <gtkmm.h>

class Gui_Ex : public Gtk::Window
{
public:
    Gui_Ex();
    virtual ~Gui_Ex();

protected:
    // Signal handlers:
    // new button
    void on_connect();
  void on_spin();

  //void on_spinbutton_digits_changed();

    //child widgets
  //Gtk::Frame m_Frame_Init, m_Frame_Control;
  Glib::RefPtr<Gtk::Adjustment> m_adjustment;
  Gtk::Label m_label;
  Gtk::Grid m_grid;

  Gtk::SpinButton m_spin;
    Gtk::Button m_button2, m_button3; //m_connect;



};

#endif // GTKMM_EX_GUI_H

gui_ex.cpp

代码语言:javascript
复制
#include "gui_ex.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <string.h> // memset
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <vector>
#include <string>
using namespace std;

#define PORT "8888"
//#define BACKLOG 2
#define IP_ADDR "192.168.137.99"
#define MAXLEN 1024

int BACKLOG =0;
static unsigned int cli_count = 0;
static int uid = 20;
vector<int> cliarray;

Gui_Ex::Gui_Ex()
:
  // m_button1("Button 1"),
   m_adjustment( Gtk::Adjustment::create(1.0, 1.0, 5.0, 1.0, 5.0, 0.0) ),
   m_button2("Enter"),
   m_button3("Connect"),
   m_spin(m_adjustment),
   m_label("Choose # clients")


{
    set_title("Grid");
  set_border_width(12);


  m_grid.attach(m_label, 0,0,1,1); //column, row, width (# col span), height (# row span)
  m_grid.attach(m_spin, 1,0,1,1);
  m_spin.set_wrap();
  m_spin.set_numeric(true);

  m_grid.attach(m_button2, 2,0,1,1);

  m_grid.attach(m_button3, 0,1,3,1);
  m_button3.set_sensitive(false);

  m_button2.signal_clicked().connect(sigc::mem_fun(*this, &Gui_Ex::on_spin));
  m_button3.signal_clicked().connect(sigc::mem_fun(*this, &Gui_Ex::on_connect));
  //m_button3.signal_clicked().connect(sigc::bind<int>(sigc::mem_fun(*this, &Gui_Ex::on_connect), m_spin.get_value_as_int()));  

  add(m_grid);
  m_grid.show_all();
  //show_all_children();

}

Gui_Ex::~Gui_Ex()
{
}


void Gui_Ex::on_spin()
{
  cout<<"Spin value =: " << m_spin.get_value_as_int() << endl;
  BACKLOG = m_spin.get_value_as_int();
  m_spin.set_sensitive(false);
  m_button2.set_sensitive(false);
  m_button3.set_sensitive(true);

}

void Gui_Ex::on_connect()
{
    int connfd =0, n = 0;
    int *new_sock, sock;

    cout << BACKLOG << endl;
    pthread_t thread;
    struct addrinfo hints, *res;
    int reuseaddr = 1; // True 

    // Get the address info 
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    if (getaddrinfo(IP_ADDR, PORT, &hints, &res) != 0) {
        perror("getaddrinfo");
        //return 1;
    }

    // Create the socket 
    sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sock == -1) {
        perror("socket");
       // return 1;
    }

    // Enable the socket to reuse the address 
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
        perror("setsockopt");
        close(sock);
       // return 1;
    }

    // Bind to the address 
    if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
        perror("bind");
        close(sock);
        //return 0;
    }

    freeaddrinfo(res);

    // Listen 
    if (listen(sock, BACKLOG) == -1) {
        perror("listen");
       // return 0;
    }
    cout << "listening for connections" << endl;
    // Main loop 
    bool running = true;
    // Initialize clients 
    while (running)
    {  
      size_t size = sizeof(struct sockaddr_in);
      struct sockaddr_in their_addr;
      int clilen = sizeof(their_addr);
      int newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
      if (newsock == -1) 
      {
        perror("accept");
       // return -1;
      }
      cli_count++;
      printf("Got a connection from %s on port %d\n", inet_ntoa(their_addr.sin_addr), htons(their_addr.sin_port));
      cliarray.push_back(newsock);
      if (cli_count == BACKLOG)
      {
         cout << "Max clients reached" << endl;
        running = false;
        break;
      }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-06-02 05:59:30

您只需要在这里显式地调用全局名称空间。也就是说,当尝试调用libc中的close函数而不是基类的close方法时,应该预先考虑全局名称空间指示符:

代码语言:javascript
复制
::close(sock);

基本上,对象命名空间中的close方法隐藏了同名的全局符号。有关更完整的解释,请参阅第一个答案:Global scope vs global namespace

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37572348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档