Jesper Taxbøl
/
TCPSpeedTest
I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong
main.cpp@0:66300c77c6e9, 2011-03-29 (annotated)
- Committer:
- tax
- Date:
- Tue Mar 29 13:20:15 2011 +0000
- Revision:
- 0:66300c77c6e9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tax | 0:66300c77c6e9 | 1 | #include "mbed.h" |
tax | 0:66300c77c6e9 | 2 | #include "EthernetNetIf.h" |
tax | 0:66300c77c6e9 | 3 | #include "TCPSocket.h" |
tax | 0:66300c77c6e9 | 4 | #include "netif.h" |
tax | 0:66300c77c6e9 | 5 | |
tax | 0:66300c77c6e9 | 6 | /* |
tax | 0:66300c77c6e9 | 7 | #Use the following python script as server |
tax | 0:66300c77c6e9 | 8 | |
tax | 0:66300c77c6e9 | 9 | import os.path |
tax | 0:66300c77c6e9 | 10 | from socket import * |
tax | 0:66300c77c6e9 | 11 | from datetime import * |
tax | 0:66300c77c6e9 | 12 | |
tax | 0:66300c77c6e9 | 13 | HOST = '' |
tax | 0:66300c77c6e9 | 14 | PORT = 1234 |
tax | 0:66300c77c6e9 | 15 | ADDR = (HOST,PORT) |
tax | 0:66300c77c6e9 | 16 | BUFSIZE = 4096 |
tax | 0:66300c77c6e9 | 17 | |
tax | 0:66300c77c6e9 | 18 | serv = socket(AF_INET,SOCK_STREAM) |
tax | 0:66300c77c6e9 | 19 | |
tax | 0:66300c77c6e9 | 20 | serv.bind((ADDR)) |
tax | 0:66300c77c6e9 | 21 | serv.listen(5) |
tax | 0:66300c77c6e9 | 22 | print 'listening...' |
tax | 0:66300c77c6e9 | 23 | while 1: |
tax | 0:66300c77c6e9 | 24 | conn,addr = serv.accept() |
tax | 0:66300c77c6e9 | 25 | print 'Someone connected from %s' % (addr[0]) |
tax | 0:66300c77c6e9 | 26 | print addr |
tax | 0:66300c77c6e9 | 27 | i = 0 |
tax | 0:66300c77c6e9 | 28 | while(1): |
tax | 0:66300c77c6e9 | 29 | filename = str(i)+".txt" |
tax | 0:66300c77c6e9 | 30 | if not os.path.isfile(filename): |
tax | 0:66300c77c6e9 | 31 | break |
tax | 0:66300c77c6e9 | 32 | i=i+1 |
tax | 0:66300c77c6e9 | 33 | |
tax | 0:66300c77c6e9 | 34 | FILE = open(filename,"w") |
tax | 0:66300c77c6e9 | 35 | starttime = datetime.now() |
tax | 0:66300c77c6e9 | 36 | amount = 0 |
tax | 0:66300c77c6e9 | 37 | while(1): |
tax | 0:66300c77c6e9 | 38 | data = conn.recv(BUFSIZE) |
tax | 0:66300c77c6e9 | 39 | if data: |
tax | 0:66300c77c6e9 | 40 | if len(data) > 0: |
tax | 0:66300c77c6e9 | 41 | amount += len(data) |
tax | 0:66300c77c6e9 | 42 | FILE.write(data) |
tax | 0:66300c77c6e9 | 43 | conn.recv(BUFSIZE) |
tax | 0:66300c77c6e9 | 44 | else: |
tax | 0:66300c77c6e9 | 45 | print "disconnected" |
tax | 0:66300c77c6e9 | 46 | break; |
tax | 0:66300c77c6e9 | 47 | |
tax | 0:66300c77c6e9 | 48 | c = (datetime.now() - starttime) |
tax | 0:66300c77c6e9 | 49 | milliseconds = (c.days * 24 * 60 * 60 + c.seconds) * 1000 + c.microseconds / 1000.0 |
tax | 0:66300c77c6e9 | 50 | if milliseconds > 0: |
tax | 0:66300c77c6e9 | 51 | print "%d bytes in %f seconds -> %f B/s" % (amount , (milliseconds/1000.0), amount / (milliseconds/1000.0)) |
tax | 0:66300c77c6e9 | 52 | conn.close() |
tax | 0:66300c77c6e9 | 53 | FILE.close() |
tax | 0:66300c77c6e9 | 54 | |
tax | 0:66300c77c6e9 | 55 | */ |
tax | 0:66300c77c6e9 | 56 | |
tax | 0:66300c77c6e9 | 57 | |
tax | 0:66300c77c6e9 | 58 | |
tax | 0:66300c77c6e9 | 59 | EthernetNetIf *eth; |
tax | 0:66300c77c6e9 | 60 | |
tax | 0:66300c77c6e9 | 61 | //Socket to upload data |
tax | 0:66300c77c6e9 | 62 | Host* tcpAddr; |
tax | 0:66300c77c6e9 | 63 | TCPSocket* tcp; |
tax | 0:66300c77c6e9 | 64 | bool socket_writable = false; |
tax | 0:66300c77c6e9 | 65 | bool socket_connected = false; |
tax | 0:66300c77c6e9 | 66 | |
tax | 0:66300c77c6e9 | 67 | #define PACKET_SIZE 300 |
tax | 0:66300c77c6e9 | 68 | #define PACKET_COUNT 20 |
tax | 0:66300c77c6e9 | 69 | |
tax | 0:66300c77c6e9 | 70 | |
tax | 0:66300c77c6e9 | 71 | unsigned long x = 0; |
tax | 0:66300c77c6e9 | 72 | char buffer[PACKET_SIZE]; |
tax | 0:66300c77c6e9 | 73 | |
tax | 0:66300c77c6e9 | 74 | void sendNextChunk() |
tax | 0:66300c77c6e9 | 75 | { |
tax | 0:66300c77c6e9 | 76 | if(x<PACKET_SIZE*PACKET_COUNT) |
tax | 0:66300c77c6e9 | 77 | { |
tax | 0:66300c77c6e9 | 78 | unsigned long y = x; |
tax | 0:66300c77c6e9 | 79 | for(int i=0;i<PACKET_SIZE;i++) |
tax | 0:66300c77c6e9 | 80 | { |
tax | 0:66300c77c6e9 | 81 | buffer[i]=(y++)%256; |
tax | 0:66300c77c6e9 | 82 | } |
tax | 0:66300c77c6e9 | 83 | int ret = tcp->send(buffer, PACKET_SIZE); |
tax | 0:66300c77c6e9 | 84 | if(ret > 0) |
tax | 0:66300c77c6e9 | 85 | { |
tax | 0:66300c77c6e9 | 86 | x+=ret; |
tax | 0:66300c77c6e9 | 87 | printf("%d \r\n", x); |
tax | 0:66300c77c6e9 | 88 | } |
tax | 0:66300c77c6e9 | 89 | else |
tax | 0:66300c77c6e9 | 90 | { |
tax | 0:66300c77c6e9 | 91 | printf("some error: %d\r\n", ret); |
tax | 0:66300c77c6e9 | 92 | } |
tax | 0:66300c77c6e9 | 93 | } |
tax | 0:66300c77c6e9 | 94 | } |
tax | 0:66300c77c6e9 | 95 | |
tax | 0:66300c77c6e9 | 96 | |
tax | 0:66300c77c6e9 | 97 | void onTCPSocketEvent(TCPSocketEvent e) |
tax | 0:66300c77c6e9 | 98 | { |
tax | 0:66300c77c6e9 | 99 | time_t t_seconds = time(NULL); |
tax | 0:66300c77c6e9 | 100 | switch(e) |
tax | 0:66300c77c6e9 | 101 | { |
tax | 0:66300c77c6e9 | 102 | case TCPSOCKET_CONNECTED: |
tax | 0:66300c77c6e9 | 103 | printf("Connected\r\n"); |
tax | 0:66300c77c6e9 | 104 | case TCPSOCKET_WRITEABLE: |
tax | 0:66300c77c6e9 | 105 | sendNextChunk(); |
tax | 0:66300c77c6e9 | 106 | break; |
tax | 0:66300c77c6e9 | 107 | case TCPSOCKET_READABLE: |
tax | 0:66300c77c6e9 | 108 | break; |
tax | 0:66300c77c6e9 | 109 | case TCPSOCKET_CONTIMEOUT: |
tax | 0:66300c77c6e9 | 110 | case TCPSOCKET_CONRST: |
tax | 0:66300c77c6e9 | 111 | case TCPSOCKET_CONABRT: |
tax | 0:66300c77c6e9 | 112 | case TCPSOCKET_ERROR: |
tax | 0:66300c77c6e9 | 113 | socket_writable = false; |
tax | 0:66300c77c6e9 | 114 | printf("%s Connection Error: %i\r\n", ctime(&t_seconds),e); |
tax | 0:66300c77c6e9 | 115 | tcp->close(); |
tax | 0:66300c77c6e9 | 116 | socket_connected=false; |
tax | 0:66300c77c6e9 | 117 | break; |
tax | 0:66300c77c6e9 | 118 | case TCPSOCKET_DISCONNECTED: |
tax | 0:66300c77c6e9 | 119 | socket_writable = false; |
tax | 0:66300c77c6e9 | 120 | printf("%s Disconnected\r\n", ctime(&t_seconds)); |
tax | 0:66300c77c6e9 | 121 | tcp->close(); |
tax | 0:66300c77c6e9 | 122 | socket_connected=false; |
tax | 0:66300c77c6e9 | 123 | break; |
tax | 0:66300c77c6e9 | 124 | default: |
tax | 0:66300c77c6e9 | 125 | printf("%s Unknown callback\r\n", ctime(&t_seconds)); |
tax | 0:66300c77c6e9 | 126 | break; |
tax | 0:66300c77c6e9 | 127 | } |
tax | 0:66300c77c6e9 | 128 | } |
tax | 0:66300c77c6e9 | 129 | |
tax | 0:66300c77c6e9 | 130 | int main() |
tax | 0:66300c77c6e9 | 131 | { |
tax | 0:66300c77c6e9 | 132 | eth = new EthernetNetIf(); |
tax | 0:66300c77c6e9 | 133 | tcp = new TCPSocket(); |
tax | 0:66300c77c6e9 | 134 | tcp->setOnEvent(&onTCPSocketEvent); |
tax | 0:66300c77c6e9 | 135 | EthernetErr ethErr = eth->setup(); // thats using dhcp |
tax | 0:66300c77c6e9 | 136 | if( ethErr == ETH_OK ) |
tax | 0:66300c77c6e9 | 137 | { |
tax | 0:66300c77c6e9 | 138 | IpAddr ip = eth->getIp(); |
tax | 0:66300c77c6e9 | 139 | printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); |
tax | 0:66300c77c6e9 | 140 | } |
tax | 0:66300c77c6e9 | 141 | tcpAddr = new Host(IpAddr(192,168,1,18), 1234, ""); |
tax | 0:66300c77c6e9 | 142 | IpAddr ip = tcpAddr->getIp(); |
tax | 0:66300c77c6e9 | 143 | printf("Server IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); |
tax | 0:66300c77c6e9 | 144 | tcp->connect(*tcpAddr); |
tax | 0:66300c77c6e9 | 145 | Timer t; |
tax | 0:66300c77c6e9 | 146 | t.start(); |
tax | 0:66300c77c6e9 | 147 | printf("start\r\n"); |
tax | 0:66300c77c6e9 | 148 | while(x<PACKET_SIZE*PACKET_COUNT) |
tax | 0:66300c77c6e9 | 149 | { |
tax | 0:66300c77c6e9 | 150 | Net::poll(); |
tax | 0:66300c77c6e9 | 151 | } |
tax | 0:66300c77c6e9 | 152 | printf("stop\r\n"); |
tax | 0:66300c77c6e9 | 153 | tcp->close(); |
tax | 0:66300c77c6e9 | 154 | t.stop(); |
tax | 0:66300c77c6e9 | 155 | printf("%d bytes in %f seconds %f\r\n", PACKET_SIZE*PACKET_COUNT, t.read(), (PACKET_SIZE*PACKET_COUNT)/t.read()); |
tax | 0:66300c77c6e9 | 156 | while(1){} |
tax | 0:66300c77c6e9 | 157 | } |