Hello there, my name is Fabian and im coding a P2P network app with Qt. It works fine as long is i loop back to localhost, but if i try to connect to another instance of my app over the internet the TcpSocket throws a "Socket timeout" or sometimes a "Connection timeout". The Server-Part is called "Cortex", the Client/Socket-Part is called Synapse. I forwarded the port on my router and my firewall does NOT block the app. You can test the app by your self : http://singularity.us.to/dev/steam/cacheNET/cachenet.7z

so here is the code of the TcpServer(Cortex):

cortex_src.zip

Here is the code of the TcpSocket(Synapse):

synapse.h
Qt Code:
  1. #ifndef SYNAPSE_H
  2. #define SYNAPSE_H
  3.  
  4. #include <QtNetwork>
  5. #include "packet.h"
  6.  
  7. class Synapse : public QThread{
  8. Q_OBJECT
  9. public:
  10. Synapse(QObject *parent_, QTcpSocket *tcpSocket_, int queuelimit_, uint32_t blocksize_);
  11. Synapse(QObject *parent_, QHostAddress address_, quint16 port_, int queuelimit_, uint32_t blocksize_);
  12. QHostAddress get_peeraddress(void){return tcpSocket->peerAddress();}
  13. QString errorString(void){return errorStr;}
  14. bool is_connected(void){return connected;}
  15. protected:
  16. void run();
  17. public slots:
  18. void close(void);
  19. bool add_packet(Packet *packet);
  20. private slots:
  21. void receive_packet();
  22. bool send_packet(Packet *packet);
  23. void send_next_packet(void);
  24. private:
  25. QTcpSocket *tcpSocket;
  26. QObject *parent;
  27.  
  28. Packet *curPacket;
  29. QList<Packet*> packet_queue;
  30.  
  31. int queuetlimit;
  32. uint32_t curSize, maxSize, blockSize;
  33.  
  34. bool connected, sending;
  35.  
  36. QString errorStr;
  37.  
  38. void ident_packet();
  39. signals:
  40. void send_dbgmsg(QString);
  41. void get_next_packet(void);
  42. };
  43.  
  44. #endif // SYNAPSE_H
To copy to clipboard, switch view to plain text mode 

