 |
|
 |
What include files are required?
#include "what_goes_here.h"
Thanks for your time
|
|
|
|
 |
|
 |
I am trying to sent streams out from my server that has more than IP but all the time all streams go from the same IP. Is there a way to switch the stream to another ip?
|
|
|
|
 |
|
|
 |
|
 |
Hi,
I want to pass TcpClient to a function as an argument(i.e. to constructor of a class). But I get this error:
CClient::CClient( TcpClient ^ client)
{
// Do something with "client" (i.e. copy it to a local member)
}
error C2512: 'System::Net::Sockets::Socket::Socket' : no appropriate default constructor available
Can you please help me on this?
Thanks
AlwaysStudent
|
|
|
|
 |
|
 |
Can any one tell me ?
How to know the IP address & port Address in Linux by C programming.
mail me: kumar_raj_datta@yahoo.com
Prince
|
|
|
|
 |
|
 |
what is this article all about? i can get almost any information by TcpClient->Client, which is of type Socket. It has a property RemoteEndpoint providing the needed information. i also wondered how you were able to cast a NetworkStream to MyNetworkStream if it isnt one. my guess is (i dont know much about cli c++) that you just get an unmanaged pointer an do a pointer cast which does always succeede. keep in mind that this unmanaged pointer can be invalidated if a gc occurs. you may think that a gc occuring between 2 instructions (call and mov or like this) ist unlikely. although it may take a million executions to get there it will happen in practise.
|
|
|
|
 |
|
 |
How to get the clients IP address using TcpClient::Client property:
Socket^ s = pTcpClient->Client;
String^ ip = (((IPEndPoint^)(s->RemoteEndPoint))->Address)->ToString();
Console::WriteLine("Client IP Address " + ip);
Though, I've no idea if the 'Client' property was available in 2002, when this article was posted.
Eric
|
|
|
|
 |
 | hi!  |  | scootz3345 | 19:25 12 Jul '05 |
|
 |
is this the full codes??? can i have the full codes please...? really need it urgently! thanx
|
|
|
|
 |
|
 |
All the code is there, if you need anything else let me know.
Al
|
|
|
|
 |
|
 |
hi Albert,
I have 2+ yrs of exp in Networking with CCNA certified. No i am intrested in Socket or Network Programming. I have started learning c,c++ .....
So shall i take this entire code as one of the project in Networking ......
jbbkjb
|
|
|
|
 |
|
 |
I convert the code to C#, the following line will throw exception:
MyNetworkStream myStream = (MyNetworkStream) networkStream;
Conversion is invalid.
So I doubt the C++ code cannot run either.
|
|
|
|
 |
|
 |
hi AL...
I am a new beginner in socket programming...vc++.net
Your article very good n give me some guide how to create the class in .net
I want to implement that in my code but the basic stage still got trouble...
I don't no how to fixed it...
I have 2 file..server n client..
This code no error and can connected.
But the output not display..
Error is << String reference not set to an instance of a String >>
->Byte data[] = Encoding::ASCII->GetBytes(s); what that mean?
can you help me...
see my code..
tq
SERVER file
void main()
{
try
{
Int32 port = 80;
IPAddress* localAddr = IPAddress::Parse(S"127.0.0.1");
TcpListener* server = new TcpListener(localAddr, port);
server->Start();
Byte bytes[] = new Byte[256];
String* data = 0;
while (true)
{
Console::Write(S"Waiting for a connection... ");
TcpClient* client = server->AcceptTcpClient();
Console::WriteLine(S"Connected!");
data = 0;
NetworkStream* stream = client->GetStream();
Int32 i;
while (i = stream->Read(bytes, 0, bytes->Length))
{
data = Encoding::ASCII->GetString(bytes, 0, i);
Console::WriteLine(String::Format(S"Received: {0}", data));
data = data->ToUpper();
Byte msg[] = Text::Encoding::ASCII->GetBytes(data);
stream->Write(msg, 0, msg->Length);
Console::WriteLine(String::Format(S"Sent: {0}", data));
}
client->Close();
}
}
catch (SocketException* e)
{
Console::WriteLine(S"SocketException: {0}", e);
}
Console::WriteLine(S"\nHit enter to continue...");
Console::Read();
}
CLIENT file:
String* Connect(String* server, String* message)
{
try {
Int32 port = 80;
TcpClient* client = new TcpClient(server,port);
Byte data[] = Encoding::ASCII->GetBytes(message);
NetworkStream* stream = client->GetStream();
stream->Write(data, 0, data->Length);
Console::WriteLine(S"Sent: {0}", message);
String* responseData = String::Empty;
Int32 bytes = stream->Read(data, 0, data->Length);
responseData =Encoding::ASCII->GetString(data, 0, bytes);
Console::WriteLine(S"Received: {0}", responseData);
client->Close();
} catch (ArgumentNullException* e) {
Console::WriteLine(S"ArgumentNullException: {0}", e);
} catch (SocketException* e) {
Console::WriteLine(S"SocketException: {0}", e);
}
Console::WriteLine(S"\n Press Enter to continue...");
Console::Read();
}
int main()
{
String* message;
String* server;
String* result = Connect(server,message);
Console::WriteLine(result);
}
salwa(beginner)
|
|
|
|
 |
|
 |
All references of
Encoding::ASCII
need to be changed to:
Text::Encoding::ASCII
Then it will work.
|
|
|
|
 |
|
 |
The code is very good but for my own case I can not do this using winsock. Could you help me. Thanks A lot!
|
|
|
|
 |
|
 |
Now I finally know where MS hid it fyi,you can also get at the protected Socket memeber via reflection NetworkStream myStream = myTcpClient.GetStream(); Type streamType = myStream.GetType(); PropertyInfo pi = streamType.GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance); Socket soc = (Socket)pi.GetValue(this.netstream, null); EndPoint endp = soc.RemoteEndPoint; String HostAddress = endp.ToString(); Adam
|
|
|
|
 |
|
 |
I can't use the static cast due to C# because it throws an Exception. Reflection was the solution!!
Thank you
|
|
|
|
 |
|
 |
Thanks Adam,
This compiles:
// get client IP using reflection
Type* pStreamType = aClientNetworkStream->GetType();
System::Reflection::PropertyInfo* pPropertyInfo
= pStreamType->GetProperty( S"Socket"
, static_cast< System::Reflection::BindingFlags >( System::Reflection::BindingFlags::NonPublic | System::Reflection::BindingFlags::Instance ) );
Socket* pClientSocket = static_cast< Socket* >( pPropertyInfo->GetValue( this->aClientNetworkStream, 0 ) );
EndPoint* pClientEndpoint = pClientSocket->RemoteEndPoint;
String* pClientIpAddress = pClientEndpoint->ToString();
|
|
|
|
 |
|
 |
Socket.RemoteEndPoint.ToString() results in something like this...
234.234.65.34:1721
|
|
|
|
 |
|
 |
have no id how to translate this to vb.net
can somebody help?
|
|
|
|
 |
|
 |
Why?
Just compile it and use the class from your VB.NET app.
Too complex? Better start reading then.
Pete
|
|
|
|
 |
|
 |
There isn't even a class to bother with!
There are tops 40 lines of code there.
Heres some rules.
1) :: becomes .
2) -> becomes .
3) #using becomes Import blah1.blah2.blah3 (I think)
4) * becomes space
5) get_ becomes space
6) Put class members inside the class declaration.
7) Delete class name prefix from class member
E.g. System::Net::Sockets::Socket * MyNetworkStream::get_MySocket()
becomes
System.Net.Sockets.Socket MySocket()
There are probably some more, but that'll start to look like a VB.NET class pretty soon. Other than that read all about the Socket class at http://msdn.microsoft.com[^]
Pete
|
|
|
|
 |
|
 |
System::Net::Sockets::Socket * MyNetworkStream::get_MySocket()
{
return(this->get_Socket());
}
how do i convert this to VB.NET codes? I'm new so please help out. Thanks.
So, far, this is what Ive got...
Public Class MyNetworkStream
Private net As NetworkStream
Public Sub New()
Me.net = NetworkStream.Null
End Sub
Public Sub New(ByVal s As Socket)
Me.net = New NetworkStream(s)
End Sub
Public Function getMySocket() As System.Net.Sockets.Socket
getMySocket = getMySocket
End Function
Public Function getIP() As String
Dim soc As Socket = getMySocket()
Dim endp As System.Net.EndPoint = soc.RemoteEndPoint
getIP = endp.ToString()
End Function
End Class
is it even correct? and how to I really use it?
thanks
|
|
|
|
 |
|
 |
i have compiled the class and whant to use it from VB.NET
I add reference to the class
And now i want to use it...
Dim Mystream As NetworkStream (since public NetworkStream in c++)
but this dosnt work
Paul
|
|
|
|
 |
|
 |
here you go, this comes from the reflection idea from one of the guys a little further up on this note. You cant port it directly in VB because of the case, but the reflection idea works great. I have tested this code and it works. Happy Hacking
Public Structure sAddressInfo
Dim m_IP As String
Dim m_Port As String
End Structure
Public Shared Function IPAndPortFromTCPClient(ByVal theClient As TcpClient) As sAddressInfo
Dim pi As PropertyInfo = AESTypes.NetworkStream_t.GetProperty("Socket", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim theSocket As Socket = pi.GetValue(theClient.GetStream, Nothing)
Dim theEnd As EndPoint = theSocket.RemoteEndPoint
Dim theAddress As String = theEnd.ToString
Dim theInfo As New sAddressInfo
theInfo.m_IP = theAddress.Split(":")(0)
theInfo.m_Port = theAddress.Split(":")(1)
Return theInfo
End Function
Pablo
-- modified at 16:33 Thursday 16th March, 2006
Sorry, Public Shared ReadOnly NetworkStream_t As Type = Type.GetType("System.Net.Sockets.NetworkStream")...but the original works better, sometimes this fails for some reason so use this
Dim pi As PropertyInfo = theClient.GetStream.GetType.GetProperty("Socket", BindingFlags.NonPublic Or BindingFlags.Instance)
-- modified at 16:35 Thursday 16th March, 2006
|
|
|
|
 |
|
 |
can any oneget me this c++ code into java ( finding client IP Address)
sxdoss
reliance infocom
india
|
|
|
|
 |