Why got invalid socket in eclipse debug mode only? - c++

In eclipse C++ I got an invalid socket when starting the debug mode.
The error is WSAEPROVIDERFAILEDINIT.
I got this error when using the debugger only. Starting with run, the creation of the socket is successful.
Under VisualStudio the same code runs in debug and release mode.
Debugging with eclipse and mingw works fine when no socket is used.
It seems to be a problem with the configuration of gdb, because eclipse is using the same exe independent if I start with run or with debug (there is no change of the timestamp which I change the configuration).
The configuration is: eclipse 4.6.3, Mingw32-gcc-g++ V6.3.0-1, I linked the libws2_32 from \Mingw\lib, under Windows 10, 64bit.
\Mingw\bin is added to the path variable under Windows and in Eclipse itself.
This is the the code:
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
int main()
{
WORD sockVer = MAKEWORD(2, 0);
WSADATA wsaData;
SOCKET listener;
SOCKADDR_IN servInfo;
servInfo.sin_family = AF_INET;
servInfo.sin_addr.s_addr = INADDR_ANY;
servInfo.sin_port = htons(80);
WSAStartup(sockVer, &wsaData);
listener = socket(servInfo.sin_family, SOCK_STREAM, IPPROTO_TCP);
if (listener == INVALID_SOCKET)
{
int lastErr = WSAGetLastError();
cout << "Error = " << lastErr << endl;
}
else
{
cout << "Listener = " << listener << endl;
}
return 0;
}

I found it by myself. I removed the workspace and created a new one. With the new one it runs. I don't know what happend.

Related

C++ socket communication with an external device

So I've an industrial controller here and I wish to communicate with this device using my pc (windows 10), over ethernet tcp/ip.
This external device (controller) is connected directly to my pc via an ethernet cable and both my pc and the external device have a static IP address within the same range.
The manufacturer of this device has its own software with which I can communicate with the device over ethernet tcp/ip, however that is for configuring and programming the device.
I would like to make a small client application that uses sockets to send some command to this controller and should read the response from the device. The device is functioning as a server.
Important to know is that the commands should be sent in hexadecimal form to the device and it responds back with a message in hexadecimal form.
I am coding this in c++ in a visual studio 2022 environment.
So far I can connect to the external device (server), but sending the command and reading the response does not seem it be working.
Another issue is that the error messages are not displayed in the console, even when I add a delay of 2 seconds everywhere.
Below is my code:
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment (lib, "Ws2_32.lib")
#include <iostream>
#include <string.h>
#include <sstream>
#include <WinSock2.h>
#include <WS2tcpip.h>
using namespace std;
int main()
{
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0)
{
cout << " Winsocket connection failed!" << endl;
Sleep(2000);
exit(1);
}
SOCKADDR_IN address;
int addressLen = sizeof(address);
IN_ADDR ipvalue;
address.sin_addr.s_addr = inet_addr("192.168.0.10");
address.sin_port = htons(9100);
address.sin_family = AF_INET;
SOCKET connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(connection, (SOCKADDR*)&address, addressLen) == 0)
{
cout << "Connected" << endl;
Sleep(2000);
}
else
{
cout << "Error connecting to server" << endl;
Sleep(2000);
exit(1);
}
char buffer[4096];
string getInput;
do
{
cout << ">input:";
getline(cin, getInput);
if (getInput.size() > 0)
{
// send message
int sendResult = send(connection, getInput.c_str(), getInput.size() + 1, 0);
if (sendResult != SOCKET_ERROR)
{
// Wait for response from server
ZeroMemory(buffer, 4096);
int bytesReceived = recv(connection, buffer, 4096, 0);
if (bytesReceived)
{
// Response to client
cout << "SERVER> " << string(buffer, 0, bytesReceived) << endl;
Sleep(2000);
}
else
cout << "error " << endl;
Sleep(2000);
}
}
}
while (getInput.size() > 0);
}

