network help needed
Unknown W. Brackets
unknown at simplemachines.org
Tue Oct 24 00:35:10 PDT 2006
It sounds like you've got an audio stream you want to send. And it
sounds like really low quality data too?
I'm afraid I'm not really an audio person, Hz, etc.. but I think
uncompressed you're talking about 1kHz 8-bit stereo sound. Or you're
not talking about sound and you're just using that terminology to
confuse me.
1kHz 8-bit stereo means 2000 bytes per second. That's about 2kb/s which
is not really too bad even over dial-up. Compress it, and you might
find even better savings... depending.
Anyway, for code, it's really not that hard. Imagine a switchboard.
You know, those old-timey things. With the operator, and a bunch of
plugs. One of those.
Now, a "socket" could be thought of as a plug. A "connection" is the
wire in between. Operators are probably routers or something. Doesn't
matter.
There are two parts to it. Clients, and servers.
For clients: first, you want to create a socket:
Socket sock = new TcpSocket(AddressFamily.INET);
Now, to make a connection, you need to have somewhere to connect to. A
phone number, if you will. This is an IP address. You also need a
"port". That's like who you're calling for. And yes, you need one.
Address addr = new InternetAddress("10.0.0.1", 9432);
After this, you connect. Happy days:
sock.connect(addr);
From then on, you can read and write data using send and receive.
However, note that these functions may return:
- an error code.
- a number of bytes less than the number you wanted to send.
- the number of bytes you wanted to send.
So, you make a buffer, and slowly send all of it.
For servers, it's not much different. But this time, you take your
socket, as created above... and "bind" to an address.
Binding to an address basically means you're taking/getting a phone
number and port. So, like this:
sock.bind(new InternetAddress("10.0.0.1", 9432));
Conveniently, this port number should be the same as the one the client
uses.
Then you have to start "listening":
sock.listen(32);
The parameter is how many people can wait in line.
After that, it gets a lot more complicated, and you might do select() or
threads, etc. But, somewhere you accept a new connection, like so:
Socket client = sock.accept();
Once you have the new socket, this one can have data sent or read from
it. Again, just use send() and receive(). They work the same.
Hope that helps some. But your question is fairly vague.
-[Unknown]
> Which way is the best way to get data(1000Hz 8channel double) over a network
> (directly connected).
> The data should have an low as possible delay, or a known delay.
>
> I'm really not into networking (as apparent in this post :), but understand
> it needs the std.socket(stream).
>
>
More information about the Digitalmars-d-learn
mailing list