daily_automated
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UDPClient {
|
||||
|
||||
public static void main(String[] args) throws UnknownHostException {
|
||||
DatagramSocket s;
|
||||
byte[] sendBuffer = new byte[1024];
|
||||
DatagramPacket sendPacket;
|
||||
final InetAddress ADDRESS = InetAddress.getByName("localhost");
|
||||
final int PORT = 1234;
|
||||
try {
|
||||
s = new DatagramSocket();
|
||||
System.out.println("Odesilam data na server...");
|
||||
|
||||
sendBuffer = "abc123".getBytes();
|
||||
sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length,
|
||||
ADDRESS, PORT);
|
||||
s.send(sendPacket);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class UDPserver {
|
||||
public static void main(String[] args) {
|
||||
UDPserver server = new UDPserver(1234);
|
||||
server.start();
|
||||
}
|
||||
private int port;
|
||||
private DatagramSocket s;
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
public UDPserver(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("SERVER: Waiting for incomming connections...");
|
||||
System.out.println("DATE TIME IP:PORT received_data");
|
||||
try {
|
||||
s = new DatagramSocket(this.port);
|
||||
while (true) {
|
||||
byte[] data = new byte[1412];
|
||||
DatagramPacket p = new DatagramPacket(data,
|
||||
data.length);
|
||||
s.receive(p);
|
||||
System.out.print(sdf.format(new Date()).toString() + " ");
|
||||
System.out.print(p.getSocketAddress() + " ");
|
||||
System.out.println(new String(p.getData()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <EtherCard.h>
|
||||
|
||||
static byte mymac[] = { 0x1A,0x2B,0x3C,0x4D,0x5E,0x6F };
|
||||
byte Ethernet::buffer[700];
|
||||
static uint32_t timer;
|
||||
|
||||
const char website[] PROGMEM = "server.example.net";
|
||||
const int dstPort PROGMEM = 1234;
|
||||
|
||||
const int srcPort PROGMEM = 4321;
|
||||
|
||||
void setup () {
|
||||
Serial.begin(9600);
|
||||
|
||||
// Change 'SS' to your Slave Select pin, if you arn't using the default pin
|
||||
if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
|
||||
Serial.println( "Failed to access Ethernet controller");
|
||||
if (!ether.dhcpSetup())
|
||||
Serial.println("DHCP failed");
|
||||
|
||||
ether.printIp("IP: ", ether.myip);
|
||||
ether.printIp("GW: ", ether.gwip);
|
||||
ether.printIp("DNS: ", ether.dnsip);
|
||||
|
||||
if (!ether.dnsLookup(website))
|
||||
Serial.println("DNS failed");
|
||||
|
||||
ether.printIp("SRV: ", ether.hisip);
|
||||
}
|
||||
|
||||
char textToSend[] = "test 123";
|
||||
|
||||
void loop () {
|
||||
if (millis() > timer) {
|
||||
timer = millis() + 5000;
|
||||
//static void sendUdp (char *data,uint8_t len,uint16_t sport, uint8_t *dip, uint16_t dport);
|
||||
ether.sendUdp(textToSend, sizeof(textToSend), srcPort, ether.hisip, dstPort );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user