I am planning to send raw data stream from esp8266 to a java pc application that is actually a serversocket.
The code for server is:
package com.test.socket;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
public class GreetingServer extends Thread {
private ServerSocket serverSocket;
private SocketAddress remoteSocketAddress;
public GreetingServer(ServerSocket serverSocket, String name) throws IOException {
this.setName(name);
this.serverSocket = serverSocket;
//serverSocket.setSoTimeout(10000);
}
public void run() {
System.out.println(getName()+"->Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket server=null;
byte buffer[]=new byte[1024];
int readBytes=0;
try {
server = serverSocket.accept();
remoteSocketAddress=server.getRemoteSocketAddress();
System.out.println(getName()+"->Just connected to " + remoteSocketAddress);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
while (true) {
try {
DataInputStream in = new DataInputStream(server.getInputStream());
if(in.available()>0){
readBytes=in.read(buffer, 0, buffer.length);
System.out.println(getName()+"->"+remoteSocketAddress+"->"+new String(buffer,0, readBytes));
}else{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
//int port = Integer.parseInt(args[0]);
try {
ServerSocket serverSocket=new ServerSocket(8086);
Thread t = new GreetingServer(serverSocket,"T1");
t.start();
Thread t2 = new GreetingServer(serverSocket,"T2");
t2.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I wrote a simple java socket client and I am able to send data asynchronously.
The client code:
package com.test.socket;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class GreetingClient {
public static void main(String[] args) {
String serverName = "localhost";
int port = 8086;
try {
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=reader.readLine();
if(str.equals("exit"))break;
out.write(str.getBytes(), 0, str.length());
out.flush();
}
out.writeUTF("Hello from " + client.getLocalSocketAddress());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now I want to replace the java client with a esp client. That is I want to connect to a server socket that is run as a java server socket program.
I also found a thread about communication between a c socket server and a java socket client at http://stackoverflow.com/questions/19561941/socket-comunication-java-client-c-server.
So is that possible to create a esp sketch that would open a tcp/ip socket and send raw data. Here I am not using any application layer protocol like http or ftp however I am open to all type of suggestions that would cater my requirement to send raw data stream from esp to java program.
Thanks,
Debojit