C++ socket client/server server do not response to client - c++

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;

Related

Can connect client to server socket linux

I have code the client socket like this:
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <string>
#include<unistd.h> //close
#include<string>
#include<iostream>
using namespace std;
int main(int argc, char* argv[]) {
//create a socket
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
int port;
string IPaddress;
cout << "Input port(ex 9999):";
cin >> port;
cin.ignore(1, '\n');
cout << "Input IP address(ex 127.0.0.1):" << endl;
getline(cin, IPaddress, '\n');
//specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(IPaddress);
server_address.sin_port = htons(port);
}
I try to connect my server by input Port and ip address but when I convert to linux. It perform inet_addr’ was not declared in this scope; . How can I fix this problem?

Visual C++ program complains about net framework

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.

C++ Network program runs but give no output

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;

Target address reversed

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.

Connecting to imap.google with SSL

I`m trying to make an email client but it doesn't work :/ Im trying to connect to google imap with SSL (without SSL i can make it) Code:
#include "StdAfx.h"
#include "openssl/ssl.h"
#include "iostream"
#include <cstdio>
#include <iostream>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <windows.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define BUFSIZE 1024
char buf[BUFSIZE];
char *msg;
WSADATA wsda;
int sock;
struct hostent *host;
struct sockaddr_in server_addr;
short int s_port = 993;
const char *s_ipaddr = "74.125.77.109";
int SSL_library_init();
SSL_METHOD *meth;
int main () {
SSL_load_error_strings();
SSL_library_init();
//RAND_seed(buf, BUFSIZE);
SSL_CTX *sslContext = SSL_CTX_new(SSLv23_client_method());
if (sslContext == NULL)
{
cout << "err\n";
}
SSL *sslConnection = SSL_new(sslContext);
if(sslConnection == NULL)
{
cout << "err\n";
}
WSAStartup(MAKEWORD(2,2), &wsda);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(s_port);
server_addr.sin_addr.s_addr = inet_addr(s_ipaddr);
if(server_addr.sin_addr.s_addr == INADDR_NONE)
{
host = NULL;
host = gethostbyname(s_ipaddr);
if(host == NULL)
{
return false;
}
memcpy(&server_addr.sin_addr, host->h_addr_list[0], host->h_length);
}
connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
SSL_set_fd(sslConnection, sock);
SSL_connect(sslConnection);
cout << "Connecting:\n";
SSL_read(sslConnection, buf, sizeof(buf)-1);
cout << buf << "\n";
cout << "Logging-in:\n";
msg = "a1 login emailapp123#gmail.com PASS\n";
SSL_write(sslConnection, msg, strlen(msg));
SSL_read(sslConnection, buf, sizeof(buf)-1);
cout << buf << "\n";
cout << "INBOX\n";
msg = "a2 SELECT INBOX\n";
SSL_write(sslConnection, msg, strlen(msg));
SSL_read(sslConnection, buf, sizeof(buf)-1);
cout << buf << "\n";
system("pause");
}
And thats what i receive:
Connecting:
* OK Gimap ready for requests from 195.150.156.162 y48if37710eei.45
Logging-in:
* CAPABILITY IMAP4rev1 UNSELECT LITERAL+ IDLE NAMESPACE QUOTA ID XLIST CHILDREN
X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
a1 OK emailapp123#gmail.com authenticated (Success)
INBOX
What should I do to make the connection right?
Thanks for every advice :-)
Chris
You need to terminate all commands sent to an imap server with both a carriage return and a line feed. Send a2 SELECT INBOX\r\n and it should work.
seems you are not terminating line command with \r\n. Please refer RFC
http://www.faqs.org/rfcs/rfc3501.html