![]() |
|
#1
|
|||
|
|||
Sending this over Windows socket?Second question here as I rush to finish this project I have going......
how best to send this array: float data [2][2048]; over the SOCKET created by accept()??? I'm trying to print it to text and convert at the moment, but that will create too large a character array for C++ to handle I suppose? Many thanks, Jantzen |
|||
|
#2
|
|||
|
|||
Re: Sending this over Windows socket?Quote:
One "quick-and-dirty" way depends on the sending and receiving machine having the same endian architecture. (Both big-endian or both little-endian) See Footnote. If you are using write() to send the data, you could do something like. CPP / C++ / C Code:
Quote:
Regards, Dave Footnote: Historically, network protocols for sending binary data have been defined to send integer numerical data in "big-endian" format, where the most significant byte is sent first. You can make up your own method for sending binary data, and assume that the receiving machine and transmitting machine are same-endian (as my little code snippet does), but some day, in some way, that habit may lead to an unpleasant surprise. Data sent from an Intel-based PC or Intel-based Mac or other little-endian machine won't be received correctly by a PowerPC-based Mac or by certain other workstations that have big-endian architecture. With integers, the network functions htonl and ntohl are usually supplied by compiler vendors to convert machine-endian 32-bit integers to and from 32-bit big-endian integers used in network protocols. If the number of bytes in a floating point variable is equal to the number of bytes in an integer on your machine, you could, maybe, develop a way to use one of these functions, a float at a time, to do the conversion. If you want a guaranteed-portable method of converting floats, writing one is not difficult. |
|
#3
|
|||
|
|||
Re: Sending this over Windows socket?many thanks!
yeah, i was digging how it could consistently output the float to one format and maybe use atoi to do it.... Anyway, with that, how do I go about converting the char* into float at the receiving end? Jantzen |
|
#4
|
|||
|
|||
Re: Sending this over Windows socket?what do I need to include to use "write"?
i get error C3861: 'write': identifier not found. Jantzen |
|
#5
|
|||
|
|||
Re: Sending this over Windows socket?Quote:
You can use recv() with the same kind of stuff for arguments two and three (address of the float and the size of the float) Regards, Dave |
|
#6
|
|||
|
|||
Re: Sending this over Windows socket?Quote:
How do you usually send stuff over a socket? I just used write() because it's convenient (and it works for me). If you had actually shown some code, I might have been more helpful. If you usually use send() or sendto() they will work the same way as write(): Put the address of the float and the size of the float in the "usual places" in the argument list. Different compilers have different places for prototypes for non-standard library functions. How have you been compiling any network applications? What did you have to include in order to call socket()? With Microsoft or Borland compilers, typically you include <winsock.h> or, maybe, <winsock2.h> With GNU compilers, you can try <unistd.h> or some such thing. Consult your compiler documentation. Need more information? 1. Post some code. 2. Tell us what compiler and operating system you are using. Regards, Dave |
|
#7
|
|||
|
|||
Re: Sending this over Windows socket?I'm using Visual C++ Express 2008.
I'm using send() for now, it seems to work, am now trying to implement the receiving end. Let's hope it works! Thanks very much again. The following are the code on the sending side, I'm writing a plugin to offload audio to another computer for processing, in real time. Let's hope it should work. Some of the functions/variables below I was just testing stuff to see what works. CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: Sending this over Windows socket?I might have to send it in one big chunk, sending them in small chunks introduce too much delay i think....
|
|
#9
|
|||
|
|||
Re: Sending this over Windows socket?Quote:
First of all, I suggest that you get it to work using what you have so far. Maybe use a smaller array for testing. Make sure that each and every value that is received is exactly the same as the value in the original array. Then use a larger array. If network transmission time becomes an issue (or if you know that it will be an issue): Since the members of a dynamically allocated array will not be guaranteed to be contiguous, you can't simply give the starting address and a size equal to the total storage size to the send() function. A couple of possibilities come to mind: 1. For this particular problem, the number of columns (values on each row) is large, and the members on each row will be contiguous, you can send the message a row at a time. For your example, rows = 2 and cols = 2048, so you would actually execute two send() function calls. CPP / C++ / C Code:
The network protocol (tcp? udp? whatever?) will break the message into as many packets as are required for a given maximum packet payload size. 2. In general, you might have a matrix with a large number of rows and each row has a small number of columns, so sending a row at a time might not be very efficient. You might consider writing the values to a buffer large enough to hold everything. Then use a single send() statement. For example, you could use the new operator (C programmers would use malloc()) to get a 1D array of floats and copy the all of the data matrix values into it. Then a single send() function call would be required. A refinement would be to copy a "chunk" at a time to a dynamically-allocated 1D array whose size is equal to the maximum packed payload size. In any case, I suggest that you start with a small array and work your way up to larger and larger arrays, testing each case to make absolutely sure that it works before going to a larger one. Regards, Dave |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Command & Conquer 95 under XP | dexter | Computer Software Forum - Games | 42 | 20-Sep-2008 14:30 |
| OnReceive notification failed from 2nd time onwards in Windows Socket Programming. | mahendra | MS Visual C++ / MFC Forum | 0 | 09-Jun-2008 04:48 |
| How To Remove Linux And Install Windows? | rockaway | Computer Software Forum - Windows | 3 | 06-Mar-2008 22:00 |
| [PROGRAM] Winsock Programming | Max Payne | MS Visual C++ / MFC Forum | 1 | 09-Mar-2007 00:38 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The