GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-May-2009, 05:44
jyc jyc is offline
New Member
 
Join Date: May 2009
Posts: 7
jyc is on a distinguished road

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  
Old 05-May-2009, 10:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sending this over Windows socket?


Quote:
Originally Posted by jyc
...
how best to send this array:

float data [2][2048];

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:
    for (i = 0; i < rows; i++) {
        for (j = 0; j < columns; j++) {
            int numbytes = write(fd, (char *)&data[i][j], sizeof(data[i][j]));
            if (numbytes != sizeof(float)) {
                perror("write:");
                exit(EXIT_FAILURE); /* Or, maybe execute a more civilized scheme */
            }
        }
    }

Quote:
Originally Posted by jyc
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?
Well, I kind of doubt that, but there is a very good reason not to convert to text and back again: roundoff error in the conversion can cause the received data not to be equal to the data in the floating point array. If you want to send binary floating point data, I think that binary is the way to go.

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  
Old 05-May-2009, 11:26
jyc jyc is offline
New Member
 
Join Date: May 2009
Posts: 7
jyc is on a distinguished road

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  
Old 05-May-2009, 11:31
jyc jyc is offline
New Member
 
Join Date: May 2009
Posts: 7
jyc is on a distinguished road

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  
Old 05-May-2009, 11:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sending this over Windows socket?


Quote:
Originally Posted by jyc
...at the receiving end...


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  
Old 05-May-2009, 11:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sending this over Windows socket?


Quote:
Originally Posted by jyc
what do I need to include to use "write"?

i get error C3861: 'write': identifier not found.

Jantzen

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  
Old 05-May-2009, 11:49
jyc jyc is offline
New Member
 
Join Date: May 2009
Posts: 7
jyc is on a distinguished road

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:
void EtherTransfer::processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames)
{
    float* in1  =  inputs[0];
    float* in2  =  inputs[1];
    float* out1 = outputs[0];
    float* out2 = outputs[1];
	float data [2][2048];
	int no_send;
	Test=sampleFrames;

	for(int i=0;i<2;i++) {
		for(int j=0;j<sampleFrames;j++) {
			data[i][j] = *(inputs[i]);
			(inputs[i])++;
		}
	}

	for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2048; j++) {
            int numbytes = send(m_socket, (char *)&data[i][j], sizeof(data[i][j]), 0);
            if (numbytes != sizeof(float)) {
                perror("write:");
				no_send = 1;
                break; 
            }
        }
    }


	float *dataTest = data[0];
	float *dataTest2 = data[1];
	for(int j=0;j<sampleFrames;j++) {
		*out1 = dataTest[j];
		out1++;
		*out2 = dataTest2[j];
		out2++;
	}
	
/*    while (--sampleFrames >= 0)
    {
		(*out1++) = (*in1++) * fGain;
        (*out2++) = (*in2++) * fGain;
    }
*/
}
  #8  
Old 05-May-2009, 12:20
jyc jyc is offline
New Member
 
Join Date: May 2009
Posts: 7
jyc is on a distinguished road

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  
Old 05-May-2009, 14:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sending this over Windows socket?


Quote:
Originally Posted by jyc
I might have to send it in one big chunk, sending them in small chunks introduce too much delay i think....

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:
            /* Might be pretty efficient is cols is large */
            for (i = 0; i < rows; i++) { 
                numbytes = send(new_fd, (char *)&data[i][0], cols*sizeof(float), 0);
                if (numbytes == -1) {
                    perror("send");
                }
            }

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 GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 21:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.