You are viewing an older revision! See the latest version
Socket
Network Interfaces
- Socket API
- Ethernet Interface
- VodafoneK3770 Interface
The mbed C++ Socket API is a simple abstraction on top of the Berkeley sockets API.
The Socket library is included within the Networking libraries (Ethernet Interface and VodafoneK3770 Interface).
TCP Socket¶

TCP Echo Server¶
Import program
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 00004 int main (void) { 00005 EthernetInterface eth; 00006 eth.init(); //Use DHCP 00007 eth.connect(); 00008 printf("IP Address is %s\n", eth.getIPAddress()); 00009 00010 TCPSocketServer server; 00011 server.bind(7); 00012 server.listen(1); 00013 00014 while (true) { 00015 printf("\nWait for new connection...\n"); 00016 TCPSocketConnection client; 00017 server.accept(client); 00018 00019 printf("Connection from: %s\n", client.get_address()); 00020 char buffer[256]; 00021 while (true) { 00022 int n = client.receive(buffer, 256, 3000); 00023 if (n <= 0) break; 00024 00025 client.send_all(buffer, n, 3000); 00026 if (n <= 0) break; 00027 } 00028 client.close(); 00029 } 00030 }
You can test the above server running on your mbed, with the following Python script running on your PC:
import socket
ECHO_SERVER_ADDRESS = "10.2.131.195"
ECHO_PORT = 7
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ECHO_SERVER_ADDRESS, ECHO_PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
TCP Echo Client¶
Import program
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 00004 const char* ECHO_SERVER_ADDRESS = "10.2.131.73"; 00005 const int ECHO_PORT = 7; 00006 00007 int main() { 00008 EthernetInterface eth; 00009 eth.init(); //Use DHCP 00010 eth.connect(); 00011 printf("IP Address is %s\n", eth.getIPAddress()); 00012 00013 TCPSocketConnection sock; 00014 while (sock.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) { 00015 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT); 00016 wait(1); 00017 } 00018 00019 char hello[] = "Hello World\n"; 00020 sock.send_all(hello, sizeof(hello) - 1); 00021 00022 char buf[256]; 00023 int n = sock.receive(buf, 256); 00024 // buf[n] = '\0'; 00025 printf("%s", buf); 00026 00027 sock.close(); 00028 eth.disconnect(); 00029 00030 while(true) {} 00031 }
You can test the above Client running on your mbed, with the following Python script running on your PC:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 7))
s.listen(1)
while True:
conn, addr = s.accept()
print 'Connected by', addr
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
UDP Socket¶
UDP Echo Server¶
Import program
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 00004 int main (void) { 00005 EthernetInterface eth; 00006 eth.init(); //Use DHCP 00007 eth.connect(); 00008 printf("IP Address is %s\n", eth.getIPAddress()); 00009 00010 UDPSocket server; 00011 server.bind(7); 00012 00013 char buffer[256]; 00014 while (true) { 00015 printf("\nWait for packet...\n"); 00016 00017 UDPPacket packet(buffer, sizeof(buffer)); 00018 int n = server.receiveFrom(packet); 00019 00020 printf("Received packet from: %s\n", packet.get_address()); 00021 packet.set_data(buffer, n); 00022 server.sendTo(packet); 00023 } 00024 }
You can test the above Server running on your mbed, with the following Python script running on your PC:
import socket
ECHO_SERVER_ADDRESS = '10.2.131.195'
ECHO_PORT = 7
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("Hello World\n", (ECHO_SERVER_ADDRESS, ECHO_PORT))
response = sock.recv(256)
sock.close()
print response