Page 1 of 1

Wifi Communication with Java App

Posted: Sun Jul 09, 2017 5:11 pm
by amartin7211
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.

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();
		}
ESP32 code adapted from the example

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");
Any help would be greatly appreciated!

Re: Wifi Communication with Java App

Posted: Mon Jul 10, 2017 5:16 am
by kolban
At a guess, the logic for your sockets flow in your ESP32 isn't right. I'm thinking that your ESP32 application is a socket client while your Java application is a socket server.

A typical client executes:

* socket()
* connect()
* send()/write()

See for example:

https://www.tutorialspoint.com/unix_soc ... xample.htm

In your ESP32 application, I don't see a connect(). One uses bind() to bind an unconnected socket to a local address for use when setting up a local listener. If I were in your place, I'd hit the books some more on sockets() APIs. There are some good sources of knowledge through Google searches.

Re: Wifi Communication with Java App

Posted: Tue Jul 11, 2017 2:17 am
by amartin7211
Ah, yes, I see! The link to the tutorial you provided was very helpful. I was able to get the c code working and should be able to model my own code after it fairly easily.

Thanks so much!