Results 1 to 5 of 5

Thread: TCP Server and client communications

  1. #1
    Join Date
    Jun 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question TCP Server and client communications

    I have a program to setup TCP clients and servers using threading and I can not figure out why it is behaving in the following way:
    When the program first runs I am able to send files or messages from the client to the server or from the server to the client. But once I send anything from the server to the client, that particular client is no longer able to send either files or messages to the server.

    I'm currently using the signals/slots method and can not see how it is becoming essentially biased.

    Any ideas on what could be causing my problems?

    Thank You,
    Rod

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TCP Server and client communications

    No idea, perhaps you could post the relevent parts of code?

  3. #3
    Join Date
    Jun 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TCP Server and client communications

    Here are two portions of my threading code. I'm afraid that the entire program is a bit intertwined. If this is not enough information I can post other parts as well.

    Qt Code:
    1. #include "cio_thread.h"
    2.  
    3. #include <QTcpSocket>
    4. #include <QHostAddress>
    5. #include <QEvent>
    6.  
    7. #include "cio_event.h"
    8.  
    9. //-----------------------------------------------------------------------------
    10. // Constructor
    11. //-----------------------------------------------------------------------------
    12.  
    13. CIO_Thread::CIO_Thread( QObject* Parent ) : QThread( Parent )
    14. {
    15. pSocket = 0;
    16. Terminating = false;
    17. }
    18.  
    19. //-----------------------------------------------------------------------------
    20. // Connection
    21. //-----------------------------------------------------------------------------
    22.  
    23. const char* CIO_Thread::Socket_State()
    24. {
    25. if ( pSocket == 0 ) return "The socket is unitialized";
    26.  
    27. switch ( pSocket->state() )
    28. {
    29. case QAbstractSocket::UnconnectedState :
    30. return "The socket is not connected";
    31. case QAbstractSocket::HostLookupState :
    32. return "The socket is performing a host name lookup";
    33. case QAbstractSocket::ConnectingState :
    34. return "The socket has started establishing a connection";
    35. case QAbstractSocket::ConnectedState :
    36. return "A connection is established";
    37. case QAbstractSocket::BoundState :
    38. return "The socket is bound to an address and port (for servers)";
    39. case QAbstractSocket::ClosingState :
    40. return "The socket is about to close (data may still be waiting to be written)";
    41. case QAbstractSocket::ListeningState :
    42. return "For internal use only";
    43. default:
    44. return "The socket state is unknown";
    45. }
    46. }
    47.  
    48. QString CIO_Thread::Socket_Peer()
    49. {
    50. if ( pSocket == 0 ) return "Nobody";
    51.  
    52. QString Peer = "Name = " + pSocket->peerName() + ", ";
    53. Peer += "Address = " + pSocket->peerAddress().toString() + ", ";
    54. Peer += "Port = " + QString::number( pSocket->peerPort() );
    55.  
    56. return Peer;
    57. }
    58.  
    59. void CIO_Thread::Socket_Disconnect()
    60. {
    61. pSocket->disconnectFromHost();
    62.  
    63. if ( pSocket->state() != QAbstractSocket::UnconnectedState )
    64. {
    65. pSocket->waitForDisconnected();
    66. }
    67.  
    68. pSocket = 0;
    69.  
    70. emit Termination_Emit();
    71. }
    72.  
    73. void CIO_Thread::Disconnect_Internal( bool Notify_Buddy )
    74. {
    75. if ( Notify_Buddy ) Data.Command_Write_Terminate();
    76.  
    77. Terminating = true;
    78. quit();
    79. }
    80.  
    81. //-----------------------------------------------------------------------------
    82.  
    83. bool CIO_Thread::Connect()
    84. {
    85. Terminating = false;
    86. start();
    87.  
    88. return true;
    89. }
    90.  
    91. void CIO_Thread::Disconnect()
    92. {
    93. if ( !isRunning() ) return;
    94.  
    95. Termination_Message = "Disconnection by request (socket state = ";
    96. Termination_Message += Socket_State();
    97. Termination_Message += ").";
    98.  
    99. Disconnect_Internal( true );
    100. }
    101.  
    102. //-----------------------------------------------------------------------------
    103. // Connection States
    104. //-----------------------------------------------------------------------------
    105.  
    106. CIO_Thread::Connection_States CIO_Thread::Connection_State()
    107. {
    108. if ( !isRunning() ) return DISCONNECTED;
    109. if ( pSocket == 0 ) return DISCONNECTED;
    110.  
    111. if ( pSocket->state() == QAbstractSocket::ConnectedState ) return CONNECTED;
    112.  
    113. return CONNECTING;
    114. }
    115.  
    116. //-----------------------------------------------------------------------------
    117. // Data
    118. //-----------------------------------------------------------------------------
    119.  
    120. bool CIO_Thread::DW_Terminate()
    121. {
    122. if ( !Data.Error_Is() ) return false;
    123.  
    124. Termination_Message = Data.Error_Get();
    125. Disconnect_Internal( true );
    126.  
    127. return true;
    128. }
    129.  
    130. void CIO_Thread::Data_Write()
    131. {
    132. while ( Data.Messages_Out_Write() );
    133. if ( DW_Terminate() ) return;
    134.  
    135. while ( Data.XML_Out_Write() );
    136. if ( DW_Terminate() ) return;
    137.  
    138. while ( Data.File_Out_Write() );
    139. if ( DW_Terminate() ) return;
    140.  
    141. pSocket->flush();
    142. }
    143.  
    144. //-----------------------------------------------------------------------------
    145.  
    146. void CIO_Thread::Path_Set( const QString& Path )
    147. {
    148. Data.File_Save_Path = Path;
    149. }
    150.  
    151. QString CIO_Thread::Path_Get()
    152. {
    153. return Data.File_Save_Path;
    154. }
    155.  
    156. //-----------------------------------------------------------------------------
    157. // Overrides
    158. //-----------------------------------------------------------------------------
    159.  
    160. void CIO_Thread::run()
    161. {
    162. Data.Stream_Activate( pSocket );
    163.  
    164. // Start Loop
    165.  
    166. exec();
    167.  
    168. // Blow this taco stand
    169.  
    170. Socket_Disconnect();
    171. }
    172.  
    173. void CIO_Thread::customEvent( QEvent* event )
    174. {
    175. if ( Terminating ) return;
    176. if ( pSocket == 0 ) return;
    177.  
    178. event->accept();
    179.  
    180. // Process Command
    181.  
    182. CIO_Event* pEvent = (CIO_Event*) event;
    183.  
    184. switch ( pEvent->Command )
    185. {
    186. case CIO_Event::WRITE_MESSAGE :
    187. Data.Messages_Out_Add( pEvent->Text );
    188. break;
    189. case CIO_Event::WRITE_XML :
    190. Data.XML_Out_Add( pEvent->xDoc );
    191. break;
    192. case CIO_Event::WRITE_FILE :
    193. Data.File_Out_Add( pEvent->Text );
    194. break;
    195. }
    196.  
    197. Data_Write();
    198. }
    199.  
    200. //-----------------------------------------------------------------------------
    201. // Slots
    202. //-----------------------------------------------------------------------------
    203.  
    204. Q_DECLARE_METATYPE( QAbstractSocket::SocketError )
    205.  
    206. void CIO_Thread::Slots_Connect( QTcpSocket& Socket )
    207. {
    208. pSocket = &Socket;
    209.  
    210. // Initialize Socket Handlers
    211.  
    212. connect
    213. (
    214. pSocket, SIGNAL( hostFound () ),
    215. this , SLOT ( Socket_Host_Found() ),
    216. Qt::QueuedConnection
    217. );
    218.  
    219. connect
    220. (
    221. pSocket, SIGNAL( connected () ),
    222. this , SLOT ( Socket_Connected() ),
    223. Qt::QueuedConnection
    224. );
    225.  
    226. qRegisterMetaType<QAbstractSocket::SocketError>( "QAbstractSocket::SocketError" );
    227.  
    228. connect
    229. (
    230. pSocket, SIGNAL( error ( QAbstractSocket::SocketError ) ),
    231. this , SLOT ( Socket_Error( ) ),
    232. Qt::QueuedConnection
    233. );
    234.  
    235. // Initialize Data Handlers
    236.  
    237. connect
    238. (
    239. pSocket, SIGNAL( readyRead() ),
    240. this , SLOT ( Data_Read() ),
    241. Qt::QueuedConnection
    242. );
    243.  
    244. }
    245.  
    246. //-----------------------------------------------------------------------------
    247.  
    248. void CIO_Thread::Socket_Host_Found()
    249. {
    250. Host_Found_Emit();
    251. }
    252.  
    253. void CIO_Thread::Socket_Connected()
    254. {
    255. emit Connected( Socket_State() );
    256. }
    257.  
    258. void CIO_Thread::Socket_Error()
    259. {
    260. if ( pSocket == 0 ) return;
    261.  
    262. Termination_Message = "Socket Error - " + pSocket->errorString();
    263. Disconnect_Internal( false );
    264. }
    265.  
    266. //-----------------------------------------------------------------------------
    267.  
    268. void CIO_Thread::Data_Read()
    269. {
    270. if ( Terminating ) return;
    271.  
    272. // Process incoming data
    273.  
    274. bool Stuff = pSocket->bytesAvailable() > 0;
    275.  
    276. while ( Stuff )
    277. {
    278. if ( !Data.Command_Read() )
    279. {
    280. Termination_Message = Data.Error_Get();
    281. Disconnect_Internal( true );
    282. return;
    283. }
    284.  
    285. if ( Data.Command_Is_Terminate() )
    286. {
    287. Termination_Message = "Peer requested termination.";
    288. Disconnect_Internal( false );
    289. return;
    290. }
    291.  
    292. Stuff = pSocket->bytesAvailable() > 0;
    293. }
    294.  
    295. // Notify your master
    296.  
    297. while ( Data.Messages_In_Present() )
    298. {
    299. Message_Read_Emit( Data.Messages_In_Get() );
    300. }
    301.  
    302. while ( Data.XML_In_Present() )
    303. {
    304. XML_Read_Emit( Data.XML_In_Get() );
    305. }
    306.  
    307. while ( Data.File_In_Present() )
    308. {
    309. File_Read_Emit( Data.File_In_Get() );
    310. }
    311. }
    312.  
    313. void CIO_Thread::Data_Write_Message( const QString& Message )
    314. {
    315. Data.Message_Write( Message );
    316. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2014
    Posts
    12
    Thanks
    4

    Default Re: TCP Server and client communications

    Did you manage to solve this?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TCP Server and client communications

    Given the three years that have elapsed I guess the answer is probably yes.

    Your problem, whatever it is, is unlikely to be identical. Why not start your own thread and explain how you need help?
    As a starting hint: You rarely need threads anywhere near Qt networking.

Similar Threads

  1. Client/server application
    By sali555 in forum Qt Programming
    Replies: 5
    Last Post: 22nd April 2011, 09:55
  2. TCP Client / Server
    By bmarsden in forum Qt Programming
    Replies: 4
    Last Post: 8th October 2010, 10:18
  3. tcp QT server - .Net c# client
    By soniaerm in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2010, 22:15
  4. Client Server
    By electronicboy in forum General Programming
    Replies: 3
    Last Post: 29th October 2009, 10:10
  5. client-server how?
    By nongentesimus in forum Newbie
    Replies: 6
    Last Post: 28th November 2006, 09:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.