Click here to Skip to main content
14,328,801 members
Rate this:
Please Sign up or sign in to vote.
See more:
Can anyone please post an example of MFC VC++ TCP server Programming to send data data packets.

What I have tried:

I tried many examples and could not find the solution.

The example i tried is with two objetcs from the CASyncScoket class
One for listen and one for connection

But the TCP connection is not transmitting

//Object Creation
TCPSOCKET m_Listener
TCPSOCKET m_Connected


//Listen Function
if(m_sListener.Listen()==FALSE)
{
AfxMessageBox("Unable to Listen on that port,pleasetry another port");
m_sListener.Close();
return;
}

//Bind Function
m_Listener.Bind( 172.xx.xx.xx, m_port);

//Accept function
m_Listener.Accept(m_Connected);

//Send function
m_Connected.Send (buf, sizeof(buf));

if there are changes to made , kindly please suggest an appropirate answer

Thanks in advance!!
cheers
Posted
Updated 16-Jul-19 3:30am
Comments
CPallini 16-Jul-19 13:44pm
   
where is TCPSOCKET declaration (i.e. what is TCPSOCKET?) ?
Member 14499788 17-Jul-19 0:36am
   
TCPSocket is a class I have declared from CAsyncSocket
# pragma once
#include "pch.h"
#include "TcpSendSocket.h"
#include "MESS_TRIALDlg.h"
#include "DataFile.h"



TcpSendSocket::TcpSendSocket()
{
//UpdateData(TRUE);


}


TcpSendSocket::~TcpSendSocket()
{
}

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Like so many people nowadays, you ignore the return values from a number of your method calls, so you have no idea if any of your code is doing what you think. Write proper code that checks all the results and examine those results to see what is happening. If things are failing then use the debugger to find out why.
   
Comments
Member 14499788 16-Jul-19 9:44am
   
I already did the way you mentioned and thats why i posted here!
Also I am new in to programming.
Richard MacCutchan 16-Jul-19 10:01am
   
So what results or errors do you see?
Member 14499788 16-Jul-19 10:02am
   
the program is functioning I am able to receive data but not able to send data, Cannot find any errors!
Thats why I a m confused
Richard MacCutchan 16-Jul-19 10:03am
   
Also, according to your comment at How to receive continous UDP data packet from the server?[^] you program works perfectly. So what have you changed since then?
Member 14499788 16-Jul-19 10:08am
   
But i need to sendthe data through tCP.
In UDP i used just rec() and create () .
In TCP listen(), bind(), accept(), Send().
But i think till listen() its working .
have some problems with accept(), I think, but I amnot sure.
Richard MacCutchan 16-Jul-19 11:16am
   
"but I amnot sure."
That is why I made my earlier suggestions. We cannot begin to guess what may be wrong on your system without more information. So make sure you are checking the results of every system call. Use the debugger to collect more detailed information.
Member 14499788 17-Jul-19 1:17am
   
while debugging I found that i have a problem with Accept function of the Socket.
I have attached the code below:

//Bind
m_sListener.Bind(6000);

//Listen
if (m_sListener.Listen() == FALSE)
{
AfxMessageBox(L"Unable to Listen on that port,please try another port");
m_sListener.Close();
return;
}
else
m_sListener.Listen(1);

//Accept
if (m_sListener.Accept(m_sConnected))
{
AfxMessageBox(L"TCP Connection accepted");

}
else
{
AfxMessageBox(L"TCP Connection not accepted");
}


Also in the OnInitialDialog()

void MESSDlg:: OnInitialDialog()
{


//Create
m_sListener.Create(6000);
}
while Debugging I have a ASSERT over the Accept function
and I am not able to figure out what is wrong in it.
Richard MacCutchan 17-Jul-19 3:24am
   
We cannot guess what the ASSERT is complaining about.

However, I suggest you read Socket Programming in C/C++ - GeeksforGeeks[^] which expalins and shows how to do it.
Member 14499788 17-Jul-19 6:33am
   
hi, ASSERT is due to the WSA ERROR 10035,
Operation is blocked for non blocking operations, But how can I eleiminate this error?
Richard MacCutchan 17-Jul-19 8:42am
   
See Windows Sockets: Blocking | Microsoft Docs[^]. You really need to spend more time with the documentation so you understand at least the basic operation of the features you are trying to use.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100