|
Hi, I am new to C#, I have just switched to C#, because there are more web resources on C# than VB, I am having a problem related to UDP data Receiving. I want to know, what is necessary information to receive UDP Data? Actually, My City Stock Exchange sends the UDP Data to their Clients on Port# 8129, but my code does neither return any data nor any error. I don’t know much about the server, which sends the data. My source code works fine locally.
Patch of this coding is given below:
<code>IPHostEntry localHostEntry;
try
{
Socket soUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
localHostEntry = Dns.GetHostByName("127.0.0.1");
IPEndPoint localIpEndPoint = new IPEndPoint(localHostEntry.AddressList[0], 8129);
soUdp.Bind(localIpEndPoint);
while (true)
{
Byte[] received = new Byte[200];
IPEndPoint tmpIpEndPoint = new IPEndPoint(localHostEntry.AddressList[0], sampleUdpPort);
EndPoint remoteEP = (tmpIpEndPoint);
int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
String dataReceived = System.Text.Encoding.ASCII.GetString(received);
Console.WriteLine(dataReceived);
}
}
catch (SocketException se)
{
Console.WriteLine("Socket Exception has occurred!" + se.ToString());
}</code>
|
|
|
|
|
When you bind the 127.0.0.1 you are creating a looback binding that is available on your machine only. The loopback address is a special IP that bypasses the core logic of TCP/IP driver so that messages can simply be routed intra-machine w/o having to undergo the packet processing required for inter-machine communication. Thus you can start by creating an end point to the actual IP address. Now if your computer uses DHCP to acquire an IP then you will have to deal with passing it as a command line or windows form parameter, or possibly just use a static IP instead. Secondly since UDP is connectionless why are you binding the socket? Finally go with the Managed UDP interface unless you truely have a reason to drop down to the native socket interface.
All you really need for UDP is:
UdpClient client = new UdpClient(8129);<br />
<br />
IPEndPoint remoteEndPoint = new IPEndPoint(0,0);<br />
<br />
byte[] datagram = client.Receive(ref remoteEndPoint);<br />
string dataReceived = System.Text.Encoding.ASCII.GetString(datagram);<br />
<br />
string request = "Thanks";<br />
byte[] packet = System.Text.Encoding.ASCII.GetBytes(request.ToCharArray());<br />
client.Send(packet, packet.Length, remoteEndPoint);<br />
Good luck with C#
Mike Luster
|
|
|
|
|
Im trying to run Thoughtpost.P2P library
but even the examples dont want to run
it throws some exception could it be because of me using .Net Framework 2?
Could i use C# Express 2005 and compile throw .Net framework 1.1 ???
Thanks !
|
|
|
|
|
It probably doesn't work because it's written for .NET 1.1. No, you can't use Visual Studio 2005 with .NET 1.1, not even the Express versions.
You MUST use Visual Studio 2003 with .NET 1.1. Or Notepad... It's just a matter of preference.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Any idea if i cant have both, 2003 and 2005 express editions installed ?
|
|
|
|
|
Installing multiple versions of both the .NET Framework and Visual Studio is not a problem. You just have to make sure they're installed in the correct order.
I don't think there is a 2003 Express edition, just 2005.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I've made two Windows Services that communicate between them and I get a lot of exceptions with the message 'An existing connection was forcibly closed by the remote host'.
I've searched in other forums but without any good answer. Do you have any idea?
Thanks
Ricardo
|
|
|
|
|
|
The problem is that this error is occurring on the server. There are a lot of clients connected to it and the clients stay connected.
|
|
|
|
|
If the error is occuring on the server then one of the client's actively closed their socket. This happens when the client closes the socket or the process crashes (in which case the kernel closes out the socket).
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
Maybe there is another problem. Look what is happening: I have the following methods:
internal void SendData(Channel channel, byte[] buffer, int offset, int count)
{
if(channel != null && channel.Socket != null && channel.Socket.Connected)
channel.Socket.BeginSend(buffer, offset, count, SocketFlags.None, new AsyncCallback(OnDataSent), channel);
}
private void OnDataSent(IAsyncResult asyncResult)
{
if(asyncResult != null && asyncResult.AsyncState != null)
{
Channel channel = (Channel)asyncResult.AsyncState;
ushort bytesSent = (ushort)channel.Socket.EndSend(asyncResult);
...
}
}
The Channel class is a custom class that holds a reference to a Socket object. First I call the SendData method, that calls the BeginSend method on the Socket object. When the socket ends sending the data, it calls the OnDataSent method. The problem is that when I try to call the EndSend method, the Socket object is null. I don't have any control over it once is the framework that calls the method. When I don't test if the Socket object is a null reference, I get the NullReferenceException. When I test, I get the other exception (the SocketException).
In other words, all the problems reside in the fact that the Socket object is null. Do you have any idea about what could be causing this behaviour? Could the network topology have any influence?
Thanks.
|
|
|
|
|
This looks like it may be a race condition. OnDataSend is called on a different thread than BeginSend. Is your Channel class thread safe?
When you say test and not testing your app are referring to attaching a debugger? If so then it's almost certainly a race condition.
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
I have been reading from various sources about delegates. The more I read the more I get confused.
My question is - What can you do with delegates that you cannot do with just calling the method and not using delegate(s) howsoever?
I mean assume that when an event occurs then instead of having a delegate call the method just call the method directly from the event handler.
Also if you can send any good link that explains the concept it will be appreciated.
Thanks
----------
Venus Patel
http://patelsinc.blogspot.com/
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
I am sure there are other advantages, but the one that comes to mind is this:
Say you create a control dynamically. Since it is not created at design time, you have no other way to associate your sub with your control. What you need to do is create your delegate, and when you create your control at runtime, associate the delegate with the controls method call.
Hope this helps.
Roy.
|
|
|
|
|
Why are delegates used instead of just calling the function in the event handler. There are plenty of reasons. Note that delegates are used to bind functions to events, or to call other objects and functions async.
Lets first explain the first, because of delegates you can add more then one listener to the event. This is usefull in multiview application.
The second one is, at least for me, the most important reason. Thanks to the delegate you will be ably to dynamicly invoke a method. This means that a call to the function will be made, but not executed straight away. So the function calling the delegate continues executing its code.
|
|
|
|
|
Gerben I think your second reason is wrong while the calling method whether directly or using its delegate run in the same thread as caller function, it doesn't continue to next statement until the called method(s) return.
---
"Art happens when you least expect it."
|
|
|
|
|
If you invoke a methond dynamicly, even if they operate in the same thread, the running function will not get interrupted. This is because using dynamic invoke you post a message to your application with the request to execute a function. You do not call the function directly.
Also see: MSDN Article on dynamic invokes.
PS: I tried it in a single thread application just to be sure.
|
|
|
|
|
Yes, I see. So it is one other usage of delegates.
Thank you
---
"Art happens when you least expect it."
|
|
|
|
|
Let me add some notes:
1. A Delegate is a type that is used to construct delegate objects that refrence functions with the same signature as delegate.
Just like classes that construct objects.
2. When a delegate object is created with some function name in its constructor, that object will be indirect agent of that function, and this happens RUNTIME. It is important because when you create a compiled class for your users you don't know what functions they will create to bind as their callbacks or event handlers so you call them directly, so there should be some indirect way for them to introduce their functions and that is Delegate!
I hope that is useful
- Mohammad
---
"Art happens when you least expect it."
-- modified at 1:06 Tuesday 13th December, 2005
|
|
|
|
|
Hi buddies,
I have created a remote object in another AppDomain, after a short time of inactivity it seems to be disconnected and removed because next time I call some method on it, an exception says that the object doesn't exists or it is disconnected.
How can I set lifetime of remote objects? May I set it to live till I explicitly remove it ?
Thank you so much,
- Mohammad
---
"Art happens when you least expect it."
|
|
|
|
|
|
Thank you so much leppie, you have helped me lots of times, I hope I can help u back someday buddy
---
"Art happens when you least expect it."
|
|
|
|
|
|
Hi,
I m new to socket programming and c#.I have been trying to write a simple telnet client which will connect to the machine and execute commands and anle to get back the results.I have been so far able to coonect using TcpClient , but I m getting garbage value in return like %??% etc.
Is there any sample code I can follow or please advise me which class I should follow to process the data obtained from the server ? Currently I m using netStream and read and write methods.
Thanks
- P
|
|
|
|
|
The reason you are getting those garbage characters back is that they are formatting codes for telnet. They are attempting to talk to your program and negotiate a common set of features or telling you how to display a certain character.
There's no easy answer here. You'll have to read the Telnet RFC (Request for comments). It details what these codes are and how you should handle them (and also which ones you can ignore).
http://www.faqs.org/rfcs/rfc854.html[^]
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|