Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to connect my arduino ethernet shield and a java application to communicate. I could not achieve that any one can help.

this is my java code

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package client;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
*
* @author amanuel Negash
*/


public class Client extends Application {
// IO streams
DataOutputStream toServer = null;
DataInputStream fromServer = null;

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Enter a radius: "));

TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);

BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);

// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage

tf.setOnAction(e -> {
try {
// Get the radius from the text field
double radius = Double.parseDouble(tf.getText().trim());

// Send the radius to the server
toServer.writeDouble(radius);
toServer.flush();

// Get area from the server
double area = fromServer.readDouble();

// Display to the text area
ta.appendText("Radius is " + radius + "\n");
ta.appendText("Area received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
SocketAddress sockaddr = new InetSocketAddress("172.17.31.47", 59637);
// Create your socket
Socket socket = new Socket();
// Connect with 10 s timeout
socket.connect(sockaddr, 999999999);;

// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());

// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
socket.close();
}
catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
}








****************************************************************************************************************************************************************************
the following is my etherent arduino code

#include <ethernet.h>
#include <spi.h>
byte mac[] = {0x90, 0xf2, 0xda, 0x0e, 0x98, 0x34 };

EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
server.begin();
delay(1000);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
server.begin();


}
void loop() {
//Serial.println("loop");
client = server.available();
char incoming;

if (client)
{
Serial.println("Client connected");
while(client.connected()) {
if (client.available()) {
incoming = client.read();
Serial.print(incoming);
client.print(incoming);
}
}
Serial.println("Client disconnected");
}
client.stop();
}

What I have tried:

I have disabled the firewall and I can ping the server.
Posted
Comments
[no name] 18-Feb-18 18:55pm    
How about providing details of what actually happens. Where does it get to?

1 solution

nothing really happens the java client ends saying the
Error:java.net.SocketTimeoutException: Receive timed out
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900