Reputation: 547
I have a simple EthernetServer example installed on my arduino. I have setup IP address and MAC address. I can ping the Arduino from my PC but I can't send any data to it from a simple Java program.
Here is a source from Arduino:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 16, 201, 218 };
EthernetServer server = EthernetServer(8080);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
server.write(client.read());
}
}
I can ping Arduino from command prompt:
C:\WINDOWS\system32>ping 172.16.201.218
Pinging 172.16.201.218 with 32 bytes of data:
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Ping statistics for 172.16.201.218:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
C:\WINDOWS\system32>
The client in Java is:
package ardsocket;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ArdSocket {
public static void main(String args[]) throws IOException {
final String host = "172.16.201.218";
final int portNumber = 8080;
System.out.println("Creating socket to '" + host + "' on port " + portNumber);
while (true) {
Socket socket = new Socket(host, portNumber);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("server says:" + br.readLine());
BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
String userInput = userInputBR.readLine();
out.println(userInput);
System.out.println("server says:" + br.readLine());
if ("exit".equalsIgnoreCase(userInput)) {
socket.close();
break;
}
}
}
}
When I run the Java source nothing happen.. Also If I try to connect over Telnet on IP and port 8080 is the same story.
What I'am doing wrong?
Upvotes: 0
Views: 2376
Reputation: 26
Hope my code Java helpful !
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class GateControl {
public static void main(String []args) {
try {
// "192.168.1.177" is a IP of Server
Socket s = new Socket("192.168.1.177", 80);
InetAddress add = s.getInetAddress();
System.out.println("Connected to " + add);
PrintWriter pw = new PrintWriter(s.getOutputStream());
// "?butonon" is a content which you send to server
pw.println("GET /?buttonon HTTP/1.1");
pw.println("");
pw.println("");
pw.flush();
System.out.println("Request sent");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 21893
Add logs to Arduino Sketch for Debug Purposes
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 16, 201, 218 };
EthernetServer server = EthernetServer(8080);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
// Client Connected
Serial.println("Client Connected.");
server.write(client.read());
}
}
And in java client try putting out.flush();
after out.println(userInput);
and I think you don't need to create Socket
, BufferedReader
and PrintWriter
inside the loop. You can move those to the top.
package ardsocket;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ArdSocket {
public static void main(String args[]) throws IOException {
final String host = "172.16.201.218";
final int portNumber = 8080;
System.out.println("Creating socket to '" + host + "' on port " + portNumber);
Socket socket = new Socket(host, portNumber);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("server says:" + br.readLine());
String userInput = userInputBR.readLine();
out.println(userInput);
out.flush();
System.out.println("server says:" + br.readLine());
if ("exit".equalsIgnoreCase(userInput)) {
socket.close();
break;
}
}
}
}
Upvotes: 0