synapse.cpp
Qt Code:
  1. #include "synapse.h"
  2.  
  3. Synapse::Synapse(QObject *parent_, QTcpSocket *tcpSocket_, int queuelimit_, uint32_t blocksize_){
  4. tcpSocket = tcpSocket_;
  5. parent = parent_;
  6. queuetlimit = queuelimit_;
  7. blockSize = blocksize_;
  8. maxSize = curSize = 0;
  9. sending = false;
  10. connected = true;
  11. }
  12.  
  13. Synapse::Synapse(QObject *parent_, QHostAddress address_, quint16 port_, int queuelimit_, uint32_t blocksize_){
  14. parent = parent_;
  15. tcpSocket = new QTcpSocket(this);
  16.  
  17. tcpSocket->connectToHost(address_,port_);
  18. if (!tcpSocket->waitForConnected()){
  19. errorStr = tcpSocket->errorString(); //this is where the timeout occurs :(
  20. connected = false;
  21. return;
  22. }
  23. queuetlimit = queuelimit_;
  24. blockSize = blocksize_;
  25. sending = false;
  26.  
  27. maxSize = curSize = 0;
  28. connected = true;
  29. }
  30.  
  31. void Synapse::run(){
  32. if (!connected) return;
  33.  
  34. QObject::connect(this,SIGNAL(get_next_packet()),this,SLOT(send_next_packet()));
  35. QObject::connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receive_packet()));
  36. QObject::connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(close()));
  37. QObject::connect(parent,SIGNAL(destroyed()),this,SLOT(close()));
  38. }
  39.  
  40. void Synapse::close(){
  41. //disconnect everything
  42. emit send_dbgmsg("Synapse: disconnected");
  43. if (connected){
  44. tcpSocket->close();
  45. QObject::disconnect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receive_packet()));
  46. QObject::disconnect(this,SIGNAL(get_next_packet()),this,SLOT(send_next_packet()));
  47. QObject::disconnect(tcpSocket,SIGNAL(disconnected()),this,SLOT(close()));
  48. QObject::disconnect(parent,SIGNAL(destroyed()),this,SLOT(close()));
  49.  
  50. if(maxSize != 0 && maxSize != 0){
  51. curPacket->clear();
  52. }
  53.  
  54. for (int i = 0;i<packet_queue.size();i++){
  55. packet_queue[i]->clear();
  56. delete packet_queue[i];
  57. }
  58. packet_queue.clear();
  59. }
  60.  
  61. this->deleteLater();
  62. }
  63.  
  64. void Synapse::receive_packet(){
  65. qint64 blocksize = tcpSocket->bytesAvailable();
  66. if (blocksize == 0){
  67. emit send_dbgmsg("Synapse: No data avaliable on socket");
  68. return;
  69. }
  70.  
  71. //allocate the buffer
  72. char *data = new char[blocksize];
  73.  
  74. //read form socket
  75. if (tcpSocket->read(data,blocksize) != blocksize){
  76. emit send_dbgmsg(QString("Synapse: %1").arg(tcpSocket->errorString()));
  77. delete data;
  78. return;
  79. }
  80.  
  81. //check if this is a new package
  82. if (curSize == 0 && blocksize >= DESCRIPTOR_SIZE){
  83. //new packet
  84. curPacket = new Packet(data,blocksize);
  85. maxSize = curPacket->get_packet_descriptor().Packetsize;
  86. curSize = curPacket->get_size();//blocksize;
  87. }else{
  88. //write packet block to stream
  89. if(curPacket){
  90. curPacket->serialize_DATA(data,blocksize);
  91. curSize = curPacket->get_size();//blocksize;
  92. }else{
  93. emit send_dbgmsg("Synapse: Invalid reference to packet");
  94. delete data;
  95. curSize = maxSize = 0;
  96. return;
  97. }
  98. }
  99.  
  100. if(curPacket){
  101. if(curSize == maxSize && maxSize > 0){
  102. //stop receiving packets, digest current pne
  103. QObject::disconnect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receive_packet()));
  104. delete data;
  105. if(curPacket->get_packet_descriptor().Version != DESCRIPTOR_VERSION || curPacket->is_descriptor_checksum_valid() == false){
  106. emit send_dbgmsg(QString("Synapse: DescriptorVersion - got %1 expected %2 PacketDescriptor-Checksum - got %3 expected %4").arg(curPacket->get_packet_descriptor().Version).arg(DESCRIPTOR_VERSION).arg(curPacket->compute_descriptor_checksum()).arg(curPacket->get_packet_descriptor().Checksum));
  107. curSize = maxSize = 0;
  108. delete data;
  109. return;
  110. }
  111. curPacket->uncompress_packet();
  112. ident_packet();
  113. return;
  114. }else if(curSize > maxSize){
  115. delete data;
  116. emit send_dbgmsg(QString("Synapse: Received corrupted packet - got %1 expected %2").arg(curSize).arg(maxSize));
  117. curSize = maxSize = 0;
  118. return;
  119. }
  120. }
  121. }
  122.  
  123. bool Synapse::send_packet(Packet *packet){
  124. if(!packet) return false;
  125. packet->finalize_packet();
  126. qint64 size = packet->get_size();
  127. emit send_dbgmsg(QString("Synapse: Sending Format %1 Type %2 Size %3 ID %4 Checksum %5").arg(packet->get_packet_descriptor().Format).arg(packet->get_packet_descriptor().Type).arg(size).arg(packet->get_packet_descriptor().JobID).arg(packet->get_packet_descriptor().Checksum));
  128. if (size <= blockSize){
  129. if (tcpSocket->write(packet->get_data(),size) == -1) return false;
  130. tcpSocket->waitForBytesWritten();
  131. }else{
  132. int i = 0;
  133. while (size >= blockSize){
  134. if (tcpSocket->write(packet->get_data()+(i*blockSize),blockSize) == -1) return false;
  135. tcpSocket->waitForBytesWritten();
  136. size -= blockSize;
  137. i++;
  138. }
  139. if(size > 0){
  140. if(tcpSocket->write(packet->get_data()+(i*blockSize),size) == -1)return false;
  141. tcpSocket->waitForBytesWritten();
  142. }
  143. }
  144. return true;
  145. }
  146.  
  147. void Synapse::ident_packet(void){
  148. Packet::PacketDescriptor descriptor = curPacket->get_packet_descriptor();
  149. emit send_dbgmsg(QString("Synapse: Received Format %1 Type %2 Size %3 ID %4 Checksum %5").arg(descriptor.Format).arg(descriptor.Type).arg(descriptor.Packetsize).arg(descriptor.JobID).arg(descriptor.Checksum));
  150. switch(descriptor.Type){
  151. case Packet::UNKNOWN:
  152. break;
  153. case Packet::CHAT_MSG:
  154. emit send_dbgmsg(QString("Synapse: CHAT_MSG %1").arg(curPacket->deserialize_CHAT_MSG()));
  155. break;
  156. case Packet::REQ_PEERLIST:
  157. break;
  158. //submit to PeerManager and PeerManager sends PeerList backto this Peer to send it
  159. case Packet::REP_PEERLIST:
  160. break;
  161. //submit to PeerManager to add to PeerList
  162. case Packet::FILE:
  163. //debuggin methode
  164. emit send_dbgmsg("Synapse: Writing test.bin...");
  165. std::fbytestream stream; // create the new file
  166. stream.open("test.bin",std::ios::out | std::ios::binary);
  167. if(!stream.is_open()) return;
  168. stream.put<std::bytes>(curPacket->get_content());
  169. stream.flush();
  170. stream.close();
  171. emit send_dbgmsg("Synapse: Writing test.bin was successfull");
  172. break;
  173. }
  174. //add to request list...
  175.  
  176. curSize = maxSize = 0;
  177. //start receiving again after adding this to the request handler
  178. QObject::connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receive_packet()));
  179. }
  180.  
  181. bool Synapse::add_packet(Packet *packet){
  182. //add readwrite lock
  183. if(packet_queue.size()+1 >= queuetlimit) return false;
  184. if(!packet) return false;
  185.  
  186. packet_queue.append(packet);
  187. if (packet_queue.size() == 1 && sending == false) emit get_next_packet();
  188. return true;
  189. }
  190.  
  191. void Synapse::send_next_packet(void){
  192. //add readwrite lock
  193. if(!packet_queue.isEmpty()){
  194. sending = true;
  195. if (send_packet(packet_queue[0])){
  196. //if sending was successfull remove packet from queue
  197. packet_queue[0]->clear();
  198. delete packet_queue[0];
  199. packet_queue.removeFirst();
  200. }else{
  201. //try again two times
  202. if (!send_packet(packet_queue[0])) send_packet(packet_queue[0]);
  203. //remove it whether it worked or not
  204. packet_queue[0]->clear();
  205. delete packet_queue[0];
  206. packet_queue.removeFirst();
  207. }
  208. //reenter the loop
  209. if(!packet_queue.isEmpty()) emit get_next_packet();
  210. sending = false;
  211. }
  212. }
To copy to clipboard, switch view to plain text mode 

Do you have any idea why it wokrs on my system on lokalhost but not over the internet? Feedback and criticism is much appreciated!

Thanks in advance!