From: Hector Santos on
Hector Santos wrote:

>> Nothing really seems to jump out at me, but maybe someone else can see
>> something?
>
> Well, you are writing then closing immediately, thus killing the
> transmission before it is even finished.
>
> Try this:
>
> Console::WriteLine(L"- Connecting");
>
> TcpClient^ clnt = gcnew TcpClient( YOUR_IP, YOUR_PORT);
>
> Console::WriteLine(L"- Connected. PAK to close");
> Console::ReadKey();
>
> clnt->Close();
>
> Console::WriteLine(L"- Closed. PAK to exit");
> Console::ReadKey();
>
> Throw in some PAKs (Press Any Key) to give time to work. :)

I'm sorry, I didn't include the example write, after the connect and
before the close.

TcpClient^ clnt = gcnew TcpClient( YOUR_IP, YOUR_PORT);

String^ msg = "hello world\r\n";
array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg );
clnt->Client->Send(data);

Console::ReadKey(); // wait before closing!

clnt->Close();

--
HLS
From: stephen park on
Wow, I really hate turning this into a .net discussion in an mfc
group, but I built a CLR console app and this just doesn't send
anything to the Newcomer server: (code in its entirety)

#include "stdafx.h"

using namespace System;
using namespace System::Net::Sockets;


int main(array<System::String ^> ^args)
{
TcpClient^ c = gcnew TcpClient( server,port );
String^ msg = "hello, world!";
array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg );
c->Client->Send( data );

Console::ReadKey();

c->Close();

return 0;
}

The server says its connected, but when I hit a key the server says
?.?.?.? [?] Closed

with the "Last Received Message" edit box empty.

Doing the same thing with csocket and an mfc console app does the same
thing. Just what the heck am I missing in both?
From: Hector Santos on
stephen park wrote:

> Wow, I really hate turning this into a .net discussion in an mfc
> group, but I built a CLR console app and this just doesn't send
> anything to the Newcomer server: (code in its entirety)
>
> #include "stdafx.h"
>
> using namespace System;
> using namespace System::Net::Sockets;
>
>
> int main(array<System::String ^> ^args)
> {
> TcpClient^ c = gcnew TcpClient( server,port );
> String^ msg = "hello, world!";
> array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg );
> c->Client->Send( data );
>
> Console::ReadKey();
>
> c->Close();
>
> return 0;
> }
>
> The server says its connected, but when I hit a key the server says
> ?.?.?.? [?] Closed
>
> with the "Last Received Message" edit box empty.
>
> Doing the same thing with csocket and an mfc console app does the same
> thing. Just what the heck am I missing in both?


I don't know what Joe's program is, but the above is correct when you
are talking to a telnet-like like server. I have no reason to
question Joe's CAsyncServer, but I have not try it to see what it
suppose to do for you.

What part are you trying to make work? What are you looking for?

What I would do is begin with the basics of writing a TTY client. If
you are just learning sockets, you need to begin with the
basics, which comes with "mistakes" and trial and error process.

Try this TTY() function:

void tty(String ^hostname, int port)
{

Console::WriteLine("* Connecting");

TcpClient^ c;

try {
c = gcnew TcpClient(hostname,port);
} catch(SocketException ^e) {
Console::WriteLine(L"- Connect Error {0}",e->ErrorCode);
Console::ReadKey();
return;
}

// use 100ms timeout for reader
c->Client->ReceiveTimeout = 100;

array<Byte>^ bytes = gcnew array<Byte>(1024);
NetworkStream ^cio = c->GetStream();

while (1) {
if (Console::KeyAvailable) {
ConsoleKeyInfo^ key = Console::ReadKey(true);
if (key->Key == ConsoleKey::Escape) break;
try {
cio->WriteByte(key->KeyChar);
} catch( Exception ^e) {
break;
}
}
try {
int nBytes = c->Client->Receive( bytes );
for (int i=0; i < nBytes; i++) {
Console::Write("{0}",(wchar_t)bytes[i]);
}
} catch( SocketException ^e) {
if (e->ErrorCode == 10054) {
Console::WriteLine(L"! Disconnect");
break;
}
}
}
c->Close();
Console::WriteLine("* Closed");
Console::ReadKey();
}


You can try this against any TELNET like server, including any TELNET,
SMTP, FTP, NNTP, POP3, IMAP server, etc.

Here are a few telnet servers:

bbs.winserver.com port=23
beta.winserver.com port=23

Even SMTP, FTP or NNTP servers, try these:

msforums.winserver.com port=119
news.microsoft.com port119

You know, you can make the tty() function a SERVER by changing the
connect() which you are implicitly doing with the constructor

gcnew TcpClient(hostname,port)

above and replacing it with SOCKET functional calls equivalent in
C/C++.NET for:

Listen()
Bind()
Accept()

Anyway, if your question is about your code, it is fine. I don't know
Joe's code but I'm sure it works. :)

--
From: Hector Santos on
Hector Santos wrote:

> You know, you can make the tty() function a SERVER by changing the
> connect() which you are implicitly doing with the constructor
>
> gcnew TcpClient(hostname,port)
>
> above and replacing it with SOCKET functional calls equivalent in
> C/C++.NET for:
>
> Listen()
> Bind()
> Accept()




BTW, if you want to create a Socket Server, then check out the
TcpListener .net class and example:

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=VS.100).aspx

It combines the Listen(), Bind(), and Accept() for you.

--
HLS
From: Joseph M. Newcomer on
My Async server is based loosely on the classic examples of TCP servers. If it is not
receiving anything, then nothing is being sent.

Note that blocking the program after the Send does not guarantee that (under Nagle's
Algorithm) that anything will be sent at all; it simply stops the program, and the network
stack is probably under no compulsion to flush the buffers. Then the connection closes
without flushing the buffers, so the pending data is discarded. This is how the network
stack works.

You need to force the network to flush the pending buffers; in normal socket programming
this is the shutdown() method; or it is CAsyncSocket::Shutdown in MFC, and you will have
to find the .NET equivalent of this.
joe

On Wed, 12 May 2010 21:29:35 -0400, Hector Santos <sant9442(a)gmail.com> wrote:

>Hector Santos wrote:
>
>> You know, you can make the tty() function a SERVER by changing the
>> connect() which you are implicitly doing with the constructor
>>
>> gcnew TcpClient(hostname,port)
>>
>> above and replacing it with SOCKET functional calls equivalent in
>> C/C++.NET for:
>>
>> Listen()
>> Bind()
>> Accept()
>
>
>
>
>BTW, if you want to create a Socket Server, then check out the
>TcpListener .net class and example:
>
>http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=VS.100).aspx
>
>It combines the Listen(), Bind(), and Accept() for you.
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm