I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1
tax 0:66300c77c6e9 2 /*
tax 0:66300c77c6e9 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
tax 0:66300c77c6e9 4
tax 0:66300c77c6e9 5 Permission is hereby granted, free of charge, to any person obtaining a copy
tax 0:66300c77c6e9 6 of this software and associated documentation files (the "Software"), to deal
tax 0:66300c77c6e9 7 in the Software without restriction, including without limitation the rights
tax 0:66300c77c6e9 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tax 0:66300c77c6e9 9 copies of the Software, and to permit persons to whom the Software is
tax 0:66300c77c6e9 10 furnished to do so, subject to the following conditions:
tax 0:66300c77c6e9 11
tax 0:66300c77c6e9 12 The above copyright notice and this permission notice shall be included in
tax 0:66300c77c6e9 13 all copies or substantial portions of the Software.
tax 0:66300c77c6e9 14
tax 0:66300c77c6e9 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tax 0:66300c77c6e9 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tax 0:66300c77c6e9 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tax 0:66300c77c6e9 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tax 0:66300c77c6e9 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tax 0:66300c77c6e9 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tax 0:66300c77c6e9 21 THE SOFTWARE.
tax 0:66300c77c6e9 22 */
tax 0:66300c77c6e9 23
tax 0:66300c77c6e9 24 /** \file
tax 0:66300c77c6e9 25 TCP Socket header file
tax 0:66300c77c6e9 26 */
tax 0:66300c77c6e9 27
tax 0:66300c77c6e9 28 #ifndef TCPSOCKET_H
tax 0:66300c77c6e9 29 #define TCPSOCKET_H
tax 0:66300c77c6e9 30
tax 0:66300c77c6e9 31 #include "core/net.h"
tax 0:66300c77c6e9 32 #include "core/host.h"
tax 0:66300c77c6e9 33 //Essentially it is a safe interface to NetTcpSocket
tax 0:66300c77c6e9 34
tax 0:66300c77c6e9 35 ///TCP Socket error codes
tax 0:66300c77c6e9 36 enum TCPSocketErr
tax 0:66300c77c6e9 37 {
tax 0:66300c77c6e9 38 __TCPSOCKET_MIN = -0xFFFF,
tax 0:66300c77c6e9 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
tax 0:66300c77c6e9 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
tax 0:66300c77c6e9 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
tax 0:66300c77c6e9 42 TCPSOCKET_MEM, ///<Not enough mem
tax 0:66300c77c6e9 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
tax 0:66300c77c6e9 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
tax 0:66300c77c6e9 45 TCPSOCKET_RST, ///<Connection was reset by remote host
tax 0:66300c77c6e9 46 //...
tax 0:66300c77c6e9 47 TCPSOCKET_OK = 0 ///<Success
tax 0:66300c77c6e9 48 };
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 ///TCP Socket Events
tax 0:66300c77c6e9 51 enum TCPSocketEvent
tax 0:66300c77c6e9 52 {
tax 0:66300c77c6e9 53 TCPSOCKET_CONNECTED, ///<Connected to host
tax 0:66300c77c6e9 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
tax 0:66300c77c6e9 55 TCPSOCKET_READABLE, ///<Data in buf
tax 0:66300c77c6e9 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
tax 0:66300c77c6e9 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
tax 0:66300c77c6e9 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
tax 0:66300c77c6e9 59 TCPSOCKET_CONABRT, ///<Connection was aborted
tax 0:66300c77c6e9 60 TCPSOCKET_ERROR, ///<Unknown error
tax 0:66300c77c6e9 61 TCPSOCKET_DISCONNECTED ///<Disconnected
tax 0:66300c77c6e9 62 };
tax 0:66300c77c6e9 63
tax 0:66300c77c6e9 64 class NetTcpSocket;
tax 0:66300c77c6e9 65 enum NetTcpSocketEvent;
tax 0:66300c77c6e9 66
tax 0:66300c77c6e9 67 ///This is a simple TCP Socket class
tax 0:66300c77c6e9 68 /**
tax 0:66300c77c6e9 69 This class exposes an API to deal with TCP Sockets
tax 0:66300c77c6e9 70 */
tax 0:66300c77c6e9 71 class TCPSocket
tax 0:66300c77c6e9 72 {
tax 0:66300c77c6e9 73 public:
tax 0:66300c77c6e9 74 ///Creates a new socket
tax 0:66300c77c6e9 75 TCPSocket();
tax 0:66300c77c6e9 76 protected:
tax 0:66300c77c6e9 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
tax 0:66300c77c6e9 78 public:
tax 0:66300c77c6e9 79 ///Closes if needed and destroys the socket
tax 0:66300c77c6e9 80 ~TCPSocket(); //close()
tax 0:66300c77c6e9 81
tax 0:66300c77c6e9 82 ///Binds the socket to (local) host
tax 0:66300c77c6e9 83 TCPSocketErr bind(const Host& me);
tax 0:66300c77c6e9 84
tax 0:66300c77c6e9 85 ///Starts listening
tax 0:66300c77c6e9 86 TCPSocketErr listen();
tax 0:66300c77c6e9 87
tax 0:66300c77c6e9 88 ///Connects socket to host
tax 0:66300c77c6e9 89 TCPSocketErr connect(const Host& host);
tax 0:66300c77c6e9 90
tax 0:66300c77c6e9 91 ///Accepts connection from client and gets connected socket
tax 0:66300c77c6e9 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
tax 0:66300c77c6e9 93
tax 0:66300c77c6e9 94 ///Sends data
tax 0:66300c77c6e9 95 /*
tax 0:66300c77c6e9 96 @return a negative error code or the number of bytes transmitted
tax 0:66300c77c6e9 97 */
tax 0:66300c77c6e9 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
tax 0:66300c77c6e9 99
tax 0:66300c77c6e9 100 ///Receives data
tax 0:66300c77c6e9 101 /*
tax 0:66300c77c6e9 102 @return a negative error code or the number of bytes received
tax 0:66300c77c6e9 103 */
tax 0:66300c77c6e9 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
tax 0:66300c77c6e9 105
tax 0:66300c77c6e9 106 /* TODO NTH : printf / scanf helpers that call send/recv */
tax 0:66300c77c6e9 107
tax 0:66300c77c6e9 108 ///Closes socket
tax 0:66300c77c6e9 109 TCPSocketErr close();
tax 0:66300c77c6e9 110
tax 0:66300c77c6e9 111 //Callbacks
tax 0:66300c77c6e9 112 ///Setups callback
tax 0:66300c77c6e9 113 /**
tax 0:66300c77c6e9 114 @param pMethod : callback function
tax 0:66300c77c6e9 115 */
tax 0:66300c77c6e9 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
tax 0:66300c77c6e9 117
tax 0:66300c77c6e9 118 class CDummy;
tax 0:66300c77c6e9 119 ///Setups callback
tax 0:66300c77c6e9 120 /**
tax 0:66300c77c6e9 121 @param pItem : instance of class on which to execute the callback method
tax 0:66300c77c6e9 122 @param pMethod : callback method
tax 0:66300c77c6e9 123 */
tax 0:66300c77c6e9 124 template<class T>
tax 0:66300c77c6e9 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
tax 0:66300c77c6e9 126 {
tax 0:66300c77c6e9 127 m_pCbItem = (CDummy*) pItem;
tax 0:66300c77c6e9 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
tax 0:66300c77c6e9 129 }
tax 0:66300c77c6e9 130
tax 0:66300c77c6e9 131 ///Disables callback
tax 0:66300c77c6e9 132 void resetOnEvent();
tax 0:66300c77c6e9 133
tax 0:66300c77c6e9 134 protected:
tax 0:66300c77c6e9 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
tax 0:66300c77c6e9 136 TCPSocketErr checkInst();
tax 0:66300c77c6e9 137
tax 0:66300c77c6e9 138 private:
tax 0:66300c77c6e9 139 NetTcpSocket* m_pNetTcpSocket;
tax 0:66300c77c6e9 140
tax 0:66300c77c6e9 141 CDummy* m_pCbItem;
tax 0:66300c77c6e9 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
tax 0:66300c77c6e9 143
tax 0:66300c77c6e9 144 void (*m_pCb)(TCPSocketEvent);
tax 0:66300c77c6e9 145
tax 0:66300c77c6e9 146 };
tax 0:66300c77c6e9 147
tax 0:66300c77c6e9 148 #endif