Hola que tal , estoy desarrollando un código como pasatiempo en el que creo un servidor local y cliente.El servidor unicaménte lo tengo codificado para correr en linux mientras el cliente lo tengo tanto para win y linux, sin embargo, cuando ejecuto el cliente dentro de un SO linux si es posible conectarse caso contrario al de Win en el que me dice que no es posible conectarse. ¿Alguno sabe si es un problema de compatiblidad o simplemente un problema en mi código ?
//SERVIDOR
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <netdb.h> //Define hostent struct
#include <unistd.h> //close socket
#include <string.h>
#define BUFFER 1024
using namespace std;
int main(int argc, char **argv)
{
//Create socket
int listening;
listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == -1)
{
cout << "Can't create socket" << endl;
return 0;
}
//Set the server
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = INADDR_ANY;
//Assign to server a unique telephone number
bind(listening, (struct sockaddr *)&server, sizeof(server));
//Listening ...
cout << "Waiting for connections ... " << endl;
listen(listening, SOMAXCONN);
//Wait for connections
struct sockaddr_in client;
int sizeClient = sizeof(client);
//Accept client
int clientSocket = accept(listening, (struct sockaddr *)&client, (socklen_t *)&sizeClient);
if (clientSocket == -1)
{
cout << "Can't connect with the client" << endl;
return 0;
}
char welcome[BUFFER];
memset(welcome, 0, BUFFER);
strcpy(welcome, "Welcome");
send(clientSocket, welcome, BUFFER, 0);
cout << "Connected!" << endl;
bool bandera = true;
while (bandera)
{
cout << "(*)";
cin.getline(welcome, BUFFER);
if (strcmp(welcome, "SHUTDOWN") == 0)
{
send(clientSocket, welcome, BUFFER, 0);
bandera = false;
}
else
{
send(clientSocket, welcome, BUFFER, 0);
}
}
close(listening);
}
//CLIENTE
#if defined _WIN32
#include <iostream>
using namespace std;
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int inet_pton(int af, const char *src, void *dst);
#else
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <string.h>
#include <unistd.h> //close socket
#endif
#define BUFFER 2048
using namespace std;
int main()
{
#if defined(_WIN32)
{
WSADATA winsock;
WORD word = MAKEWORD(2, 2);
int winStatus = WSAStartup(word, &winsock);
if (winStatus != 0)
{
cout << "Can't intialize Winsock on windows" << endl;
return 0;
}
}
#endif
int socket_ = socket(AF_INET, SOCK_STREAM, 0);
if (socket_ == -1)
{
cout << "Can't create the socket" << endl;
return 0;
}
//Set socket
sockaddr_in client;
client.sin_port = htons(8080);
client.sin_family = AF_INET;
#if (_WIN32)
string ipAdress="127.0.0.1";
inet_pton(AF_INET, ipAdress.c_str(), &client.sin_addr);
#else
client.sin_addr.s_addr = inet_addr("127.0.0.1");
#endif
//Connect
int connecting = connect(socket_, (struct sockaddr *)&client, sizeof(client));
if (connecting == -1)
{
cout << "You can't connect" << endl;
return 0;
}
char rcvd[BUFFER];
memset(rcvd, 0, BUFFER);
recv(socket_, rcvd, BUFFER, 0);
cout << rcvd << endl;
bool bandera = true;
while (bandera)
{
memset(rcvd, 0, BUFFER);
recv(socket_, rcvd, BUFFER, 0);
if (strcmp(rcvd, "SHUTDOWN") == 0)
{
#if defined(_WIN32)
WSACleanup();
#endif
//close(socket_);
bandera = false;
cout << "The connection was closed" << endl;
}
else
cout << "*) " << rcvd << endl;
}
}
int inet_pton(int af, const char *src, void *dst)
{
#if (_WIN32)
struct sockaddr_storage ss;
int size = sizeof(ss);
char src_copy[INET6_ADDRSTRLEN + 1];
ZeroMemory(&ss, sizeof(ss));
/* stupid non-const API */
strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
src_copy[INET6_ADDRSTRLEN] = 0;
if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
{
switch (af)
{
case AF_INET:
*(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
return 1;
case AF_INET6:
*(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
return 1;
}
}
#endif
return 0;
}
MOD: Modificadas las etiquetas de Código GeSHi para el lenguaje C++
Al servidor le da igual desde donde se le mande la petición. Asique tiene que ser problema del código de tu cliente.
Busca algún ejemplo de cliente por la web que te funcione. Yo suelo usar ncat para debuggear este tipo de aplicaciones.