15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


I needs some CSocket help please.

Scott Andrew -- sandrew@pacbell.net
Thursday, January 16, 1997

Environment: VC++ 4.2-flat, Win 95

I have a few questions about using CSocket for syncronous usage.. 

Question 1
--------------
I am creating an application that needs to FTP but not supply UI when
connecting. I have overridden CSocket to give me to wrap fucntionality.

I have a SendCommand call that sends a command and reads from the buffer
until it's done. Then return's TRUE or FALSE depending on the result. What
is the best way to tell when there is no more to read?? I was using
CAsyncSocket::Ioctl(FIONREAD, &dwBytes) to see. But it would return 0
occasionally, but not all the data would come through... The second thing I
have used, that seems to work, is to go through my ProcessRelyMessage that
looks at the reply string and searches for a valid return code (ie. 220 is
valid but 220- is not.) and returns -1 if there is not enough, or a real
result (TRUE or FALSE).

Becuase of the nature I really want this to be fully syncronous.. The other
idea is to through the socket into a thread and make the main process wait
for an even each time. It sends a command. This would allow OnReceive to be
used.. I can really use some suggestions..

Question 2
--------------
What is the best way to time out on a CSocket??? Should I set up my own
timer and then close the socket if nothing happends?? I have played with
the WINSOCK API a little. I am being asked to do this in MFC..

Scott Andrew 



SCS.007@mch.scn.de
Saturday, January 18, 1997

[Mini-digest: 4 responses]


>> Question 1
>> --------------
>> I am creating an application that needs to FTP but not supply UI when
>> connecting. I have overridden CSocket to give me to wrap fucntionality.

>> I have a SendCommand call that sends a command and reads from the buffer
>> until it's done. Then return's TRUE or FALSE depending on the result. What
>> is the best way to tell when there is no more to read?? I was using
>> CAsyncSocket::Ioctl(FIONREAD, &dwBytes) to see. But it would return 0
>> occasionally, but not all the data would come through... 
	I really don't understand. Are you using CSocket or CAsyncSocket 
derived class? Assuming u r using a CSocket derived class, The call to Send 
returns only when all the data has been sent. If you are using a CAsyncSocket 
derived class, you can repeatedly call 'Send()' until you get a error code of 
WSAEWOULDBLOCK. You get a FD_WRITE ( translated by MFC into your OnSend() 
function ), and you can keep sending again.

>> The second thing I
>> have used, that seems to work, is to go through my ProcessRelyMessage that
>> looks at the reply string and searches for a valid return code (ie. 220 is
>> valid but 220- is not.) and returns -1 if there is not enough, or a real
>> result (TRUE or FALSE).
	I don't know what return code you are talking about.

>> Becuase of the nature I really want this to be fully syncronous.. The other
>> idea is to through the socket into a thread and make the main process wait
>> for an even each time. It sends a command. This would allow OnReceive to be
>> used.. I can really use some suggestions..
	I really can't see where your problem is if you are using CSocket. 
Try sending you code.....

>> Question 2
>> --------------
>> What is the best way to time out on a CSocket??? Should I set up my own
>> timer and then close the socket if nothing happends?? I have played with
>> the WINSOCK API a little. I am being asked to do this in MFC..
	Timeout when?? During Connect(), Send() or what... ?? If you call 
Connect(...), you will be automatically timedout if a connnection cannot be 
setup. But if it is a connection you have left idle, nothing happens. During 
testing of my project, I have noticed that during file transfer, I pull out 
the cable, the sending socket gets a FD_CLOSE message - that is the OnClose 
func is called. I can't assure you that this behaviour is consistent. 
	
Be careful working with MFC. It creates a hidden window for every call to 
AfxSocketInit(). Consider the implications before jumping into it.

Chandru.
-----From: "Siemens, TA" 

Hi Scott!

As an answer to your 1st question I can only say that I myself has threaded
the Listen socket operation along with the serving of client sockets. This
way I get to do other work in different threads. As for the question of
whether the buffer is empty or not, you do not state if you use CArchive
for your socket read operations. If you do, you can use one it's member
functions to detect this. But! Before you do this, look up article Q138694
in the knowledge base, which refers to a bug on CArchive operations with
CSocket. The solution offered by MS includes a way to determine if there's
any data waiting.

Your 2nd question is answered by the article Q138692 in the knowledge base.
It's really very simple.

Hope this helps you on the way.

Best regards,

Mike Thomas Jakobsen
Siemens@inet.uni-c.dk
Siemens A/S
Borupvang 3
DK-2750 Ballerup
+45 4477 4477

