Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Controle_ventilo_ethernet_v1_2_4 by
Diff: main.cpp
- Revision:
- 1:179393386b47
- Parent:
- 0:5ca563ae05db
- Child:
- 2:1dd0818af42e
diff -r 5ca563ae05db -r 179393386b47 main.cpp --- a/main.cpp Mon Oct 31 16:59:50 2016 +0000 +++ b/main.cpp Fri Nov 18 10:46:45 2016 +0000 @@ -9,27 +9,62 @@ #include "Socket.h" #include "UDPSocket.h" - -#define ECHO_SERVER_PORT 7 - -int main (void) { +const char* ECHO_SERVER_ADDRESS = "192.168.2.2"; +const int ECHO_SERVER_PORT = 7; + +int main() { EthernetInterface eth; + eth.Ethernet(); //Use DHCP problème eth.connect(); - printf("\nServer IP Address is %s\n", eth.get_ip_address()); + printf("\nClient IP Address is %s \n", eth.get_ip_address()); + + UDPSocket sock; + sock.UDPSocket(); // problème + + SocketAddress echo_server; + echo_server.set_ip_address(ECHO_SERVER_ADDRESS); + echo_server.set_port(ECHO_SERVER_PORT); - UDPSocket server; - server.bind(ECHO_SERVER_PORT); + char out_buffer[] = "Hello World"; + printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); + sock.sendto(echo_server, out_buffer, sizeof(out_buffer)); + + char in_buffer[256]; + int n = sock.recvfrom(&echo_server, in_buffer, sizeof(in_buffer)); + + in_buffer[n] = '\0'; + printf("Received message from server: '%s'\n", in_buffer); + + sock.close(); - SocketAddress client; - char buffer[256]; - while (true) { - printf("\nWaiting for UDP packet...\n"); - int n = server.recvfrom(client, buffer, sizeof(buffer)); - buffer[n] = '\0'; - - printf("Received packet from: %s\n", client.get_ip_address()); - printf("Packet contents : '%s'\n",buffer); - printf("Sending Packet back to Client\n"); - server.sendto(client, buffer, n); - } -} \ No newline at end of file + eth.disconnect(); + while(1) {} +} + +// code python + +/* +import socket +import signal +import sys + +def signal_handler(signal, frame): + print 'You pressed Ctrl+C!' + sock.close() + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + +ECHO_PORT = 7 + +print 'Server Running at ', socket.gethostbyname(socket.gethostname()) +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock.bind(('', ECHO_PORT)) + +while True: + print "waiting for UDP data packet..." + data, address = sock.recvfrom(256) + print "Received packet from", address, "with data",data + print "Sending packet back to client" + sock.sendto(data, address) +*/ \ No newline at end of file