|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | hi! |  | scootz3345 | 19:25 12 Jul '05 |
|
|
is this the full codes??? can i have the full codes please...? really need it urgently! thanx
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | 1.86/5 (3 votes) |
|
|
|
 |
|
|
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)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
The code is very good but for my own case I can not do this using winsock. Could you help me. Thanks A lot!
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
I can't use the static cast due to C# because it throws an Exception. Reflection was the solution!! Thank you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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();
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
sxdoss wrote: can any oneget me this c++ code into java ( finding client IP Address)
Since this is a Managed C++ example, and it uses the FCL and this is a predominately C lang. site, you might not want to hold your breath. Try looking in the JDK Docs., Java has a slew of Networking classes.
good luck.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
As far as I've been able to find out, you can't statically cast in C#, so the CLR will throw an Invalid cast exception or something when you try to dynamically cast something to a derived type. Is there a way to do this in C#??
-Adam
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
As far as I know, works the same way with C#, will only throw an exception when you are trying to cast a null.
This is a very interesting question that you can post in the C# forum. I tried myself to static_cast something in C# and it worked! Send me your code and I'll test it otherwise
Thanks Al
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |