I am working on an Android app including client and server that client app sends camera captured data to server for processing, and server sent the result back to client to display. The current version of it uses TCP for transmitting data, so the code includes something like:
Client side:
send(){
Socket socket = new Socket();
DataOutputStream outputStream = new DataoutputStream(socket.getOutPutStream());
outputStream.writeUTF(...)
...
outputStream.write(data);
outputSteram.flush();
...
}
receive(){
DataInputStream dataInputStream = new DataInputStream(...);
...
dataInputStream.readFully(data, 0, length);
...
}
Server side:
...
serverDataInputStream.readFully(data,0,length);
//process data
...
serverDataOutputStream.write(reply)
...
I tried to change this to use UDP, but the data sent from client to server is much larger than the maximum size of a UDP packet, so have to fragment the data in client and reassemble it on server manually. It turns out to be too much troublesome.
I know RTP takes care of data fragmentation, so now I want to change this into RTP. After Googleing for a while, I found the following information:
http://sourceforge.net/projects/jlibrtp/
http://code.google.com/p/sipdroid/source/browse/trunk/src/org/sipdroid/sipua/ui/VideoCamera.java
And these two posts are very similar to what I am doing:
Android Camera RTSP/RTP Stream?
Creating RTP Packets from Android Camera to Send
But the posts just include limited sample code. Can anyone post more details and sample code here? I don't have much experience of Java and Android programming. So detailed examples or step by step instruction will be extremely appreciated!
Thank you!
Related
Me and a friend are working on a project which requires us to communicate between a C#.NET application and a Java application.
We're using the TcpClient and BinaryWriter classes on the .NET side of things to send and receive things. We're using code similar to this to send things:
byte[] content = //we're getting our content here
Writer.Write(new byte[9]); //this is the BinaryWriter with the NetworkStream of the TcpClient
Writer.Flush();
On the Java side of things, we're using netty to handle our networking. To receive the content we send from the .NET side, we add a ChannelInboundHandlerAdapter to the pipeline and use the channelRead method to read the content:
public void channelRead(ChannelHandlerContext ctx, Object received)
{
ByteBuf receivedByteBuf = (ByteBuf)received;
this.bytesRead = receivedByteBuf.readableBytes();
System.out.println("Received " + this.bytesRead + " bytes.");
final byte[] buffer = new byte[this.bytesRead];
receivedByteBuf.markReaderIndex();
receivedByteBuf.readBytes(buffer);
receivedByteBuf.resetReaderIndex();
}
Now the strange thing is, that when we try sending content, it doesn't always arrive in one piece. Sometimes we only receive all but some bytes we originally sent, which arrive in a new call of channelRead. In this example, only 6-8 bytes would arrive. This is very strange, as this only happens when using .NET. We tried sending content using python and everything worked fine and it arrived in one channelRead call.
import socket
import string, random
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 8888))
s.send(''.join(random.choice(string.lowercase) for x in range(500)))
s.close()
Unfortunately, the nature of our project prevents us from changing our Java networking library, so we're stuck with netty.
Did we miss some setting in netty or does this have to do with the nature of the .NET TCP libraries? We would appreciate any help we can get.
I have a ESP8266 chip which is connected to the microcircuit. When chip gets value "200" the light is starting to blink 4 times and than it returns "100" value. I need to make an Android app using Java which will connect to the ESP8266 chip, send data to it and will get value "100". I don't know what library I should use to deal with it. Please, help me, how can I do that? I think it is not the most hard question here.
For your Controller you dont need any Libary. You just can use the serial AT Commands: http://www.electrodragon.com/w/ESP8266
After setting up your ESP like this:
In your App you should deal with TCP-Sockets: https://de.wikibooks.org/wiki/Googles_Android/_TCP-Sockets
Try something like this in an async task:
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), Connect_Timeout);
DataOutputStream DataOut = new DataOutputStream(socket.getOutputStream());
DataOut.writeBytes(message);
DataOut.flush();
socket.close();
So your ESP is the Server and the App the Client. This should work without problems.
I had a board connect to the PC using LAN cable(RJ45). I need to write the Java code to connect the board and get some data from it. How can I do it?
Actually I got a code from C++, it used CAsyncSocket class to do it. The C++ code is like this:
CAsyncSocket.Create();
CAsyncSocket.connect(IP, PORT);
Now, I would like to convert it into Java. Actually, I'm not so familiar with Java. Can someone show the code to me?
Example: My board IP is 192.168.2.10 and PORT is 2000. How can I connect it using Java?
see here (for example):
Socket socket = new Socket("192.168.2.10", 2000);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Input: " + input.readLine());
socket.close();
http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html
Check out the socket tutorial Lesson: All About Sockets:
URLs and URLConnections provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application.
See the [http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html](Reading from and Writing to a Socket) example:
Let's look at a simple example that illustrates how a program can establish a connection to a server program using the Socket class and then, how the client can send data to and receive data from the server through the socket.
I have a spreadsheet application in Java, and one of the features it provides (which I developed) is sheet sharing. Basically, anyone can be a client or a server because the app has both server and client code. The user who is the server creates the share, specifies the IP, and then the share is created and active (best case scenario) with the server listening for clients on its IP and selected port.
For auto-discovery, I am using DatagramSockets via UDP broadcasts, while the 'real communication' is TCP-based (after the client is already connected). However, I'm trying to send a List through that UDP socket and I don't know how to do it. That List contains the active shares on the server that I need to send to the client so it knows what it can connect to.
It goes like this:
Client -> looks for active servers by sending a packet to the network -> server listens and sends a packet back. This packet should be the List (if it is possible to send it via these kind of sockets).
Can anyone shed some light on my question? Thank you.
You can convert your List to a byte[] before sending, and convert it back to a List on the reciever using Java Serialization.
// Sender
List list = new ArrayList();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(out);
outputStream.writeObject(list);
outputStream.close();
byte[] listData = out.toByteArray();
// Reciever
ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(listData));
list = inputStream.readObject();
Just make sure all the objects you put in your List implements Serializable.
i m a new .
i m a java developer(fresher) and currently i m working on BSE project and i m facing problem to read the packet of bytes on the client(client socket) from the server(server socket). if u can help me then please help me.
Thanks in advance
Well, if you want to interact directly with packets, then you need to use a DatagramSocket instead of the regular Socket and ServerSocket.
Then, you should visit this link to see a good tutorial on how to get started with sending and receiving individual packets.
The basic idea is that the Client or Server will block on the recieve() call while it waits for its partner to send a packet using send().
If you aren't interested in the individual packets like you indicated in your question, then you will want to use Socket and ServerSocket. The first step to communicating between the two involves code that will look similar to the following:
//Server
// this call will block until the client tries to connect to the server
Socket cientConn = new ServerSocket(8878).accept();
// now you can use the connection's input and output streams to send data
/******************/
// Client
Socket serverConn = new Socket(addressOfServer, 8878);
// now you can use the connections input and output streams
After you get connections set up, you will have basically 2 read/write loops. One on the client, and one on the server.
while(true) [
// check for data from an input stream
...
// respond with message back
}
You will need a similar loop for the client and the server.