[英]Arduino ethernet shield + Java socket connection
I have a simple EthernetServer example installed on my arduino. 我在arduino上安装了一个简单的EthernetServer示例。 I have setup IP address and MAC address. 我已经设置了IP地址和MAC地址。 I can ping the Arduino from my PC but I can't send any data to it from a simple Java program. 我可以从PC ping Arduino,但是无法通过简单的Java程序向其发送任何数据。
Here is a source from Arduino: 这是来自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());
}
}
When I run the code I get: 当我运行代码时,我得到:
I can ping Arduino from command prompt: 我可以在命令提示符下ping Arduino:
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: Java中的客户端是:
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. 当我运行Java源代码时,什么也没有发生。另外,如果我尝试通过Telnet在IP和端口8080上进行连接,则是同样的情况。
What I'am doing wrong? 我做错了什么?
Hope my code Java helpful ! 希望我的Java代码对您有所帮助!
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();
}
}
}
Add logs to Arduino Sketch for Debug Purposes 将日志添加到Arduino Sketch以进行调试
#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();
并在Java客户端中尝试out.flush();
after out.println(userInput);
在out.println(userInput);
and I think you don't need to create Socket
, BufferedReader
and PrintWriter
inside the loop. 而且我认为您无需在循环内创建Socket
, BufferedReader
和PrintWriter
。 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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.