facing problem in binding
-
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
-
@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... -
@ManiRon So, it's not a compiler error but a runtime error.
You should zero your address first likebzero((char *) &address, sizeof(address));
As shown here http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html