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");