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 #include "TCPSocket.h"
tax 0:66300c77c6e9 25 #include "if/net/nettcpsocket.h"
tax 0:66300c77c6e9 26
tax 0:66300c77c6e9 27 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
tax 0:66300c77c6e9 28 {
tax 0:66300c77c6e9 29
tax 0:66300c77c6e9 30 }
tax 0:66300c77c6e9 31
tax 0:66300c77c6e9 32 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
tax 0:66300c77c6e9 33 {
tax 0:66300c77c6e9 34 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
tax 0:66300c77c6e9 35 }
tax 0:66300c77c6e9 36
tax 0:66300c77c6e9 37 TCPSocket::~TCPSocket() //close()
tax 0:66300c77c6e9 38 {
tax 0:66300c77c6e9 39 close();
tax 0:66300c77c6e9 40 }
tax 0:66300c77c6e9 41
tax 0:66300c77c6e9 42 TCPSocketErr TCPSocket::bind(const Host& me)
tax 0:66300c77c6e9 43 {
tax 0:66300c77c6e9 44 TCPSocketErr tcpSocketErr = checkInst();
tax 0:66300c77c6e9 45 if(tcpSocketErr)
tax 0:66300c77c6e9 46 return tcpSocketErr;
tax 0:66300c77c6e9 47 return (TCPSocketErr) m_pNetTcpSocket->bind(me);
tax 0:66300c77c6e9 48 }
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 TCPSocketErr TCPSocket::listen()
tax 0:66300c77c6e9 51 {
tax 0:66300c77c6e9 52 TCPSocketErr tcpSocketErr = checkInst();
tax 0:66300c77c6e9 53 if(tcpSocketErr)
tax 0:66300c77c6e9 54 return tcpSocketErr;
tax 0:66300c77c6e9 55 return (TCPSocketErr) m_pNetTcpSocket->listen();
tax 0:66300c77c6e9 56 }
tax 0:66300c77c6e9 57
tax 0:66300c77c6e9 58 TCPSocketErr TCPSocket::connect(const Host& host)
tax 0:66300c77c6e9 59 {
tax 0:66300c77c6e9 60 TCPSocketErr tcpSocketErr = checkInst();
tax 0:66300c77c6e9 61 if(tcpSocketErr)
tax 0:66300c77c6e9 62 return tcpSocketErr;
tax 0:66300c77c6e9 63 return (TCPSocketErr) m_pNetTcpSocket->connect(host);
tax 0:66300c77c6e9 64 }
tax 0:66300c77c6e9 65
tax 0:66300c77c6e9 66 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket)
tax 0:66300c77c6e9 67 {
tax 0:66300c77c6e9 68 TCPSocketErr tcpSocketErr = checkInst();
tax 0:66300c77c6e9 69 if(tcpSocketErr)
tax 0:66300c77c6e9 70 return tcpSocketErr;
tax 0:66300c77c6e9 71 NetTcpSocket* pNewNetTcpSocket;
tax 0:66300c77c6e9 72 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket);
tax 0:66300c77c6e9 73 if(pNewNetTcpSocket)
tax 0:66300c77c6e9 74 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket);
tax 0:66300c77c6e9 75 return tcpSocketErr;
tax 0:66300c77c6e9 76 }
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len)
tax 0:66300c77c6e9 79 {
tax 0:66300c77c6e9 80 TCPSocketErr tcpSocketErr = checkInst();
tax 0:66300c77c6e9 81 if(tcpSocketErr)
tax 0:66300c77c6e9 82 return tcpSocketErr;
tax 0:66300c77c6e9 83 return m_pNetTcpSocket->send(buf, len);
tax 0:66300c77c6e9 84 }
tax 0:66300c77c6e9 85
tax 0:66300c77c6e9 86 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len)
tax 0:66300c77c6e9 87 {
tax 0:66300c77c6e9 88 TCPSocketErr tcpSocketErr = checkInst();
tax 0:66300c77c6e9 89 if(tcpSocketErr)
tax 0:66300c77c6e9 90 return tcpSocketErr;
tax 0:66300c77c6e9 91 return m_pNetTcpSocket->recv(buf, len);
tax 0:66300c77c6e9 92 }
tax 0:66300c77c6e9 93
tax 0:66300c77c6e9 94 TCPSocketErr TCPSocket::close()
tax 0:66300c77c6e9 95 {
tax 0:66300c77c6e9 96 if(!m_pNetTcpSocket)
tax 0:66300c77c6e9 97 return TCPSOCKET_SETUP;
tax 0:66300c77c6e9 98 m_pNetTcpSocket->resetOnEvent();
tax 0:66300c77c6e9 99 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed)
tax 0:66300c77c6e9 100 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed
tax 0:66300c77c6e9 101 m_pNetTcpSocket = NULL;
tax 0:66300c77c6e9 102 return tcpSocketErr;
tax 0:66300c77c6e9 103 }
tax 0:66300c77c6e9 104
tax 0:66300c77c6e9 105 //Callbacks
tax 0:66300c77c6e9 106 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) )
tax 0:66300c77c6e9 107 {
tax 0:66300c77c6e9 108 m_pCb = pMethod;
tax 0:66300c77c6e9 109 }
tax 0:66300c77c6e9 110
tax 0:66300c77c6e9 111 #if 0 //For info only
tax 0:66300c77c6e9 112 template<class T>
tax 0:66300c77c6e9 113 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
tax 0:66300c77c6e9 114 {
tax 0:66300c77c6e9 115 m_pCbItem = (CDummy*) pItem;
tax 0:66300c77c6e9 116 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
tax 0:66300c77c6e9 117 }
tax 0:66300c77c6e9 118 #endif
tax 0:66300c77c6e9 119
tax 0:66300c77c6e9 120 void TCPSocket::resetOnEvent() //Disable callback
tax 0:66300c77c6e9 121 {
tax 0:66300c77c6e9 122 m_pCb = NULL;
tax 0:66300c77c6e9 123 m_pCbItem = NULL;
tax 0:66300c77c6e9 124 m_pCbMeth = NULL;
tax 0:66300c77c6e9 125 }
tax 0:66300c77c6e9 126
tax 0:66300c77c6e9 127 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e)
tax 0:66300c77c6e9 128 {
tax 0:66300c77c6e9 129 if(m_pCbItem && m_pCbMeth)
tax 0:66300c77c6e9 130 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e);
tax 0:66300c77c6e9 131 else if(m_pCb)
tax 0:66300c77c6e9 132 m_pCb((TCPSocketEvent) e);
tax 0:66300c77c6e9 133 }
tax 0:66300c77c6e9 134
tax 0:66300c77c6e9 135 TCPSocketErr TCPSocket::checkInst()
tax 0:66300c77c6e9 136 {
tax 0:66300c77c6e9 137 if(!m_pNetTcpSocket)
tax 0:66300c77c6e9 138 {
tax 0:66300c77c6e9 139 m_pNetTcpSocket = Net::tcpSocket();
tax 0:66300c77c6e9 140 if(!m_pNetTcpSocket)
tax 0:66300c77c6e9 141 {
tax 0:66300c77c6e9 142 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
tax 0:66300c77c6e9 143 }
tax 0:66300c77c6e9 144 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
tax 0:66300c77c6e9 145 }
tax 0:66300c77c6e9 146 return TCPSOCKET_OK;
tax 0:66300c77c6e9 147 }