C++ TCP send error: Unable to resolve non-existing file '/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c'

I've been trying to make a server in C++. The code compiles and runs, but the function "send" wont work and gives an error as shown in the title. The full error is shown below and is from VSCode.
Unable to open 'send.c': Unable to read file '/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c' (Error: Unable to resolve non-existing file '/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c').
Here is my code. If send() is removed from the code, everything works just fine.
#include <iostream>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
const int PORT = 5000;
const int MAX_CONNECTIONS = 8;
int main(){
const char* openingMessage = "Welcome!\n";
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
int socketS2G = socket(AF_INET, SOCK_STREAM, 0); //AF_INET is IPv4, SOCK_STREAM is TCP and Protocol value for Internet Protocol(IP), which is 0.
if(socketS2G == 0){
std::cout << "Something went wrong!\n";
} else {
//bind address to socket objekt
if(bind(socketS2G, (struct sockaddr *)&address, sizeof(address)) < 0){
std::cout << "Could not bind address and port\n";
} else {
std::cout << "Port now open\n";
//listen to socket
if (listen(socketS2G, 3) < 0){
std::cout << "Can't lisent to socket\n";
} else {
std::cout << "Listening on socket...\n";
//accept requests from clients
socklen_t addresslen = sizeof(address);
while(true){
std::cout << "Ready to accept connection\n";
accept(socketS2G,(struct sockaddr *)&address,&addresslen);
//THE LINE BELOW IS WHERE THINGS BEGIN TO GO WRONG
send(socketS2G, openingMessage, strlen(openingMessage), 0);
}
}
}
}
return 0;
}
The error only occurs when I try to connect to 127.0.0.1 at port 5000. I use the telnet command to connect to my application.
I have a feeling that I might am missing a library, or maybe I am calling the wrong function. I therefore both updated and upgraded my Ubuntu machine.

irsock programming and visual c++ 12

I want develop some irDA sockets applications, I am using:
compiler: visual c++ 2013.
platform: windows 7 x64
It seem s to me that something went wrong:
#include <iostream>
#include <winsock2.h>
#include <af_irda.h>
using std::cout;
using std::endl;
#pragma comment(lib,"ws2_32.lib")
int main()
{
WSADATA wSaData;
WSAStartup(MAKEWORD(2,2),&wSaData);
int sockServ = socket(AF_IRDA, SOCK_STREAM, 0);
if(sockServ == SOCKET_ERROR) //this condition succeeds which means creating socket failed!??
cout<<"Failed to create socket! " << WSAGetLastError() << endl;
DEVICELIST devLst;
devLst. = 0;
int len = sizeof(devLst;);
int rc = getsockopt(sockServ,SOL_IRLMP,IRLMP_ENUMDEVICES,(char*)&devLst,&devLst);
//rc also = -1;
WCE_IAS_QUERY wceIasQuery; // Error: WCE_IAS_QUERY is undefined
//...
return 0;
}
I'd like explanation:
1- why socket creation failed?
2- why getsockopt failed?
3- why WCE_IAS_QUERY is undeclared identifier?
4- what are the pre-requisites for irSock programming?
thank you guys!

VC11 Winsock Application Won't Shut Down

