Vraag & Antwoord

Programmeren

Float naar char*

10 antwoorden
  • Ik heb een code op gametutorials.com gevonden, maar die heb ik wat aangepast maar nu werkt die niet. Hier is de source: [code:1:40034b3144] int startupClient(unsigned short port, const char* serverName); void shutdownClient(int clientSocket); #include <winsock2.h> #include <stdio.h> void main() { printf("Welcome to redKlyde's Networking Tutorials!\n"); printf("Tutorial #5 : Multiple Hosts - ClientSide\n\n"); int mySocket; mySocket = startupClient(7654, "localhost"); if (mySocket == -1) { shutdownClient(mySocket); return; } int nBytes; float buffer; for (;;) { unsigned long messageSize = sizeof(buffer); messageSize = htonl(messageSize); if ((nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } messageSize = ntohl(messageSize); if ((nBytes = send(mySocket, (char*)buffer, messageSize, 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } } shutdownClient(mySocket); } int startupClient(unsigned short port, const char* serverName) { int error; WSAData wsaData; if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) { printf("Could Not Start Up Winsock!\n"); return -1; } int mySocket = socket(AF_INET, SOCK_STREAM, 0); if (mySocket == SOCKET_ERROR) { printf("Error Opening Socket!\n"); return -1; } struct hostent *host_entry; if ((host_entry = gethostbyname(serverName)) == NULL) { printf("Could not find host!\n"); } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr; if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { printf("Error connecting to server!\n"); } printf("Client Started\n"); return mySocket; } void shutdownClient(int clientSocket) { closesocket(clientSocket); WSACleanup(); printf("Client Shutdown\n"); } [/code:1:40034b3144] En dan geeft die de fout: [code:1:40034b3144] error C2440: 'type cast' : cannot convert from 'float' to 'char *' There is no context in which this conversion is possible [/code:1:40034b3144] Op deze regel: [code:1:40034b3144] if ((nBytes = send(mySocket, (char*)buffer, messageSize, 0)) == SOCKET_ERROR) { [/code:1:40034b3144] Alvast bedankt :)
  • Moet dat niet zijn: (char*)(&float) omdat een float geen pointer naar een geheugenwaarde is, maar zelf de waarde is. Met & ervoor (of erachter?) krijgt je het adres van deze float, vervolgens kan je dat waarschijnlijk wel casten naar een char*. Denk ik.
  • Ik denk dat de foutmelding het al zegt: je kan geen string maken van een float. Misschien wel als je Real gebruikt oid.
  • Ok, zal het proberen, maar het is de bedoeling om een waarde in float te versturen. EDIT: Het werkt niet :(
  • Wat wil je eigenlijk versturen? De precieze 8 (4?) bytes zoals je float in het geheugen staat, of de string: "3.543345"?
  • Precies wat in het geheugen staat.
  • Dit: [code:1:d4ca9de696]#include <stdio.h> void test(char* enzo, int length) { int i; printf("size is %i\n", length); for (i=0; i<length; i++) { printf("char %i is %i\n", i, (int)(unsigned char)(enzo[i])); } } int main() { float dus; dus = 35234.33; test((char*)(&dus), sizeof(double)); return 0; }[/code:1:d4ca9de696] wat sterk lijkt op wat ik zei, werkt: [code:1:d4ca9de696]size is 4 char 0 is 71 char 1 is 9 char 2 is 162 char 3 is 84[/code:1:d4ca9de696] Edit: ik zei in m'n eerste reactie: (char*)(&float) maar bedoelde natuurlijk (char*)(&buffer)...
  • Het werkt niet (of ik doe het verkeerd), maar dit is de code: [b:d28b603a67]Client:[/b:d28b603a67] [code:1:d28b603a67] int startupClient(unsigned short port, const char* serverName); void shutdownClient(int clientSocket); #include <winsock2.h> #include <stdio.h> #include <iostream.h> void main() { printf("Welcome to redKlyde's Networking Tutorials!\n"); printf("Tutorial #5 : Multiple Hosts - ClientSide\n\n"); int mySocket; mySocket = startupClient(7654, "localhost"); if (mySocket == -1) { shutdownClient(mySocket); return; } int nBytes; float buffer = 213; for (;;) { unsigned long messageSize = sizeof(buffer); messageSize = htonl(messageSize); if ((nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } messageSize = ntohl(messageSize); if ((nBytes = send(mySocket, (char*)(&buffer), messageSize, 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } } shutdownClient(mySocket); } int startupClient(unsigned short port, const char* serverName) { int error; WSAData wsaData; if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) { printf("Could Not Start Up Winsock!\n"); return -1; } int mySocket = socket(AF_INET, SOCK_STREAM, 0); if (mySocket == SOCKET_ERROR) { printf("Error Opening Socket!\n"); return -1; } struct hostent *host_entry; if ((host_entry = gethostbyname(serverName)) == NULL) { printf("Could not find host!\n"); } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr; if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { printf("Error connecting to server!\n"); } printf("Client Started\n"); return mySocket; } void shutdownClient(int clientSocket) { closesocket(clientSocket); WSACleanup(); printf("Client Shutdown\n"); } [/code:1:d28b603a67] [b:d28b603a67]Server:[/b:d28b603a67] [code:1:d28b603a67] #include <winsock2.h> #include <stdio.h> int startupServerForListening(unsigned short port); void shutdownServer(int socket); HANDLE threadHandle; HANDLE mutexHandle; FD_SET masterSet; bool gQuitFlag = false; void acceptingThreadProcedure(int* serverSocket) { int mySocket = *serverSocket; for (;;) { unsigned int clientSocket = accept(mySocket, 0, 0); if (clientSocket == SOCKET_ERROR) { printf("Accept Failed!\n"); gQuitFlag = true; return; } else { WaitForSingleObject(mutexHandle, INFINITE); FD_SET(clientSocket, &masterSet); ReleaseMutex(mutexHandle); printf("client on %d connected\n", clientSocket); } } } void main() { printf("Welcome to redKlyde's Networking Tutorials!\n"); printf("Tutorial # 5 : Multiple Hosts - ServerSide\n\n"); int serverSocket; serverSocket = startupServerForListening(7654); if (serverSocket == -1) { printf("Network Startup Failed!\nProgram Terminating\n"); return; } mutexHandle = CreateMutex(NULL, false, NULL); if (mutexHandle == NULL) { printf("Error creating mutex\n"); shutdownServer(serverSocket); return; } int threadId; threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptingThreadProcedure, &serverSocket, 0, (LPDWORD)&threadId); if (threadHandle == NULL) { printf("Could not start acceptance thread\n"); shutdownServer(serverSocket); return; } Sleep(100); FD_ZERO(&masterSet); for (;;) { if (gQuitFlag) { break; } WaitForSingleObject(mutexHandle, INFINITE); FD_SET pollingSet = masterSet; // unlock the mutex ReleaseMutex(mutexHandle); if (pollingSet.fd_count == 0) { continue; } timeval waitTime; waitTime.tv_sec = 0; waitTime.tv_usec = 0; int result = select(pollingSet.fd_count, &pollingSet, NULL, NULL, &waitTime); if (result == 0) { continue; } if (result == SOCKET_ERROR) { printf("Error in select()\n"); continue; } for (unsigned int i = 0; i < pollingSet.fd_count; i++) { unsigned int clientSocket = pollingSet.fd_array[i]; int nBytes; float buffer; unsigned long messageSize; nBytes = recv(clientSocket, (char*)&messageSize, sizeof(messageSize), 0); if (nBytes == SOCKET_ERROR) { int error = WSAGetLastError(); if (error == WSAECONNRESET) { WaitForSingleObject(mutexHandle, INFINITE); FD_CLR(clientSocket, &masterSet); ReleaseMutex(mutexHandle); closesocket(clientSocket); printf("client on %d disconnected\n", clientSocket); continue; } else { printf("Recv Failed!\n"); gQuitFlag = true; break; } } if (nBytes == 0) { WaitForSingleObject(mutexHandle, INFINITE); FD_CLR(clientSocket, &masterSet); ReleaseMutex(mutexHandle); closesocket(clientSocket); printf("client on %d disconnected\n", clientSocket); continue; } messageSize = ntohl(messageSize); nBytes = recv(clientSocket, (char*)(&buffer), messageSize, 0); if (nBytes == SOCKET_ERROR) { printf("Recv Failed!\n"); gQuitFlag = true; break; } printf("Message Received from client on %d : %s\n", clientSocket, buffer); } } shutdownServer(serverSocket); printf("Press Enter to Exit ...\n"); getchar(); } int startupServerForListening(unsigned short port) { int error; WSAData wsaData; if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) { printf("Could Not Start Up Winsock!\n"); return -1; } int mySocket = socket(AF_INET, SOCK_STREAM, 0); if (mySocket == SOCKET_ERROR) { printf("Error Opening Socket!\n"); return -1; } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = INADDR_ANY; if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { printf("Bind Failed!\n"); closesocket(mySocket); return -1; } if (listen(mySocket, 5) == SOCKET_ERROR) { printf("Listen Failed!\n"); closesocket(mySocket); return -1; } printf("Server Started\n"); return mySocket; } void shutdownServer(int socket) { WaitForSingleObject(threadHandle, INFINITE); CloseHandle(threadHandle); CloseHandle(mutexHandle); closesocket(socket); WSACleanup(); printf("Server Shutdown\n"); } [/code:1:d28b603a67] Ik heb bij de client als voorbeeld 213 gedaan, want als ik 213.125 doe bvb dan crasht de server. En als ik het zo laat, staat er in de server: [b:d28b603a67]Message received from client on 84: <null>[/b:d28b603a67]
  • Je hebt een float (buffer) en je wilt die printen met printf("%s")? Da's vreemd he :wink:
  • Het werkt, echt heel erg bedankt :D. Ik heb het veranderd naar %f (niet zo slim %S ;)).

Beantwoord deze vraag

Weet jij het antwoord op deze vraag? Registreer of meld je aan met je account

Dit is een gearchiveerde pagina. Antwoorden is niet meer mogelijk.