服务器没有收到客户端的IP地址

服务器没有收到客户端的IP地址

本文介绍了服务器没有收到客户端的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++控制台中创建了一个客户端服务器程序。我运行服务器,然后运行客户端。服务器未收到客户端IP地址。



这里是服务器代码:

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;
    // Setup fd_set structure
    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;
            // Receiving part
            char recvbuff[1024];
            int ByteReceived, i, nlen, SelectTiming;

            // Initialize Winsock version 2.2
            if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
            {
                 printf("Server: WSAStartup failed with error %ld.\n", WSAGetLastError());
                 // Exit with error
                 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 )
            {
                 // Tell the user that we could not find a usable WinSock DLL
                 printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
                 // Do the clean up
                 WSACleanup();
                 // and exit with error
                 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("\"");
										}
										// No data
										else if ( ByteReceived == 0 )
										{
											printf("Server: Connection closed!\n");
										}
										// Others
										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");



								//	if( recvTimeOutTCP(ListeningSocket, 10, 0) == 0)
										//break;
								}
							}
			}


            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;
            // Server/receiver address
            SOCKADDR_IN ServerAddr, ThisSenderInfo;
            // Server/receiver port to connect to
            unsigned int Port = 7171;
            int RetCode;
            // Be careful with the array bound, provide some checking mechanism...
            char sendbuf[1024] = "This is a test string from client";
            int BytesSent, nlen;

            // Initialize Winsock version 2.2
            WSAStartup(MAKEWORD(2,2), &wsaData);
            printf("Client: Winsock DLL status is %s.\n", wsaData.szSystemStatus);

            // Create a new socket to make a client connection.
            // AF_INET = 2, The Internet Protocol version 4 (IPv4) address family, TCP protocol
            SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if(SendingSocket == INVALID_SOCKET)
            {
				printf("Client: socket failed! Error code: %ld\n", WSAGetLastError());
				// Do the clean up
				WSACleanup();
				// Exit with error
				return -1;
            }
            else
			{
				printf("Client: socket is ok!\n");
			}

            // Set up a SOCKADDR_IN structure that will be used to connect
            // to a listening server on port 5150. For demonstration
            // purposes, let's assume our server's IP address is 127.0.0.1 or localhost

            // IPv4
            ServerAddr.sin_family = AF_INET;
            // Port no.
            ServerAddr.sin_port = htons(Port);
            // The IP address
            ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

            // Make a connection to the server with socket SendingSocket.
            RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
            if(RetCode != 0)
            {
				printf("Client: connect failed! Error code: %ld\n\n", WSAGetLastError());
				// Close the socket
				closesocket(SendingSocket);
				// Do the clean up
				WSACleanup();
				// Exit with error
				return -1;
            }
            else
            {
				printf("Client: got connected...\n");
				printf("Client: Ready for sending and/or receiving data...\n\n");
            }

            // At this point you can start sending or receiving data on
            // the socket SendingSocket.

            // Some info on the receiver side...
            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));

            // Sends some data to server/receiver...
            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);
				// Some info on this sender side...
				// Allocate the required resources
				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");
			}
            // When you are finished sending and receiving data on socket SendingSocket,
            // you should close the socket using the closesocket API. We will
            // describe socket closure later in the chapter.
            if(closesocket(SendingSocket) != 0)
			{
				printf("Client: Cannot close \"SendingSocket\" socket. Error code: %ld\n", WSAGetLastError());
			}
            else
			{
				printf("Client: Closing \"SendingSocket\" socket...\n");
			}

            // When your application is finished handling the connection, call WSACleanup.
            if(WSACleanup() != 0)
			{
				printf("Client: WSACleanup failed!...\n");
			}
            else
			{
				printf("Client: WSACleanup worked...\n");
			}

            return 0;
}




服务器输出中的
a行是:服务器:从客户端接收IP:0.0.0.0

如何获取运行客户端的计算机的IP地址?



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?

推荐答案


这篇关于服务器没有收到客户端的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:37