Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. facing problem in binding
QtWS25 Last Chance

facing problem in binding

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #1

    I am trying to create a sockey program using client server concept but i face problem while binding data in server side . Below is the code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include"winsock2.h"
    #include"winsock.h"

    int MainWindow::serTransmit()
    {
    int server_fd;
    struct sockaddr_in address;
    char buffer[1024] = {0};
    qDebug("PRINT1");
    if ((server_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == 0)
    {
    perror("socket failed");
    exit(EXIT_FAILURE);
    }
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;;
    address.sin_port = htons( PORT );
    qDebug("PRINT2");
    if (bind(server_fd, (sockaddr *)&address, sizeof(sockaddr_in)))
    {
    perror("bind failed");
    exit(EXIT_FAILURE);
    }
    ui->lineEdit->setText(buffer);
    printf("%s\n",buffer );
    return 0;
    }

    Kindly suggest if i have done any mistake and solution for this

    jsulmJ 1 Reply Last reply
    0
    • ManiRonM ManiRon

      I am trying to create a sockey program using client server concept but i face problem while binding data in server side . Below is the code

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include"winsock2.h"
      #include"winsock.h"

      int MainWindow::serTransmit()
      {
      int server_fd;
      struct sockaddr_in address;
      char buffer[1024] = {0};
      qDebug("PRINT1");
      if ((server_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == 0)
      {
      perror("socket failed");
      exit(EXIT_FAILURE);
      }
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY;;
      address.sin_port = htons( PORT );
      qDebug("PRINT2");
      if (bind(server_fd, (sockaddr *)&address, sizeof(sockaddr_in)))
      {
      perror("bind failed");
      exit(EXIT_FAILURE);
      }
      ui->lineEdit->setText(buffer);
      printf("%s\n",buffer );
      return 0;
      }

      Kindly suggest if i have done any mistake and solution for this

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ManiRon Why don't you use http://doc.qt.io/qt-5/qtnetwork-index.html ? Then your question would be related to Qt.
      And you don't even mention what problem you face...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      ManiRonM 1 Reply Last reply
      1
      • jsulmJ jsulm

        @ManiRon Why don't you use http://doc.qt.io/qt-5/qtnetwork-index.html ? Then your question would be related to Qt.
        And you don't even mention what problem you face...

        ManiRonM Offline
        ManiRonM Offline
        ManiRon
        wrote on last edited by
        #3

        @jsulm my problem is when i try to bind data it returns an error saying the bind failed: invalid argument

        jsulmJ 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @jsulm my problem is when i try to bind data it returns an error saying the bind failed: invalid argument

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ManiRon Are you aware that your question is completely unrelated to Qt?
          You're using low level socket API not Qt.
          Please post at least the whole compiler error.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          ManiRonM 1 Reply Last reply
          0
          • jsulmJ jsulm

            @ManiRon Are you aware that your question is completely unrelated to Qt?
            You're using low level socket API not Qt.
            Please post at least the whole compiler error.

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by
            #5

            @jsulm yes sir this is what the error say invalid argument in bind

            jsulmJ 1 Reply Last reply
            0
            • ManiRonM ManiRon

              @jsulm yes sir this is what the error say invalid argument in bind

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ManiRon Please copy paste the compiler error

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              ManiRonM 1 Reply Last reply
              0
              • jsulmJ jsulm

                @ManiRon Please copy paste the compiler error

                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #7

                @jsulm 0_1549519170378_Untitled.jpg

                jsulmJ 1 Reply Last reply
                0
                • ManiRonM ManiRon

                  @jsulm 0_1549519170378_Untitled.jpg

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ManiRon So, it's not a compiler error but a runtime error.
                  You should zero your address first like

                  bzero((char *) &address, sizeof(address));
                  

                  As shown here http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  ManiRonM 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @ManiRon So, it's not a compiler error but a runtime error.
                    You should zero your address first like

                    bzero((char *) &address, sizeof(address));
                    

                    As shown here http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html

                    ManiRonM Offline
                    ManiRonM Offline
                    ManiRon
                    wrote on last edited by
                    #9

                    @jsulm said in facing problem in binding:

                    bzero((char *) &address, sizeof(address));

                    I used memset(&address, 0, sizeof(sockaddr_in)); this and set it to zero but still the problem exists

                    jsulmJ 1 Reply Last reply
                    0
                    • ManiRonM ManiRon

                      @jsulm said in facing problem in binding:

                      bzero((char *) &address, sizeof(address));

                      I used memset(&address, 0, sizeof(sockaddr_in)); this and set it to zero but still the problem exists

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @ManiRon Are you sure you want to use SOCK_DGRAM (UDP) and not SOCK_STREAM (TCP)?
                      If I remember correctly you don't need bind() for UDP.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      ManiRonM 2 Replies Last reply
                      0
                      • jsulmJ jsulm

                        @ManiRon Are you sure you want to use SOCK_DGRAM (UDP) and not SOCK_STREAM (TCP)?
                        If I remember correctly you don't need bind() for UDP.

                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by
                        #11

                        @jsulm

                        I have to check

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @ManiRon Are you sure you want to use SOCK_DGRAM (UDP) and not SOCK_STREAM (TCP)?
                          If I remember correctly you don't need bind() for UDP.

                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by
                          #12

                          @jsulm

                          actually socket creation itself is a problem
                          server_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)

                          here server_fd is -1

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved