Click here to Skip to main content
15,886,693 members
Articles / Desktop Programming / MFC
Article

TCP_IP_Send_Receive_Using_VC++.NET

Rate me:
Please Sign up or sign in to vote.
1.28/5 (10 votes)
5 Dec 20042 min read 54K   2.3K   21   14
This article describes how to send and receive data using TCP and worker threads in C++

Server

Client

Introduction

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.

Background

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.

Receive

CSocket* sockServ;
CSocket* sockRecv;

The above variables are defined globally

CSocket sockServT;
CSocket sockRecvT;

sockServ = &sockServT;
sockRecv = &sockRecvT;

State = sockServ->Create(PortNo);

sockServ->Listen();
sockServ->Accept(*sockRecv);
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.

Send

sockClient.Create();
sockClient.Connect(IP,Port);
DataSend=new char[Data.GetLength()];
DataSend=Data.GetBuffer();

sockClient.Send(DataSend,Data.GetLength());
sockClient.Close();

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

License

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


Written By
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

Comments and Discussions

 
QuestionVB.net Version? Pin
antonio62216-Jan-11 6:17
antonio62216-Jan-11 6:17 
Questionworking with sql server 2005 database in asp.net Pin
net_deen10-Mar-08 18:11
net_deen10-Mar-08 18:11 
GeneralNot really .NET Pin
v.szabi17-Apr-07 20:50
v.szabi17-Apr-07 20:50 
GeneralThe version for VC++ 6.0 Pin
cristitomi13-Mar-07 4:50
cristitomi13-Mar-07 4:50 
Hi!
I have ported this code to Visual C++ 6.0
Who is interested, mail me and I will send the zip archive containing the ported projects for both client and server.
Best regards,
Christian.

I am in love with VC++
GeneralRe: The version for VC++ 6.0 Pin
cristitomi23-Mar-07 2:41
cristitomi23-Mar-07 2:41 
Questiondoes not compile in vs.net 2005 Pin
sabbir137522-Jul-06 23:48
sabbir137522-Jul-06 23:48 
AnswerRe: does not compile in vs.net 2005 Pin
marcob6919-Nov-06 4:52
marcob6919-Nov-06 4:52 
GeneralRe: does not compile in vs.net 2005 Pin
bertszoghy30-Jan-07 14:12
bertszoghy30-Jan-07 14:12 
AnswerRe: does not compile in vs.net 2005 Pin
Stuart russell8-Apr-07 13:32
Stuart russell8-Apr-07 13:32 
GeneralRe: does not compile in vs.net 2005 Pin
dfss19-Jul-07 4:27
dfss19-Jul-07 4:27 
GeneralUnable to connect to the port Pin
fluminis3-Feb-05 10:47
fluminis3-Feb-05 10:47 
GeneralYou're very helpfull :) Pin
SimCom18-Jan-05 9:16
SimCom18-Jan-05 9:16 
Generalnot a new thought Pin
yizumi5-Dec-04 13:16
yizumi5-Dec-04 13:16 
QuestionMemLeak ? Pin
Zalosny5-Dec-04 9:12
Zalosny5-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.