Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C

A Generic C-Language TCP Client Application

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
9 May 2010CPOL4 min read 29.1K   793   20  
A library for writing simple TCP client applications
/// @source      Thread.h
/// @description Public interface for class Thread.
//  See licensing information in the file README.TXT.

// -----------------------------------------------------------------------------
#ifndef __THREAD_H__
#define __THREAD_H__
// -----------------------------------------------------------------------------

#if PLATFORM(Windows)
#include <windows.h>
#endif

#if PLATFORM(Linux)
#include <pthread.h>
#include <unistd.h> // for convenience (provides sleep)
#endif

// -----------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
// -----------------------------------------------------------------------------

// Linux-specific declarations
#if PLATFORM(Linux)

/// Return type of the main function of a thread.
typedef void* THREAD_RESULT;

/// Calling convention for the main function of a thread.
#define __THREAD__ /* nothing! */

/// Type of the main function of a thread main.
typedef THREAD_RESULT ( __THREAD__ *ThreadFunc)(void*);
#define threadfunc THREAD_RESULT  __THREAD__

/// Identification of the current thread.
#define currentThreadID pthread_self()

#define currentProcessID()  ((uint)getpid())

#endif // PLATFORM(Linux)

// -----------------------------------------------------------------------------

// Windows-specific declarations
#if PLATFORM(Windows)

// *** when using CreateThread
// *** /// Return type of the main function of a thread.
// *** typedef DWORD THREAD_RESULT;

// when using _beginthreadex
/// Return type of the main function of a thread.
typedef unsigned THREAD_RESULT;

// *** when using CreateThread
// *** /// Calling convention for the main function of a thread.
// *** #define __THREAD__ WINAPI

// when using _beginthreadex (in fact, WINAPI and __stdcall are identical!)
/// Calling convention for the main function of a thread.
#define __THREAD__ __stdcall

/// Type of the main function of a thread.
typedef THREAD_RESULT ( __THREAD__ *ThreadFunc)(void*);
#define threadfunc THREAD_RESULT  __THREAD__

/// Identification of the current thread.
#define currentThreadID GetCurrentThreadId()

#define currentProcessID()  ((uint)GetCurrentProcessId())

#endif // PLATFORM(Windows)

// -----------------------------------------------------------------------------

// the Thread opaque type
// typedef struct Thread Thread;

// class initialization
/// see Thread::thread_init
extern TC2API void thread_init(void);

// query
// ~~~~~

/// see Thread::thread_selfSeqNo
extern TC2API uint thread_selfSeqNo(void);

/// The current thread id provided by the operating system
#define thread_selfID() ((uint)(currentThreadID))

/// The current process id provided by the operating system
#define thread_pid() currentProcessID()

// operations
// ~~~~~~~~~~

/// see Thread::thread_addThreads
extern TC2API void thread_addThreads(uint, ThreadFunc, void*);

// convenience
// ~~~~~~~~~~~

/// see Thread::thread_printf
extern TC2API void thread_printf(cchar*, ...);

// -----------------------------------------------------------------------------
#ifdef __cplusplus
} // #ifdef __cplusplus
#endif
// -----------------------------------------------------------------------------
#endif // __THREAD_H__
// -----------------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
zvx
Software Developer
Brazil Brazil
I'm a long-time software developer living in Brazil.

I've been developing software for retail and banking automation in C/C++ for many years now. In the old days I even did some COBOL programming, and some assembly for the 8080.

My experience ranges from low level software such as interface code for serial devices for DOS and Windows (bar code scanners, printers, cash dispensers, etc) and goes to writing end user applications for POS terminals and bank ATMs. In between I've done a great deal of TCP/IP programming using the basic Berkeley sockets interface, which is my main interest nowadays.

Comments and Discussions