----------
> From: Scott Andrew 
> To: mfc-l@netcom.com
> Subject: I needs some CSocket help please.
> Date: 17. januar 1997 05:08
> 
> Environment: VC++ 4.2-flat, Win 95
> 
> I have a few questions about using CSocket for syncronous usage.. 
> 
> Question 1
> --------------
> I am creating an application that needs to FTP but not supply UI when
> connecting. I have overridden CSocket to give me to wrap fucntionality.
> 
> I have a SendCommand call that sends a command and reads from the buffer
> until it's done. Then return's TRUE or FALSE depending on the result.
What
> is the best way to tell when there is no more to read?? I was using
> CAsyncSocket::Ioctl(FIONREAD, &dwBytes) to see. But it would return 0
> occasionally, but not all the data would come through... The second thing
I
> have used, that seems to work, is to go through my ProcessRelyMessage
that
> looks at the reply string and searches for a valid return code (ie. 220
is
> valid but 220- is not.) and returns -1 if there is not enough, or a real
> result (TRUE or FALSE).
> 
> Becuase of the nature I really want this to be fully syncronous.. The
other
> idea is to through the socket into a thread and make the main process
wait
> for an even each time. It sends a command. This would allow OnReceive to
be
> used.. I can really use some suggestions..
> 
> Question 2
> --------------
> What is the best way to time out on a CSocket??? Should I set up my own
> timer and then close the socket if nothing happends?? I have played with
> the WINSOCK API a little. I am being asked to do this in MFC..
> 
> Scott Andrew 
-----From: pmoss@bbn.com (Peter Moss)

Scott,

Re your Q1, are you using CArchive and CSocketFile to handle your sending and 
receiving of msgs? This is a great way to make this process painless. You 
simply serialize your msg on both ends.  You can use the 
CArchive::IsBufferEmpty() method to see if there is no more data in the msg.  
Also, if you have the luxury of designing your msg structure so that it 
includes the msg length, that makes life easier.  If you haven't already 
checked out the MSDN CHATSRVR and CHATTER example, you should.  It illustrates 
all these principles.

Re Q2, there is something on the MSDN that tells you how to do this.  Look for 
the article "How to Configure a Time-Out on a CSocket Operation."  PSS ID 
Q138692.

BTW, there are 2 problems in VC 4.2-flat with CSocket that are  that have been 
fixed in 4.2b.  As MikeB often points out, you SHOULD do the patch.






mfc-l @ netcom.com 
01/18/97 03:02 AM

To: mfc-l  @ Internet
cc:  
Subject: I needs some CSocket help please.


Environment: VC++ 4.2-flat, Win 95

I have a few questions about using CSocket for syncronous usage.. 

Question 1
--------------
I am creating an application that needs to FTP but not supply UI when
connecting. I have overridden CSocket to give me to wrap fucntionality.

I have a SendCommand call that sends a command and reads from the buffer
until it's done. Then return's TRUE or FALSE depending on the result. What
is the best way to tell when there is no more to read?? I was using
CAsyncSocket::Ioctl(FIONREAD, &dwBytes) to see. But it would return 0
occasionally, but not all the data would come through... The second thing I
have used, that seems to work, is to go through my ProcessRelyMessage that
looks at the reply string and searches for a valid return code (ie. 220 is
valid but 220- is not.) and returns -1 if there is not enough, or a real
result (TRUE or FALSE).

Becuase of the nature I really want this to be fully syncronous.. The other
idea is to through the socket into a thread and make the main process wait
for an even each time. It sends a command. This would allow OnReceive to be
used.. I can really use some suggestions..

Question 2
--------------
What is the best way to time out on a CSocket??? Should I set up my own
timer and then close the socket if nothing happends?? I have played with
the WINSOCK API a little. I am being asked to do this in MFC..

Scott Andrew 



-----From: Dean Grimm 


Did you know that 4.2 has a CFtpConnection class?

----------
From: 	Scott Andrew
Sent: 	Thursday, January 16, 1997 8:08 PM
To: 	mfc-l@netcom.com
Subject: 	I needs some CSocket help please.

Environment: VC++ 4.2-flat, Win 95

I have a few questions about using CSocket for syncronous usage.. 

Question 1
--------------
I am creating an application that needs to FTP but not supply UI when
connecting. I have overridden CSocket to give me to wrap fucntionality.

I have a SendCommand call that sends a command and reads from the buffer
until it's done. Then return's TRUE or FALSE depending on the result. What
is the best way to tell when there is no more to read?? I was using
CAsyncSocket::Ioctl(FIONREAD, &dwBytes) to see. But it would return 0
occasionally, but not all the data would come through... The second thing I
have used, that seems to work, is to go through my ProcessRelyMessage that
looks at the reply string and searches for a valid return code (ie. 220 is
valid but 220- is not.) and returns -1 if there is not enough, or a real
result (TRUE or FALSE).

Becuase of the nature I really want this to be fully syncronous.. The other
idea is to through the socket into a thread and make the main process wait
for an even each time. It sends a command. This would allow OnReceive to be
used.. I can really use some suggestions..

Question 2
--------------
What is the best way to time out on a CSocket??? Should I set up my own
timer and then close the socket if nothing happends?? I have played with
the WINSOCK API a little. I am being asked to do this in MFC..

Scott Andrew 





| Вернуться в корень Архива |