help with getpeername function


Hi, I have the following code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

int SocketServer::ListenOnPort()
{
   
	WSADATA wsaData;
        SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET;
        struct addrinfo *result = NULL,
                     hints;
	SOCKADDR_IN clientAddr; // Internet address
        IN_ADDR clientIn; // IP address
        int nClientAddrLen;
   
       int iResult;
 
     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
     if (iResult != 0) 
	   { printf("WSAStartup falló. Error no: %d\n", iResult);
         return 1;
       }

    ZeroMemory(&hints, sizeof(hints)); 
    hints.ai_family = AF_INET;       
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;


    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); 
    
	if ( iResult != 0 ) 
	 { printf("getaddrinfo falló. Error no: %d\n", iResult);
       WSACleanup();
       return 1;
     }

    
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) 
	 {  printf("Falló la creación del socket: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
     }

    // Setup the TCP listening socket
    iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen); 
    if (iResult == SOCKET_ERROR) 
	  { printf("Error en la Asociación. Error no: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
     }

    freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("Listening error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

	nClientAddrLen = sizeof(clientAddr);
    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        printf("Error al aceptar la conexión: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    } 
   else
	 { if (getpeername(ClientSocket, (LPSOCKADDR)&clientAddr, &nClientAddrLen) == SOCKET_ERROR)
        {  printf("getpeername() generated error %d", WSAGetLastError());
	    }
	 else 
	    { // copy the four byte IP address into an IP address structure
          memcpy(&clientIn, &clientAddr.sin_addr.s_addr, 4);
          
		  printf("client IP address is %s, port is %d", inet_ntoa(clientIn), ntohs(clientAddr.sin_port));
      
		 	 
	      } 

 
		  } // No longer need server socket
    closesocket(ListenSocket);

 
}



It supposses that my server and client connect using port 2000 so I define DEFAUL_PORT like this

#define DEFAULT_PORT "2000"

the problem is that when i print the the client's ip address and port after
using getpeername, i expected to get 2000 as the port number, but I get a complete different name, I already tried htons and ntohs functions. Could you help me please.?
Hint: Do You want every client to use port 2000? ;)... The listening socket´s port is not the port the connection stays alife on.


Re-Read the descriptions of the functions You are using!
Last edited on
Topic archived. No new replies allowed.