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.
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "TCPSocket.h" 00004 #include "netif.h" 00005 00006 /* 00007 #Use the following python script as server 00008 00009 import os.path 00010 from socket import * 00011 from datetime import * 00012 00013 HOST = '' 00014 PORT = 1234 00015 ADDR = (HOST,PORT) 00016 BUFSIZE = 4096 00017 00018 serv = socket(AF_INET,SOCK_STREAM) 00019 00020 serv.bind((ADDR)) 00021 serv.listen(5) 00022 print 'listening...' 00023 while 1: 00024 conn,addr = serv.accept() 00025 print 'Someone connected from %s' % (addr[0]) 00026 print addr 00027 i = 0 00028 while(1): 00029 filename = str(i)+".txt" 00030 if not os.path.isfile(filename): 00031 break 00032 i=i+1 00033 00034 FILE = open(filename,"w") 00035 starttime = datetime.now() 00036 amount = 0 00037 while(1): 00038 data = conn.recv(BUFSIZE) 00039 if data: 00040 if len(data) > 0: 00041 amount += len(data) 00042 FILE.write(data) 00043 conn.recv(BUFSIZE) 00044 else: 00045 print "disconnected" 00046 break; 00047 00048 c = (datetime.now() - starttime) 00049 milliseconds = (c.days * 24 * 60 * 60 + c.seconds) * 1000 + c.microseconds / 1000.0 00050 if milliseconds > 0: 00051 print "%d bytes in %f seconds -> %f B/s" % (amount , (milliseconds/1000.0), amount / (milliseconds/1000.0)) 00052 conn.close() 00053 FILE.close() 00054 00055 */ 00056 00057 00058 00059 EthernetNetIf *eth; 00060 00061 //Socket to upload data 00062 Host* tcpAddr; 00063 TCPSocket* tcp; 00064 bool socket_writable = false; 00065 bool socket_connected = false; 00066 00067 #define PACKET_SIZE 300 00068 #define PACKET_COUNT 20 00069 00070 00071 unsigned long x = 0; 00072 char buffer[PACKET_SIZE]; 00073 00074 void sendNextChunk() 00075 { 00076 if(x<PACKET_SIZE*PACKET_COUNT) 00077 { 00078 unsigned long y = x; 00079 for(int i=0;i<PACKET_SIZE;i++) 00080 { 00081 buffer[i]=(y++)%256; 00082 } 00083 int ret = tcp->send(buffer, PACKET_SIZE); 00084 if(ret > 0) 00085 { 00086 x+=ret; 00087 printf("%d \r\n", x); 00088 } 00089 else 00090 { 00091 printf("some error: %d\r\n", ret); 00092 } 00093 } 00094 } 00095 00096 00097 void onTCPSocketEvent(TCPSocketEvent e) 00098 { 00099 time_t t_seconds = time(NULL); 00100 switch(e) 00101 { 00102 case TCPSOCKET_CONNECTED: 00103 printf("Connected\r\n"); 00104 case TCPSOCKET_WRITEABLE: 00105 sendNextChunk(); 00106 break; 00107 case TCPSOCKET_READABLE: 00108 break; 00109 case TCPSOCKET_CONTIMEOUT: 00110 case TCPSOCKET_CONRST: 00111 case TCPSOCKET_CONABRT: 00112 case TCPSOCKET_ERROR: 00113 socket_writable = false; 00114 printf("%s Connection Error: %i\r\n", ctime(&t_seconds),e); 00115 tcp->close(); 00116 socket_connected=false; 00117 break; 00118 case TCPSOCKET_DISCONNECTED: 00119 socket_writable = false; 00120 printf("%s Disconnected\r\n", ctime(&t_seconds)); 00121 tcp->close(); 00122 socket_connected=false; 00123 break; 00124 default: 00125 printf("%s Unknown callback\r\n", ctime(&t_seconds)); 00126 break; 00127 } 00128 } 00129 00130 int main() 00131 { 00132 eth = new EthernetNetIf(); 00133 tcp = new TCPSocket(); 00134 tcp->setOnEvent(&onTCPSocketEvent); 00135 EthernetErr ethErr = eth->setup(); // thats using dhcp 00136 if( ethErr == ETH_OK ) 00137 { 00138 IpAddr ip = eth->getIp(); 00139 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); 00140 } 00141 tcpAddr = new Host(IpAddr(192,168,1,18), 1234, ""); 00142 IpAddr ip = tcpAddr->getIp(); 00143 printf("Server IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); 00144 tcp->connect(*tcpAddr); 00145 Timer t; 00146 t.start(); 00147 printf("start\r\n"); 00148 while(x<PACKET_SIZE*PACKET_COUNT) 00149 { 00150 Net::poll(); 00151 } 00152 printf("stop\r\n"); 00153 tcp->close(); 00154 t.stop(); 00155 printf("%d bytes in %f seconds %f\r\n", PACKET_SIZE*PACKET_COUNT, t.read(), (PACKET_SIZE*PACKET_COUNT)/t.read()); 00156 while(1){} 00157 }
Generated on Tue Jul 12 2022 15:26:15 by
