In my previous post I was using the Arduino Ethernetshield to connect my Arduino to a Java Server running on my local machine. Now I’m Using the Arduino as a TCP Server and my local machine as a Client. So it’s just the other way around
I modified the Arduino Webserver example to a simple TCP Server:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <SPI.h> #include <Ethernet.h> // Enter a MAC address, IP address and Portnumber for your Server below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress serverIP(192,168,195,214); int serverPort=8888; // Initialize the Ethernet server library // with the IP address and port you want to use EthernetServer server(serverPort); void setup() { // start the serial for debugging Serial.begin(9600); // start the Ethernet connection and the server: Ethernet.begin(mac, serverIP); server.begin(); Serial.println("Server started");//log } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { String clientMsg =""; while (client.connected()) { if (client.available()) { char c = client.read(); //Serial.print(c); clientMsg+=c;//store the recieved chracters in a string //if the character is an "end of line" the whole message is recieved if (c == '\n') { Serial.println("Message from Client:"+clientMsg);//print it to the serial client.println("You said:"+clientMsg);//modify the string and send it back clientMsg=""; } } } // give the Client time to receive the data delay(1); // close the connection: client.stop(); } } |
To connect to this Server I’m using a simple Java TCP Client.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.lauridmeyer.tests; import java.io.*; import java.net.*; class Client { //Ip Adress and Port, where the Arduino Server is running on private static final String serverIP="192.168.195.214"; private static final int serverPort=8888; public static void main(String argv[]) throws Exception { String msgToServer;//Message that will be sent to Arduino String msgFromServer;//recieved message will be stored here Socket clientSocket = new Socket(serverIP, serverPort);//making the socket connection System.out.println("Connected to:"+serverIP+" on port:"+serverPort);//debug //OutputStream to Arduino-Server DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); //BufferedReader from Arduino-Server BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));// msgToServer = "Hello Arduino Server";//Message tha will be sent outToServer.writeBytes(msgToServer+'\n');//sending the message System.out.println("sending to Arduino-Server: "+msgToServer);//debug msgFromServer = inFromServer.readLine();//recieving the answer System.out.println("recieved from Arduino-Server: " + msgFromServer);//print the answer clientSocket.close();//close the socket //don't do this if you want to keep the connection } } |
Change the IP Adress to make it work in your local Network (in the Arduino AND the Java Code). Then
- compile and upload the code to the arduino. The Server should be starting (check the Serial Monitor)
- Afterwards compile and start the Java Client.
- If it connects correctly to the Arduino it will send the message: “Hello Arduino Server”.
- The Arduino-Server recieves this message and will send it back: “You said: Hello Arduino Server”.
Downloads:
This is just a very simple example to get started with the Arduino Ethernetshield. You should also check out my similar Post to use the Arduino as a TCP Client.



Im getting this exception here >> in compilr.com and also on eclipse IDE
Exception in thread “main” java.lang.NoClassDefFoundError: Program
Caused by: java.lang.ClassNotFoundException: Program
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Program. Program will exit.
what to do ? do you have a .exe of the applicaiton
Hello, my name is Pedro Manuel and I am delighted to meet these great tutorials. First I would like to apologize for my English, I’m Spanish and I have a very low level of English, sorry.
I’m developing a Java application able to connect to the arduino TCP girders. Java-arduino communication I have it resolved, the problem I have is when you want to communicate with the arduino java client.
To receive information arduino first I have to send a command from java requesting information. I would like to know how it could be done for the arduino is able to communicate with the java client without sending the information request command. To do this I created a Java application that is client and server at a time, and in like manner the arduino. This presents a major hassle and I do not think it is necessary to cough.
Could you tell me how the Java client can be aware of the arrival of information arduino?
Like I said earlier, I am developing application consists of a client like the mentoring process and a thread, run the server capable of serving the packets of information that the arduino sends. I’m having a lot of problems with this project and would like to know your opinion. Do you need all this, what could be done differently?
I leave you my email if you could contact me or answer me on my blog, I am a faithful follower of the.
One last thing, what happened to the blog that’s been dropped a few days?, I thought you left and that made me sad.
A greeting.