Note that there are some explanatory texts on larger screens.

plurals
  1. POgetpeername sa_data
    primarykey
    data
    text
    <p>I want to get the IP address of the socket that's connected to my server socket. I've included my code.</p> <p>The sa_data doesn't return char[14] of the type "xxx.xxx.xxx.xxx". Instead, it returns <code>[11][1][xxx][xxx][xxx][xxx][][][][][][][]...[]</code>. Can anyone help?</p> <pre><code>int InitialResult; char SendMessage[1024]; char ReceiveMessage[1024]; WSADATA WSAData; addrinfo Hints; addrinfo *Result; SOCKET ListenSocket; SOCKET ClientSocket; ZeroMemory(&amp;Hints, sizeof(Hints)); Hints.ai_family = AF_INET; Hints.ai_socktype = SOCK_STREAM; Hints.ai_protocol = IPPROTO_TCP; Hints.ai_flags = AI_PASSIVE; while(1) { InitialResult = WSAStartup(MAKEWORD(2,2), &amp;WSAData); if (InitialResult != 0) { cout &lt;&lt; "WSA start up failed."; } InitialResult = getaddrinfo(NULL, portnumber, &amp;Hints, &amp;Result); if (InitialResult != 0) { cout &lt;&lt; "Get address information failed."; WSACleanup(); } ListenSocket = socket(Result-&gt;ai_family, Result-&gt;ai_socktype, Result-&gt;ai_protocol); if (ListenSocket == INVALID_SOCKET) { cout &lt;&lt; "Socket initialization failed."; WSACleanup(); } InitialResult = bind(ListenSocket, Result-&gt;ai_addr, (int)Result-&gt;ai_addrlen); if (InitialResult == SOCKET_ERROR) { cout &lt;&lt; "Bind failed." &lt;&lt; portnumber; closesocket(ListenSocket); ListenSocket = INVALID_SOCKET; WSACleanup(); } InitialResult = listen(ListenSocket, SOMAXCONN); if (InitialResult == SOCKET_ERROR) { cout &lt;&lt; "Listen failed." &lt;&lt; portnumber; closesocket(ListenSocket); ListenSocket = INVALID_SOCKET; WSACleanup(); } ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { cout &lt;&lt; "Socket accept failed." &lt;&lt; portnumber; WSACleanup(); } sockaddr test; int a = sizeof(test); cout &lt;&lt; "getpeername() return value : " &lt;&lt; getpeername(ClientSocket, &amp;test, &amp;a) &lt;&lt; endl; cout &lt;&lt; "test.sa_data : " &lt;&lt; test.sa_data; StartReceive(ReceiveMessage, ClientSocket); strcpy(SendMessage,"Congratulations!! You have successfully transfered some data to G DA."); StartSend(SendMessage, ClientSocket); StartSend(portnumber, ClientSocket); InitialResult = shutdown(ClientSocket, SD_SEND); if (InitialResult == SOCKET_ERROR) { cout &lt;&lt; "Shut down failed." &lt;&lt; portnumber; closesocket(ClientSocket); WSACleanup(); } closesocket(ClientSocket); closesocket(ListenSocket); WSACleanup(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload