Prev: Is this UTF-8 regular expression semantically correct?
Next: Newcomer's CAsyncSocket example: trouble connecting with other clients
From: stephen park on 11 May 2010 20:44 Hiya, I'm using the AsyncServer portion from Dr. Newcomer's code found here: http://www.flounder.com/kb192570.htm I'm writing various clients using mfc and .net to see if I can send messages to it. Since I'm completely new to socket programming, let me start by asking, "Can you only connect to his server app by using CAsyncSocket? My current little bit of learning is to write a .net console app using TcpClient to connect to it and send a message, but the message isn't going through. The server shows a socket has been attached, but no message gets sent, and then the socket disconnects with a message ?.?.?.? [?] Closed OK, yes, I realize that this is an MFC group, but I'm also unsuccessful writing an mfc console app that uses CSocket. Well, anyway, here's the .net version: Console::WriteLine( "Sending message" ); TcpClient^ client = gcnew TcpClient( IP, PORT ); NetworkStream ^stream = client->GetStream(); String^ msg = "hello there"; array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg ); try { stream->Write( data, 0, data->Length ); stream->Flush(); } catch( Exception ^e) { Console::WriteLine( e->StackTrace ); } client->Close(); Nothing really seems to jump out at me, but maybe someone else can see something?
From: David Ching on 11 May 2010 21:37 "stephen park" <steebu(a)gmail.com> wrote in message news:75b2e779-afa0-4f50-bac6-ec675be9fd11(a)a2g2000prd.googlegroups.com... > Can you only connect to his server app by using CAsyncSocket? > Of course not! It would be a very poor server if the client also had to use CAsyncSocket! :-) > My current little bit of learning is to write a .net console app using > TcpClient to connect to it and send a message, but the message isn't > going through. The server shows a socket has been attached, but no > message gets sent, and then the socket disconnects with a message > > ?.?.?.? [?] Closed > > OK, yes, I realize that this is an MFC group, but I'm also > unsuccessful writing an mfc console app that uses CSocket. Well, > anyway, here's the .net version: > > > Console::WriteLine( "Sending message" ); > > TcpClient^ client = gcnew TcpClient( IP, PORT ); > > NetworkStream ^stream = client->GetStream(); > > String^ msg = "hello there"; > array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg ); > > try { > stream->Write( data, 0, data->Length ); > stream->Flush(); > } > catch( Exception ^e) { > Console::WriteLine( e->StackTrace ); > } > > > client->Close(); > > > Nothing really seems to jump out at me, but maybe someone else can see > something? > Congratulations on discovering C++/CLI to experiment with .NET. Coming from native C++, it's much easier to grasp than C#. I used it as a stepping stone to C#. Anyway, are you sure the server is getting a connection? I don't see how because you never call TcpClient::Connect(). And you don't specify the IP or port of the server either. (I believe the IP/port you specify in the TcpClient::TcpClient() is the client endpoint (you give the IP address assigned to your network card and the desired port you want to send out of ). -- David
From: Joseph M. Newcomer on 11 May 2010 22:17 See below... On Tue, 11 May 2010 18:37:59 -0700, "David Ching" <dc(a)remove-this.dcsoft.com> wrote: >"stephen park" <steebu(a)gmail.com> wrote in message >news:75b2e779-afa0-4f50-bac6-ec675be9fd11(a)a2g2000prd.googlegroups.com... >> Can you only connect to his server app by using CAsyncSocket? >> > >Of course not! It would be a very poor server if the client also had to use >CAsyncSocket! :-) **** The client does not know or care what means might have been used to implement the server. The server does not know or care what means might have been used to implement the client. **** > > >> My current little bit of learning is to write a .net console app using >> TcpClient to connect to it and send a message, but the message isn't >> going through. The server shows a socket has been attached, but no >> message gets sent, and then the socket disconnects with a message >> >> ?.?.?.? [?] Closed >> >> OK, yes, I realize that this is an MFC group, but I'm also >> unsuccessful writing an mfc console app that uses CSocket. Well, >> anyway, here's the .net version: >> >> >> Console::WriteLine( "Sending message" ); >> >> TcpClient^ client = gcnew TcpClient( IP, PORT ); >> >> NetworkStream ^stream = client->GetStream(); >> >> String^ msg = "hello there"; >> array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg ); >> >> try { >> stream->Write( data, 0, data->Length ); >> stream->Flush(); >> } >> catch( Exception ^e) { >> Console::WriteLine( e->StackTrace ); >> } >> >> >> client->Close(); >> >> >> Nothing really seems to jump out at me, but maybe someone else can see >> something? >> > >Congratulations on discovering C++/CLI to experiment with .NET. Coming from >native C++, it's much easier to grasp than C#. I used it as a stepping >stone to C#. > >Anyway, are you sure the server is getting a connection? I don't see how >because you never call TcpClient::Connect(). And you don't specify the IP >or port of the server either. (I believe the IP/port you specify in the >TcpClient::TcpClient() is the client endpoint (you give the IP address >assigned to your network card and the desired port you want to send out >of ). *** If either of these are true, it would explain what is happening. I looked at the TcpClient() class constructor and it appears that it does not take any parameters, at least according to the documentation I found. joe **** > >-- David Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: David Ching on 11 May 2010 23:02 "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message news:uo3ku5d9mbqquue7ft1o2hugnoun0osnb8(a)4ax.com... > I looked at the TcpClient() class constructor and it appears that it does > not take any > parameters, at least according to the documentation I found. It actually has 3 ctors that behave differently. http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx It confused me... the one that takes a string and an int actually does use those for the remote server and tries to connect. That seems to be the one OP is using. I was suggesting the one that takes an IPEndPoint instead and this is interpreted for the local ip/port. I recommend using this ctor, especially in non-trivial network configurations such as having multiple network adapters installed, otherwise it may connect over the wrong network adapter. (I wasted a day using the similar UdpClient wondering why it wasn't sending anything when it turns out it was sending over a VMWARE network interface instead of the physical network card!) This was fixed when I used the ctor with the IPEndPoint parameter. -- David
From: Hector Santos on 12 May 2010 01:17
stephen park wrote: > > Console::WriteLine( "Sending message" ); > > TcpClient^ client = gcnew TcpClient( IP, PORT ); > > NetworkStream ^stream = client->GetStream(); > > String^ msg = "hello there"; > array<Byte>^data = Text::Encoding::ASCII->GetBytes( msg ); > > try { > stream->Write( data, 0, data->Length ); > stream->Flush(); > } > catch( Exception ^e) { > Console::WriteLine( e->StackTrace ); > } > client->Close(); > > 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. :) -- HLS |