I have an interesting scenario for a winsock app that seemingly will not close. The following is enough code to fully replicate the issue:
#include "stdafx.h"
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <WS2tcpip.h>
#include <MSTcpIP.h>
#include <ws2ipdef.h>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
WSAData wsaStartup;
WSAStartup(MAKEWORD(2, 2), &wsaStartup);
SOCKET s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
addrinfo *result;
addrinfo hint = { 0 };
hint.ai_family = AF_INET6;
int error = getaddrinfo("localhost", "45000", &hint, &result);
if (error || !result) {
cout << "Unable to resolve host. " << WSAGetLastError() << endl;
return 1;
}
error = connect(s, result->ai_addr, result->ai_addrlen);
if (error == SOCKET_ERROR) {
cout << "Unable to connect to host. " << WSAGetLastError() << endl;
} else {
cout << "Connection successful." << endl;
}
freeaddrinfo(result);
closesocket(s);
WSACleanup();
return 0;
}
I have spent numerous hours trying to track the issue down. It seems like getaddrinfo and connect both spawn an nt thread that hangs out, and prevents the app from terminating.
The only important compiler option that I changed here is: Linker->Advanced->EntryPoint where I specified "main". If I get rid of that compiler option, and change the main signature to:
int _tmain(int argc, _TCHAR* argv[])
everything seems to work fine. In my use case, I am fine having the above _tmain function, but I am wondering if anyone has any idea what magic is going on behind the scenes of the _tmain function that is making the app close.
How do I correctly set the entry point for an exe in Visual Studio?
Perhaps you need to provide the correct signature for main() to match what the runtime is expecting.

C++ winsock gives 10038 error on bind()

um, first post here, this place seems to be all over google and i can usally find my solution with having to acually ask a question my self in any site/forums; but if i sweat any more bullets over this ima hunt down whoever developed winsock and shoot them (sorry for the anger i think ive turned over every rock in every corrner of the net with no luck.... breeaatheee.... wheew)
Im new to network programming, but have been working with C++ for the last three years on a hobby level, and also been playing with AS3 recently.
Im trying to write a server (for the client with is the AS3 project im also working on) and as far as i can tell this SOCKET is perfectly fine. im not re-creating it, multi-threading with it, no re-assignment or anything. no funny bisuness. simply trying to set it all up and bind() is spitting out that nasty 10038 right in my face.
Ive looked on MSDN, and i know very well that 10038 means "attempted operation on an invalid socket"; for the life of me i cant see where its invalid.
but enough of my rambling, heres the code: (functions.h is empty, havnt got that far along yet)
//Server for Project7 - Client written in AS3 under FlashDevelop. Developed under and for the Windows Operating System Enviroment
//All connections handled under TCP/IP on port 3011
//Client is URL locked to www.cutdev.com
//Copyright Tyler Buchinski 2012 All Rights Reserved
#include <iostream>
#include "functions.h"
#define WIN32_MEAN_AND_LEAN
#include <winsock2.h>
#include <windows.h>
using namespace std;
int main()
{
const int iReqWinsockVer = 2; // Minimum winsock version required
WSADATA wsaData;
if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
{
// Check if major version is at least iReqWinsockVer
if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
{
SOCKET SocketListen;
SocketListen = (AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(SocketListen == INVALID_SOCKET)
{
cout << "ERROR - could not creaate listening socket." << endl;
system("pause");
return 4;
}
sockaddr_in Listener, Channel1;
Listener.sin_family = AF_INET;
Listener.sin_port = htons(3011);
Listener.sin_addr.S_un.S_addr = INADDR_ANY;
int err = bind(SocketListen,(sockaddr*)(&Listener),sizeof(Listener));
if (!err == 0)
{
cout << "Listener binding failed!" << endl;
cout << err << endl;
cout << WSAGetLastError();
return 3;
}
}
else
{
// Required version not available
cout <<"Required version of Winsock not installed." << endl;
}
// Cleanup winsock
if (!WSACleanup() == 0)
{
// cleanup failed
cout << "WSACleanup Failed!!" << endl;
system("pause");
}
}
else
{
cout << "WSA Startup failed!" << endl;
}
return 0;
}
Thanks in advance for any help!
-Tyler
Error 10038 is WSAENOTSOCK:
An operation was attempted on something that is not a socket.
This error is returned if the descriptor in the s parameter is not a socket.
This happens since you omitted the call to socket() and SocketListen contains the value of the IPPROTO_TCP constant instead of a socket descriptor:
SocketListen = (AF_INET,SOCK_STREAM,IPPROTO_TCP);
should become:
SocketListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);