I wrote a simple client program in Visual C++ 2010 which connects to a client using winsock. When I try to run this program on another computer, it complains about missing Net Framework.
I wonder why that would be the case? What's in my code that requires net framework?
The error message:
application, you must first install one of the following versions of
the .NET Framework v4.0...etc
Here's my code
#pragma once
#pragma comment(lib, "Ws2_32.lib")
#include "stdafx.h"
#include <sdkddkver.h>
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
#include <time.h>
#include <cstring>
#include <sstream>
#define SCK_VERSION2 0x020
using namespace std;
void main() {
long Successful;
WSAData WinSockData;
WORD DLLVersion;
DLLVersion = MAKEWORD(2,1);
Successful = WSAStartup(DLLVersion, &WinSockData);
int sd,rcv,i,myint = 1;
hostent *host = gethostbyname("localhost");
char * myhostadd = inet_ntoa (*((struct in_addr *) host->h_addr_list[0]));
string memzi2,memzi,Converter;
char Message[200],tell[200] = "haa";
SOCKADDR_IN Address;
SOCKET sock;
sock = socket(AF_INET,SOCK_STREAM,NULL);
Address.sin_addr.s_addr = inet_addr(myhostadd);
Address.sin_family = AF_INET;
Address.sin_port = htons(7177);
cout << "Connecting to server...";
Successful = connect(sock, (SOCKADDR*)&Address, sizeof(Address));
u_long iMode=1;
ioctlsocket(sock,FIONBIO,&iMode);
if (Successful == 0) {
cout << "Connected. "<< endl;
for (;;++i) {
std::stringstream convert2;
convert2 << myint;
memzi2 = convert2.str();
std::cout << "Client: " << memzi2 << std::endl;
const char * c = memzi2.c_str();
sd = send(sock, c, sizeof(tell), NULL);
cout << "Server: ";
rcv = recv(sock,Message,sizeof(Message),NULL);
Converter = Message;
cout << Converter << endl;
std::stringstream convert1(Converter);
convert1 >> myint;
if (myint > 5000) {
myint = 1;
}
++myint;
}
closesocket(sock);
}
else cout << "Failed." << endl;
cout << "\n\n\t";
system("pause");
exit(1);
}
Thanks in advance!
Can be a simple reason, it will be using C++ CLI, i.e. common language runtime. Go to project properties and fix it up, it will not show any more.
Related
I'm new to C++ and am trying to setup a connection to a remote server but having problems getting it to work. Spec: Ubuntu 16.04, pre-installed g++ compiler and when I run the following code it returns "pre-standard C++":
if( __cplusplus == 201103L ) std::cout << "C++11\n" ;
else if( __cplusplus == 19971L ) std::cout << "C++98\n" ;
else std::cout << "pre-standard C++\n" ;
My code is as follows:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
using namespace std;
int main() {
int client;
int portNum = 80;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
const char ip[] = "216.58.210.36"; //google ip for test connection
const char req[] = "GET / HTTP/1.1\nHost: www.google.com"; //test
char res[bufsize];
struct sockaddr_in server_addr;
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0) {
cout << "\nError establishing socket..." << endl;
exit(1);
}
cout << "\n=> Socket client has been created..." << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);
inet_aton(ip, &server_addr.sin_addr);
if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0){
cout << "=> Connection to the server port number: " << portNum << endl;
}
send(client, req, bufsize, 0);
cout << "=> Awaiting confirmation from the server..." << endl;
recv(client, buffer, bufsize, 0);
cout << "=> Connection confirmed, response:" << buffer << endl;
cout << res << endl;
close(client);
return 0;
}
The client is created and the socket connects but the code hangs on the call to recv() and no response is received. I'm assuming that's because the request I'm sending is in the wrong format/data type/etc. Can anyone advise where I'm going wrong? Cheers!
You haven't sent a complete HTTP request so the server is waiting for more data.
You need to use \r\n as your line separator and your request has to end in a blank line:
const char req[] = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
Plus as others have commented you need to check for errors.
You are also sending 1024 bytes of data from a buffer which is much smaller.
I'm working on a Project in c++ that use Sockets to get data from a server and work with it.
I have a problem with that, because when i receive a very long message from the server, my Project can't read it because it is divided in two parts and i never received it or just the last part.
Can someone help me to solve this?
Thanks
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <WinSock2.h> //Esta puta da problemas, mejor usar #include <Windows.h>
#include <queue>
#include <mutex>
#include <regex>
#include <process.h>
#pragma comment(lib, "ws2_32.lib")
//Datos de la Socket
int st = 1000;
int sendCTR = 10000;
void main() {
//start sockets
WSADATA wsaData;
if (::WSAStartup(MAKEWORD(2, 2), &wsaData) && wsaData.wVersion != MAKEWORD(2, 2)) {
std::cout
<< "Cannot start up socket library: " << ::WSAGetLastError() << std::endl;
std::getchar();
exit(EXIT_FAILURE);
}
sockaddr_in gameAddr;
gameAddr.sin_family = AF_INET;
gameAddr.sin_addr.S_un.S_addr = inet_addr(NT_GAME_SERVER_IP);
gameAddr.sin_port = htons(NT_GAME_SERVER_PORT);
gameSocket = socket(AF_INET, SOCK_STREAM, 0);
if (connect(gameSocket, (SOCKADDR*)&gameAddr, sizeof(gameAddr)) == SOCKET_ERROR) {
std::cout
<< "Cannot connect to GameServer (" << NT_GAME_SERVER_IP << ":" << NT_GAME_SERVER_PORT << "), retrying..." << std::endl;
goto GAME;
}
Send(gameSocket, std::to_string(Session));
Sleep(500);
Send(gameSocket, NT_USER, Session);
Sleep(300);
Send(gameSocket, NT_PSSW, Session);
long long lastPulse = time(NULL), pulseCounter = 0;
Sleep(300);
//log packets
std::vector<unsigned char> gameBuffer(0x4000);
int gameRecvLen;
ses = Session;
std::thread t(Order);
while ((gameRecvLen = recv(gameSocket, (char*)&gameBuffer.front(), gameBuffer.size(), NULL)) > 0 && gameRecvLen < 0x4000) {
for each(std::string gamePacket in decryptit(gameBuffer, gameRecvLen)) {
if (gamePacket == "") continue; //Null stuff...
std::vector<std::string> Packet = Crypto::split(gamePacket, ' ');
//MAIN STUFF
}
}
}
I am beginner in socket programming and I have problem with my client/server program, client can't connect to server. I have no idea why, please help. I want to fined out where is the problem.
Server:
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <stdlib.h>
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
WSAData WinSockData;
WORD Version = MAKEWORD(2,1);
long SUCESSFUL;
SUCESSFUL = WSAStartup(Version,&WinSockData);
SOCKADDR_IN ADDRESS;
int AdresSize = sizeof(ADDRESS);
SOCKET sock_LISTEN;
SOCKET sock_CONNECT;
sock_CONNECT = socket(AF_INET,SOCK_STREAM,NULL);
ADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1");
ADDRESS.sin_family = AF_INET;
ADDRESS.sin_port = htons(27015);
sock_LISTEN = socket(AF_INET,SOCK_STREAM,NULL);
bind(sock_LISTEN,(SOCKADDR*)&ADDRESS,sizeof(ADDRESS));
for(;;)
{
listen(sock_LISTEN,SOMAXCONN);
cout << "Waiting for connections..." << endl;
if(sock_CONNECT = accept(sock_LISTEN,(SOCKADDR*)&ADDRESS,&AdresSize))
{
cout << "Conection was found" << endl;
SUCESSFUL = send(sock_CONNECT,"Hello",5,NULL);
}
}
return 0;
}
Client:
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <stdlib.h>
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
WSAData WinSockData;
WORD DLLVersion;
DLLVersion = MAKEWORD(2,1);
long SUCESSFUL;
SUCESSFUL = WSAStartup(DLLVersion,&WinSockData);
string RESPONSE;
string CONVERTER;
char MESSAGE[200];
SOCKADDR_IN ADDRESS;
SOCKET sock;
sock = socket(AF_INET,SOCK_STREAM,NULL);
ADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1");
ADDRESS.sin_family = AF_INET;
ADDRESS.sin_port = htonl(27015);
cout << "Do You want to connect to this server (Y/N)" << endl;
cin >> RESPONSE;
RESPONSE[0] = tolower(RESPONSE[0]);
if(RESPONSE == "n")
{
cout << "Quiting" <<endl;
}else if (RESPONSE == "y")
{
connect(sock,(SOCKADDR*)&ADDRESS,sizeof(ADDRESS));
SUCESSFUL = recv(sock,MESSAGE,sizeof(MESSAGE),NULL);
CONVERTER = MESSAGE;
cout << CONVERTER << endl;
}else
{
cout << "Illegal response" <<endl;
}
return 0;
}
This looks odd
ADDRESS.sin_port = htons(27015);
and this looks even odder
ADDRESS.sin_port = htonl(27015);
Use htons for both.
If that doesn't work, just write
ADDRESS.sin_port = 27015;
My simple program gives no errors through the compiler and runs fine but it does not give the output it is supposed too until someone is connected. I have done a good bit of research and editing but can not figure it out.Also how do I let more than one person connect? Any help to get this to work would be appreciated. Thanks in advance!!! Code is below.
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
char msg[20];
using namespace std;
int main(int argc, char *argv[])
{
cout << "Made it to main!";
int listener_d = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in name;
name.sin_family = PF_INET;
name.sin_port = (in_port_t)htons(30000);
name.sin_addr.s_addr = INADDR_ANY;
if(bind (listener_d, (struct sockaddr *) &name, sizeof(name)) == -1)
{
cout << "Can't bind the port!";
}
else
{
cout << "The port has been bound.";
}
listen(listener_d, 10);
cout << "Waiting for connection...";
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size);
cin >> msg;
send(connect_d, msg, strlen(msg), 0);
}
return 0;
}
Maybe you should try flushing the output.
std::cout << "Waiting for connection..." << std::flush;
I use to code in Python. Now I'm trying C++. When I run the program I see the target address (w/ Wireshark) reverse, even if I use htonl. In Python this same program worked fine.
Follow the code. At the bottom I printed the result.
I'm using Ubuntu 12.04LTS and g++ (Ubuntu/Linaro 4.6.3).
//UdpClient.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <typeinfo>
using namespace std;
int main(int argc, char* argv[])
{
int s, p, rb,rs;
int bytesend;
char buf[1024];
int len;
char ent[16];
char Porta[5];
unsigned long EndServ;
struct sockaddr_in UdpServer, UdpClient;
int UdpServerLen = sizeof(UdpServer);
//do text
string msg("The quick brown fox jumps over the lazy dog\n");
len = msg.copy(buf, msg.size(), 0);
buf[len] = '\0';
//do socket
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1){
cout << "No socket done\n";
}
else {
cout << "Socket done\n";
}
//populate UdpClient struct
UdpClient.sin_family = AF_INET;
UdpClient.sin_addr.s_addr = INADDR_ANY;
UdpClient.sin_port = 0;
//populate UdpServer struct
UdpServer.sin_family = AF_INET;
UdpServer.sin_addr.s_addr = inet_addr(argv[1]);
//check if addres is correct
cout << "ServerAddress: " << hex << UdpServer.sin_addr.s_addr << endl;
UdpServer.sin_port = htons(atoi(argv[2]));
//bind socket
rb = bind(s, (struct sockaddr *)&UdpClient, sizeof(UdpClient));
if (rb == 0){
cout << "Bind OK!\n";
}
else {
cout << "Bind NOK!!!\n";
close(s);
exit(1);
}
//send text to Server
cout << "UdpServSiz: " << sizeof(UdpServer) << endl;
rs = sendto(s, buf, 1024, 0, (struct sockaddr *)&UdpServer, sizeof(UdpServer));
if (rs == -1){
cout << "Message NOT sent!!!\n";
}
else {
cout << "Message SENT!!!\n";
}
close(s);
return 0;
}
/*
edison#edison-AO532h:~/CmmPGMs$ ./UdpClient 127.0.0.1 6789
Socket done
ServerAddress: 100007f (using htonl or not!!)
Bind OK!
Message SENT!!!
edison#edison-AO532h:~/CmmPGMs$
*/
Sounds like you're on ARM (Linaro)? In which case the endianness of the processor matches network order, so htonl and ntohl basically do nothing.