2015-04-19 106 views
0

有我的webclient套接字的代碼arduino和我的java web服務器都在運行,但如何將它們連接在一起。如何將服務器連接到Arduino客戶端?

#include "Arduino.h" 
#include <Ethernet.h> 
#include <SPI.h> 
#include <WebSocketClient.h> 

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03}; 
char server[] = "localhost:8080"; // server name 
WebSocketClient client; 

void setup() { 
    Serial.begin(9600); 
    Ethernet.begin(mac); 
    client.connect(server); //client connect to the server 
    client.setDataArrivedDelegate(dataArrived); 
    client.send("Hello World!"); 
} 

void loop() { 
    client.monitor(); 
} 

如何將服務器連接到Arduino客戶端?

void dataArrived(WebSocketClient client, String data) { 
    Serial.println("Data Arrived: " + data); 
} 

回答

0
public class server { 
public static void main (String[]args) throws Exception { 
server Server = new server(); 
Server.run(); 
} 
public void run() throws Exception { 
ServerSocket SRVSOCK = new ServerSocket(8080); 
Socket sock=SRVSOCK.accept(); 
InputStreamReader IR= new InputStreamReader(sock.getInputStream()); 
BufferedReader BR= new BufferedReader(IR); 
String message = BR.readLine(); 
System.out.println(message); 
if(message!=null){ 
    PrintStream ps=new PrintStream(sock.getOutputStream()); 
    ps.println("recieved"); 
    SRVSOCK.close(); 
} 


} 
this is my server code 
相關問題