#include <QtCore/QCoreApplication>
#include <winsock2.h>
#include <stdio.h>
#include <QDebug>
// #include <netinet/in.h>
// #include <arpa.inet.h>
int main(int argc, char *argv[])
{
struct sockaddr_in peer; //"struct sockaddr_in" is a structure type and "peer" is a name of the variable. The "struct" word comes from C and it is not needed in C++
int s;
int rc;
char buf[1];
peer.sin_family = AF_INET;
peer.sin_port = htons(7500);
qDebug() << peer.sin_port;
peer.sin_addr.s_addr = inet_addr("127.0.0.1");
s = socket(AF_INET, SOCK_STREAM, 0);
if(s < 0)
{
perror("socket call failed");
exit(0);
}
rc = connect(s, (struct sockaddr *)&peer, sizeof(peer));
if(rc)
{
perror("connect call failed");
exit(1);
}
rc = send(s, "1", 1, 0);
if(rc <= 0)
{
perror("send call failed");
exit(1);
}
rc = recv(s, buf, 1, 0);
if(rc <= 0)
perror("recv call failed");
else
printf("%c\n", buf[0]);
exit(0);
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <winsock2.h>
#include <stdio.h>
#include <QDebug>
// #include <netinet/in.h>
// #include <arpa.inet.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct sockaddr_in peer; //"struct sockaddr_in" is a structure type and "peer" is a name of the variable. The "struct" word comes from C and it is not needed in C++
int s;
int rc;
char buf[1];
peer.sin_family = AF_INET;
peer.sin_port = htons(7500);
qDebug() << peer.sin_port;
peer.sin_addr.s_addr = inet_addr("127.0.0.1");
s = socket(AF_INET, SOCK_STREAM, 0);
if(s < 0)
{
perror("socket call failed");
exit(0);
}
rc = connect(s, (struct sockaddr *)&peer, sizeof(peer));
if(rc)
{
perror("connect call failed");
exit(1);
}
rc = send(s, "1", 1, 0);
if(rc <= 0)
{
perror("send call failed");
exit(1);
}
rc = recv(s, buf, 1, 0);
if(rc <= 0)
perror("recv call failed");
else
printf("%c\n", buf[0]);
exit(0);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks