Сервер:
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
using namespace std;
int main()
{
// Create a socket
int listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == -1)
{
cerr << "Can't create a socket! Quitting" << endl;
return -1;
}
// Bind the ip address and port to a socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(4444);
inet_pton(AF_INET, "127.0.0.1", &hint.sin_addr);
bind(listening, (sockaddr*)&hint, sizeof(hint));
// Tell Winsock the socket is for listening
listen(listening, SOMAXCONN);
// Wait for a connection
sockaddr_in client;
socklen_t clientSize = sizeof(client);
int clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
char host[NI_MAXHOST]; // Client's remote name
char service[NI_MAXSERV]; // Service (i.e. port) the client is connect on
memset(host, 0, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);
memset(service, 0, NI_MAXSERV);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
cout << host << " connected on port " << service << endl;
}
else
{
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
cout << host << " connected on port " << ntohs(client.sin_port) << endl;
}
// Close listening socket
close(listening);
// While loop: accept and echo message back to client
char buf[4096];
while (true)
{
memset(buf, 0, 4096);
// Wait for client to send data
int bytesReceived = recv(clientSocket, buf, 4096, 0);
if (bytesReceived == -1)
{
cerr << "Error in recv(). Quitting" << endl;
break;
}
if (bytesReceived == 0)
{
cout << "Client disconnected " << endl;
break;
}
cout << string(buf, 0, bytesReceived) << endl;
// Echo message back to client
send(clientSocket, buf, bytesReceived + 1, 0);
}
// Close the socket
close(clientSocket);
return 0;
}
Клиент:
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
using namespace std;
int main()
{
// Create a socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return 1;
}
// Create a hint structure for the server we're connecting with
int port = 4444;
string ipAddress = "127.0.0.1";
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
// Connect to the server on the socket
int connectRes = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connectRes == -1)
{
return 1;
}
// While loop:
char buf[4096];
string userInput;
do {
// Enter lines of text
cout << "> ";
getline(cin, userInput);
// Send to server
int sendRes = send(sock, userInput.c_str(), userInput.size() + 1, 0);
if (sendRes == -1)
{
cout << "Could not send to server! Whoops!\r\n";
continue;
}
// Wait for response
memset(buf, 0, 4096);
int bytesReceived = recv(sock, buf, 4096, 0);
if (bytesReceived == -1)
{
cout << "There was an error getting response from server\r\n";
}
else
{
// Display response
cout << "SERVER> " << string(buf, bytesReceived) << "\r\n";
}
} while(true);
// Close the socket
close(sock);
return 0;
}
Если в параметрах сокета указать локальный IP 127.0.0.1 то все работает.
А при попытке подключится к этому серверу на другом пк(указывается IP) связи не происходит.
В чем проблемма?
Cпасибо за помощь!