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
Forum Updated to NodeBB v4.3 + New Features

facing problem in binding

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 1.3k 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.
  • M Offline
    M Offline
    ManiRon
    wrote on 7 Feb 2019, 04:18 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

    J 1 Reply Last reply 7 Feb 2019, 05:29
    0
    • M ManiRon
      7 Feb 2019, 04:18

      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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Feb 2019, 05:29 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

      M 1 Reply Last reply 7 Feb 2019, 05:48
      1
      • J jsulm
        7 Feb 2019, 05:29

        @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...

        M Offline
        M Offline
        ManiRon
        wrote on 7 Feb 2019, 05:48 last edited by
        #3

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

        J 1 Reply Last reply 7 Feb 2019, 05:51
        0
        • M ManiRon
          7 Feb 2019, 05:48

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 7 Feb 2019, 05:51 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

          M 1 Reply Last reply 7 Feb 2019, 05:57
          0
          • J jsulm
            7 Feb 2019, 05:51

            @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.

            M Offline
            M Offline
            ManiRon
            wrote on 7 Feb 2019, 05:57 last edited by
            #5

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

            J 1 Reply Last reply 7 Feb 2019, 05:58
            0
            • M ManiRon
              7 Feb 2019, 05:57

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 7 Feb 2019, 05:58 last edited by
              #6

              @ManiRon Please copy paste the compiler error

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

              M 1 Reply Last reply 7 Feb 2019, 06:00
              0
              • J jsulm
                7 Feb 2019, 05:58

                @ManiRon Please copy paste the compiler error

                M Offline
                M Offline
                ManiRon
                wrote on 7 Feb 2019, 06:00 last edited by
                #7

                @jsulm 0_1549519170378_Untitled.jpg

                J 1 Reply Last reply 7 Feb 2019, 06:12
                0
                • M ManiRon
                  7 Feb 2019, 06:00

                  @jsulm 0_1549519170378_Untitled.jpg

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 7 Feb 2019, 06:12 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

                  M 1 Reply Last reply 7 Feb 2019, 06:55
                  2
                  • J jsulm
                    7 Feb 2019, 06:12

                    @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

                    M Offline
                    M Offline
                    ManiRon
                    wrote on 7 Feb 2019, 06:55 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

                    J 1 Reply Last reply 7 Feb 2019, 07:01
                    0
                    • M ManiRon
                      7 Feb 2019, 06:55

                      @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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 7 Feb 2019, 07:01 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

                      M 2 Replies Last reply 7 Feb 2019, 07:52
                      0
                      • J jsulm
                        7 Feb 2019, 07:01

                        @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.

                        M Offline
                        M Offline
                        ManiRon
                        wrote on 7 Feb 2019, 07:52 last edited by
                        #11

                        @jsulm

                        I have to check

                        1 Reply Last reply
                        0
                        • J jsulm
                          7 Feb 2019, 07:01

                          @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.

                          M Offline
                          M Offline
                          ManiRon
                          wrote on 7 Feb 2019, 10:29 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

                          1/12

                          7 Feb 2019, 04:18

                          • Login

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