Click here to Skip to main content
Click here to Skip to main content

TCP_IP_Send_Receive_Using_VC++.NET

, 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++

Server

Client

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

,

etc) -->

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

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
Follow on   Twitter

Comments and Discussions

 
QuestionVB.net Version? Pinmemberantonio62216-Jan-11 6:17 
Questionworking with sql server 2005 database in asp.net Pinmembernet_deen10-Mar-08 18:11 
GeneralNot really .NET Pinmemberv.szabi17-Apr-07 20:50 
GeneralThe version for VC++ 6.0 Pinmembercristitomi13-Mar-07 4:50 
GeneralRe: The version for VC++ 6.0 Pinmembercristitomi23-Mar-07 2:41 
Questiondoes not compile in vs.net 2005 Pinmembersabbir137522-Jul-06 23:48 
AnswerRe: does not compile in vs.net 2005 Pinmembermarcob6919-Nov-06 4:52 
GeneralRe: does not compile in vs.net 2005 Pinmemberbertszoghy30-Jan-07 14:12 
AnswerRe: does not compile in vs.net 2005 PinmemberStuart russell8-Apr-07 13:32 
GeneralRe: does not compile in vs.net 2005 Pinmemberdfss19-Jul-07 4:27 
GeneralUnable to connect to the port Pinmemberfluminis3-Feb-05 10:47 
GeneralYou're very helpfull :) PinmemberSimCom18-Jan-05 9:16 
Generalnot a new thought Pinmemberyizumi5-Dec-04 13:16 
QuestionMemLeak ? PinsussKrodEx5-Dec-04 9:12 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 5 Dec 2004
Article Copyright 2004 by Kumudu Gunasekara
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid