Wifi Communication with Java App
Posted: Sun Jul 09, 2017 5:11 pm
I'm new to the esp32 and the sockets API. I am trying to communicate with a java app I wrote. I am attempting to adapt the openssl_client_example_main.c but am not using the ssl stuff. I can get the socket created and bound, however, when I try to send data it fails.
Here is the java code. It works with a java client program but not the esp32 program.
ESP32 code adapted from the example
Any help would be greatly appreciated!
Here is the java code. It works with a java client program but not the esp32 program.
Code: Select all
public class MySocketServer {
private String hostname;
private int port;
public static void main(String[] args) throws IOException {
System.out.println("Starting server...");
int number,temp;
ServerSocket s1 = new ServerSocket(4444);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
number = sc.nextInt();
temp = number*2;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
ss.close();
}
Code: Select all
static void openssl_example_task(void *p) // task function for RTOS
{
ESP_LOGI(TAG, "within openssl_example_task");
int ret; // short for "return"?
SSL_CTX *ctx; // ctx is context
SSL *ssl;
int socket;
int sendmsg;
struct sockaddr_in sock_addr; // structs are collections of variables of different types
struct hostent *hp;
struct ip4_addr *ip4_addr;
int recv_bytes = 16;
char recv_buf[recv_bytes];
const char send_data[] = "yo";
const int send_bytes = sizeof(send_data);
ESP_LOGI(TAG, "OpenSSL demo thread start OK"); // LOGI is a macro for log information
// -----------------------------------------------------------------------------
ESP_LOGI(TAG, "get target IP address");
hp = gethostbyname("127.0.0.1");
if (!hp) {
ESP_LOGI(TAG, "hp failed");
goto failed1;
}
ESP_LOGI(TAG, "OK");
// -----------------------------------------------------------------------------
ip4_addr = (struct ip4_addr *)hp->h_addr;
ESP_LOGI(TAG, IPSTR, IP2STR(ip4_addr));
ESP_LOGI(TAG, "create SSL context ......");
ctx = SSL_CTX_new(TLSv1_1_client_method());
if (!ctx) {
ESP_LOGI(TAG, "ssl context failed");
goto failed1;
}
ESP_LOGI(TAG, "OK");
// -----------------------------------------------------------------------------
ESP_LOGI(TAG, "create socket ......");
socket = socket(AF_INET, SOCK_STREAM, 0);
if (socket < 0) {
ESP_LOGI(TAG, "socket failed");
goto failed2;
}
ESP_LOGI(TAG, "OK");
// -----------------------------------------------------------------------------
ESP_LOGI(TAG, "bind socket ......");
memset(&sock_addr, 0, sizeof(sock_addr)); // memset used to fill a block of memory with a particular value.
sock_addr.sin_family = AF_INET; // AF_INET is IPv4 network addresses
sock_addr.sin_addr.s_addr = 0;
sock_addr.sin_port = htons(4444); // htons converts a host formatted short int to network byte order
// binding associates a socket with an address
ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr)); // return < 0 if error
if (ret) {
ESP_LOGI(TAG, "failed");
goto failed3;
}
ESP_LOGI(TAG, "OK");
ESP_LOGI(TAG, "send data ......");
sendmsg = write(socket, send_data, send_bytes);
if (sendmsg){
ESP_LOGI(TAG, "failed");
goto failed3;
}
ESP_LOGI(TAG, "OK");