Is it normal in C++ to be constantly converting variable types?
For instance, I want to get the computer name and send that variable through a socket.
I find the function GetComputerName which gives me what I need in TCHAR.
I use the send function and it needs the computername variable in const char.
Is it normal to have to be jumping around like this?
Here is an example of code. Doesn't work because tchar to const char issue.
For instance, I want to get the computer name and send that variable through a socket.
I find the function GetComputerName which gives me what I need in TCHAR.
I use the send function and it needs the computername variable in const char.
Is it normal to have to be jumping around like this?
Here is an example of code. Doesn't work because tchar to const char issue.
Code
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 27015
#define INADDR "127.0.0.1"
int main(void)
{
TCHAR compnameBuf[MAX_COMPUTERNAME_LENGTH + 2];
DWORD compnameBufSize;
compnameBufSize = sizeof compnameBuf - 1;
if (GetComputerName(compnameBuf, &compnameBufSize) == TRUE) {
_tprintf(_T("Your computer name is %s\n"), compnameBuf);
}
WSADATA wsaData;
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
int recvbuflen = DEFAULT_BUFLEN;
const char* sendbuf = "Client sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
clientService.sin_port = htons(DEFAULT_PORT);
//----------------------
// Connect to server.
iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR) {
wprintf(L"connect failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("test");
//----------------------
// Send an initial buffer
char sendcomputer = compnameBuf;
iResult = send(ConnectSocket, compnameBuf, (int)strlen(compnameBuf), 0);
if (iResult == SOCKET_ERROR) {
wprintf(L"send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %d\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// close the socket
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"close failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
WSACleanup();
return(0);
}
