I created a client server program in c++ console. i run the server and then run the client. The server is not receiving the clients ip address.
here is the server code:
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int recvTimeOutTCP(SOCKET socket, long sec, long usec)
{
struct timeval timeout;
struct fd_set fds;
timeout.tv_sec = sec;
timeout.tv_usec = usec;
FD_ZERO(&fds);
FD_SET(socket, &fds);
return select(0, &fds, 0, 0, &timeout);
}
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ListeningSocket, NewConnection;
SOCKADDR_IN ServerAddr, SenderInfo;
int Port = 7171;
char recvbuff[1024];
int ByteReceived, i, nlen, SelectTiming;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
printf("Server: WSAStartup failed with error %ld.\n", WSAGetLastError());
return 1;
}
else
{
printf("Server: The Winsock DLL found!\n");
printf("Server: The current status is %s.\n\n", wsaData.szSystemStatus);
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 )
{
printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
WSACleanup();
return 1;
}
else
{
printf("Server: The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
%u.%u\n\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
}
// Create a new socket to listen for client connections.
ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors to ensure that the socket is a valid socket.
if (ListeningSocket == INVALID_SOCKET)
{
printf("Server: Error at socket, error code: %ld.\n", WSAGetLastError());
// Clean up
WSACleanup();
// and exit with error
return 1;
}
else
{
printf("Server: socket is ok!\n");
}
// The IPv4 family
ServerAddr.sin_family = AF_INET; //AF_INET tells Winsock we are using the IP address family.
// host-to-network byte order
ServerAddr.sin_port = htons(Port);
// Listen on all interface, host-to-network byte order
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Associate the address information with the socket using bind.
// Call the bind function, passing the created socket and the sockaddr_in
// structure as parameters. Check for general errors.
if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("Server: bind failed! Error code: %ld.\n", WSAGetLastError());
// Close the socket
closesocket(ListeningSocket);
// Do the clean up
WSACleanup();
// and exit with error
return 1;
}
else
{
printf("Server: bind is working\n");
}
// Listen for client connections with a backlog of 5
if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
printf("Server: listen: Error listening on socket %ld.\n", WSAGetLastError());
// Close the socket
closesocket(ListeningSocket);
// Do the clean up
WSACleanup();
// Exit with error
return 1;
}
else
{
printf("Server: listening for connections...\n\n");
}
// Set 10 seconds 10 useconds timeout
SelectTiming = recvTimeOutTCP(ListeningSocket, 10, 10);
switch (SelectTiming)
{
case 0:
// Timed out, do whatever you want to handle this situation
printf("\nServer: Timeout over while waiting client!...");
break;
case -1:
// Error occurred, more tweaking here and the recvTimeOutTCP()...
printf("\nServer: Some error encountered with code number: %ld\n", WSAGetLastError());
break;
default:
{
// Accept a new connection when available. 'while' always true
while(1)
{
// Reset the NewConnection socket to SOCKET_ERROR
// Take note that the NewConnection socket in not listening
NewConnection = SOCKET_ERROR;
// While the NewConnection socket equal to SOCKET_ERROR
// which is always true in this case...
while(NewConnection == SOCKET_ERROR)
NewConnection = accept(ListeningSocket, NULL, NULL);
printf("Server: accept is working...\n");
printf("Server: New client got connected, ready to receive and send data...\n\n");
ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);
// When there is data
if ( ByteReceived > 0 )
{
printf("Server: recv is working....\n");
// Some info on the receiver side...
getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
printf("Server: Receiving IP(s) from client: %s\n", inet_ntoa(ServerAddr.sin_addr));
printf("Server: Receiving port from client: %d\n", htons(ServerAddr.sin_port));
memset(&SenderInfo, 0, sizeof(SenderInfo));
nlen = sizeof(SenderInfo);
getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
printf("Server: Sending IP used: %s\n", inet_ntoa(SenderInfo.sin_addr));
printf("Server: Sending port used: %d\n", htons(SenderInfo.sin_port));
printf("Server: Bytes received: %d\n", ByteReceived);
printf("Server: Those bytes are: \"");
for(i=0;i < ByteReceived;i++)
{
printf("%c", recvbuff[i]);
}
printf("\"");
}
else if ( ByteReceived == 0 )
{
printf("Server: Connection closed!\n");
}
else
{
printf("Server: recv failed with error code: %d\n", WSAGetLastError());
}
}
if( shutdown(NewConnection, SD_SEND) != 0)
printf("\n\nServer: Well, there is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
else
printf("\nServer: shutdown is working...\n");
}
}
}
printf("\n\nServer: The listening socket is timeout...\n");
if(closesocket(ListeningSocket) != 0)
{
printf("Server: Cannot close \"ListeningSocket\" socket. Error code: %ld\n", WSAGetLastError());
}
else
{
printf("Server: Closing \"ListeningSocket\" socket...\n");
}
if(WSACleanup() != 0)
{
printf("Server: WSACleanup failed! Error code: %ld\n", WSAGetLastError());
}
else
{
printf("Server: WSACleanup is working...\n");
}
return 0;
}
and here's the client:
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET SendingSocket;
SOCKADDR_IN ServerAddr, ThisSenderInfo;
unsigned int Port = 7171;
int RetCode;
char sendbuf[1024] = "This is a test string from client";
int BytesSent, nlen;
WSAStartup(MAKEWORD(2,2), &wsaData);
printf("Client: Winsock DLL status is %s.\n", wsaData.szSystemStatus);
SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(SendingSocket == INVALID_SOCKET)
{
printf("Client: socket failed! Error code: %ld\n", WSAGetLastError());
WSACleanup();
return -1;
}
else
{
printf("Client: socket is ok!\n");
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
if(RetCode != 0)
{
printf("Client: connect failed! Error code: %ld\n\n", WSAGetLastError());
closesocket(SendingSocket);
WSACleanup();
return -1;
}
else
{
printf("Client: got connected...\n");
printf("Client: Ready for sending and/or receiving data...\n\n");
}
getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
printf("Client: Server IP(s) used: %s\n", inet_ntoa(ServerAddr.sin_addr));
printf("Client: Server port used: %d\n\n", htons(ServerAddr.sin_port));
BytesSent = send(SendingSocket, sendbuf, strlen(sendbuf), 0);
if(BytesSent == SOCKET_ERROR)
{
printf("Client: send error %ld.\n", WSAGetLastError());
}
else
{
printf("Client: message sent ok - bytes sent: %ld\n", BytesSent);
memset(&ThisSenderInfo, 0, sizeof(ThisSenderInfo));
nlen = sizeof(ThisSenderInfo);
getsockname(SendingSocket, (SOCKADDR *)&ThisSenderInfo, &nlen);
printf("Client: Client IP(s) used: %s\n", inet_ntoa(ThisSenderInfo.sin_addr));
printf("Client: Client port used: %d\n", htons(ThisSenderInfo.sin_port));
printf("Client: Bytes message is: \"%s\"\n", sendbuf);
}
if( shutdown(SendingSocket, SD_SEND) != 0)
{
printf("Client: There is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
}
else
{
printf("\nClient: shutdown working...\n");
}
if(closesocket(SendingSocket) != 0)
{
printf("Client: Cannot close \"SendingSocket\" socket. Error code: %ld\n", WSAGetLastError());
}
else
{
printf("Client: Closing \"SendingSocket\" socket...\n");
}
if(WSACleanup() != 0)
{
printf("Client: WSACleanup failed!...\n");
}
else
{
printf("Client: WSACleanup worked...\n");
}
return 0;
}
a line in the output of the server is: Server: Receiving IP(s) from client: 0.0.0.0
how do i get the IP address of the computer the client is running on?