Click here to Skip to main content
13,045,540 members (70,720 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 5 Dec 2004


, 5 Dec 2004
Rate this:
Please Sign up or sign in to vote.
This article describes how to send and receive data using TCP and worker threads in C++



<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->


This article demonstrates how to send and receive data using TCP/IP in VC++.NET. This alsow describes how to use threads and how to use costom post messages to communicate with threads.


This article uses CSocket object to communicate and WM_USER kind post messages to communicate with the thread. This also uses worker threads.

Creating the thread

The thread is created by using AfxBeginThread(ReceiveMessage,&m_EDT_Port) . This function has two overloads. One to call UI threads and one to call worker threads. We are using a worker thread here. In here "ReceiveMessage" means the function the thread is going to perform and "&m_EDT_Port" means that I'm passing the port number taken from the edit control to the function.

Receive and Send

In this sample program I've used a server socket to receive and a client socket to send. But you can do vice versa. That means you can call CSocket.Send() and CSocket.Receive with both server and client sockets. The thing is you need a server socket in one side and a client socket in the other side to establish the communication. Once you establish the connection you can send and receive from both the client and the server socket.


CSocket* sockServ;
CSocket* sockRecv;

The above variables are defined globally

CSocket sockServT;
CSocket sockRecvT;

sockServ = &sockServT;
sockRecv = &sockRecvT;

State = sockServ->Create(PortNo);

RecvData=new char[500];

int len = sockRecv->Receive(RecvData ,500);

In this block only the required lines of code is shown. In the sample program you will find the other definitions and stuff

Here as you can see we have used a server socket and we first create it with the port number. Then we start listening in that port number. Then we accept the port in a nother socket and use that socket to receive the data. Keep in mind that you can use the same port to send the data too.


DataSend=new char[Data.GetLength()];


As you can see sending using a client socket as simple as this. Just connect the client usint the IP and port and you can start sending or receiving.

So thats it for sending and receiving using TCP


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Kumudu Gunasekara
Software Developer (Senior)
Sri Lanka Sri Lanka
I'm a software engineer working in a leading IT company in Sri Lanka. I'm a Microsoft Certified Technology Specialist. I used to submit articles that I taught would benefit fellow programmers. Unfortunately my last submission was more than 7 years ago. I'm thinking about starting again soon. Check out my full bio
My web site

You may also be interested in...

Comments and Discussions Version? Pin
antonio62216-Jan-11 6:17
memberantonio62216-Jan-11 6:17 
Questionworking with sql server 2005 database in Pin
net_deen10-Mar-08 18:11
membernet_deen10-Mar-08 18:11 
GeneralNot really .NET Pin
v.szabi17-Apr-07 20:50
memberv.szabi17-Apr-07 20:50 
GeneralThe version for VC++ 6.0 Pin
cristitomi13-Mar-07 4:50
membercristitomi13-Mar-07 4:50 
GeneralRe: The version for VC++ 6.0 Pin
cristitomi23-Mar-07 2:41
membercristitomi23-Mar-07 2:41 
Questiondoes not compile in 2005 Pin
sabbir137522-Jul-06 23:48
membersabbir137522-Jul-06 23:48 
AnswerRe: does not compile in 2005 Pin
marcob6919-Nov-06 4:52
membermarcob6919-Nov-06 4:52 
GeneralRe: does not compile in 2005 Pin
bertszoghy30-Jan-07 14:12
memberbertszoghy30-Jan-07 14:12 
AnswerRe: does not compile in 2005 Pin
Stuart russell8-Apr-07 13:32
memberStuart russell8-Apr-07 13:32 
GeneralRe: does not compile in 2005 Pin
dfss19-Jul-07 4:27
memberdfss19-Jul-07 4:27 
GeneralUnable to connect to the port Pin
fluminis3-Feb-05 10:47
memberfluminis3-Feb-05 10:47 
GeneralYou're very helpfull :) Pin
SimCom18-Jan-05 9:16
memberSimCom18-Jan-05 9:16 
Generalnot a new thought Pin
yizumi5-Dec-04 13:16
memberyizumi5-Dec-04 13:16 
QuestionMemLeak ? Pin
KrodEx5-Dec-04 9:12
sussKrodEx5-Dec-04 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Dec 2004
Article Copyright 2004 by Kumudu Gunasekara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid