|
Hi,
I've implemented a class (see below) to handle communications for multiple objects that use a separate thread to handle UDP communications over the same ports using the UdpClient class (.net 2.0). To be sure only one instance can be made for each port I've implemented this in a static way. As you can see below this looks relatively simple.
However, once I've started listening for data using "listen" and the callback is made, the EndReceive (I marked it below) method gets me an error:
"The I/O operation has been aborted because of either a thread exit or an application request"
I've checked for possible thread abortion, but I'm sure this isn't the case. So my question is: what causes this error?
Also, I don't see why it's necessary to pass a reference to the endpoint in the EndreceiveMethod: Isn't the callback made because data was received from a known endpoint? Does the endreceive method update the endpoint passed to the endpoint the data was actually received from (so that I can delegate this to my evenlisteners)?
I've checked MSDN docs on "EndReceive" but they don't comment on this.
Any comments or suggestions are very welomce, thanks in advance!
<br />
class LineConnection<br />
{<br />
static UdpClient receiver = new UdpClient(5048);<br />
static UdpClient sender = new UdpClient(5049);<br />
public delegate void DataReceivedDelegate(byte[] d, IPEndPoint endpoint);<br />
public static event DataReceivedDelegate DataReceived;<br />
public static bool receiving;<br />
<br />
public static void Send(byte[] b, IPEndPoint ipe)<br />
{<br />
sender.Send(b, b.Length, ipe);<br />
}<br />
<br />
public static void listen()<br />
{<br />
if (!receiving)<br />
{<br />
receiver.BeginReceive(ReceivedData, null);<br />
}<br />
}<br />
<br />
private static void ReceivedData(IAsyncResult result)<br />
{<br />
if (result.IsCompleted)<br />
{<br />
try<br />
{<br />
receiving = false;<br />
IPEndPoint end = new IPEndPoint(IPAddress.Any, 5048);<br />
<br />
byte[] b = receiver.EndReceive(result, ref end); <br />
<br />
if (DataReceived != null && b != null)<br />
DataReceived(b, end);<br />
}<br />
catch (Exception e)<br />
{<br />
Console.WriteLine("Error getting data: "<br />
+ e.ToString());<br />
}<br />
}<br />
}<br />
}<br />
|
|
|
|
|
sjembek wrote: IPEndPoint end = new IPEndPoint(IPAddress.Any, 5048);
Try:
IPEndPoint end = null;
|
|
|
|
|
No luck, unfortunately, I get the same error-message
|
|
|
|
|
Try running the receive syncronous and in another thread.
|
|
|
|
|
Yep, that's what I did, and that works. But I'd still like to know what the problem is, there's no reason this shouldn't work.
|
|
|
|
|
Hello, i am a newbie to Microsoft Communication server, I need help on development of Instant messenger using communication server API's
(i don't know is it possible or not but i heard that its possible)
(i had implemented chat programs using sockets.)
if anybody know things about this, please reply..
|
|
|
|
|
Hi to all,
How to use function "GetData(ref object DataBindings,object type, object maxlen)" of WinsockClass in Visual C#? If possible give me ex. how to call this function or what should be parameter of this function?
Best Regards & Thanks,
Aniket Salunkhe
-- modified at 5:51 Monday 26th June, 2006
|
|
|
|
|
Hi all!!
If I upload files and want to save it to a different file format, how do I do this in .NET 2.0?
What I mean is, if I upload a file called MyFile.gif, how do I specifyy how to resave it to MyFile.jpg? It's easy to go and change the extension, but when I change a .gif file to a .jpg file in my graphics editor, then it is usually redused in size.
Can any one let me know what to go and search on at Google? I tried "image manipulation .net 2.0". Nothing returns, or very little in fact.
Regards
ma se
|
|
|
|
|
May you use GDI+, using Image and save as another filet type.
|
|
|
|
|
I'm not sure what GDI is?? But I would like to keep it .NET as far as possible.
|
|
|
|
|
/*
System.Drawing.Image image = System.Drawing.Image.FromFile();
System.Drawing.Image image = System.Drawing.Image.FromStream();
image.Save("a.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
image.Save("a.gif", System.Drawing.Imaging.ImageFormat.Gif);
*/
|
|
|
|
|
Thanks, but I am a bit confused with the code. Which is the source file, and which is the destination file?
|
|
|
|
|
Two ways
1) you get the stream from Memory, by BinaryRead a little complex
2) you save the file to your hard disk first, then
get local file path and save as aonther file,
simple but unefficient.
|
|
|
|
|
GDI+ is a librabry in .NET which is used to manipulate graphics.
Read GDI+ articles from google(search GDI+ .NET)
here you can get code to generate image from another image, in this way you can craete a jpg from gif or vice versa.
Thanks
Sushant Duggal.
|
|
|
|
|
Hi,
I need to get the IP Adress (and many othe infos) of any user of my ASP.Net (web) application.
I looked arround the 'System.Web.HttpBrowserCapabilities' but there is no way to get the IP Adress.
Thanks a lot
The novice.
|
|
|
|
|
Have a look a Request.ServerVariables Collection - It definately has the user's IP Address in there plus, I suspect, the other information you are after.
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
|
|
|
|
|
Use Request.UserHostAddress to return the client IP.
To get more info use Request.Browser
|
|
|
|
|
Hi there !
I want to store a list of System.Drawing.Rectangle objects in an ArrayList.
On action of user, the position of the Rectanlge objects should be changed.
Anyway, the collection is not updated when I use foreach in the Move function see below).
I guess the content of the arraylist is somehow protected, but what kind of dynamic list should I use instead ?
Thanks
ch
My code looks like this :
public class RectCollection
{
private ArrayList rects;
public RectCollection()
{
this.rects = new ArrayList();
}
public ArrayList Rects
{
get { return rects; }
set { rects = value; }
}
public void Add(Rectangle rect)
{
this.rects.Add(rect);
}
public void Move(int x, int y)
{
foreach (Rectangle rect in this.rects)
{
rect.Offset(x, y);
}
}
|
|
|
|
|
Its nothing to do with the list being "protected" from change - it is due to the nature of Rectangle. It is a value type, which means you effectively operate on a copy of it when calling Offset after it is unboxed from the ArrayList.
try this as your move method:
public void Move(int x, int y)
{
for(int i=0;i<this.rects.Count;i++)
{
Rectangle rect = (Rectangle)this.rects[i];
rect.Offset(x,y);
this.rects[i] = rect;
}
}
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
|
|
|
|
|
Hi to everyone,
I am writing a c# program that uses a native c++ dll. In the c++ dll I have a function which allocates space for a char array and returns it as follows:
char * my_func(size)<br />
{<br />
char * pOut = new char[size];<br />
...<br />
...<br />
return pOut;<br />
}
In C# part I wrapped the function as follows:
<br />
[DllImport("my_dll.dll", EntryPoint="my_func")]<br />
public static extern IntPtr my_func(int size);<br />
I am using the function in my code as follows:
<br />
IntPtr pData = my_func(nSize);<br />
byte[] arrByte = new byte[nSize];<br />
Marshal.Copy(pData, arrByte, 0, nSize);<br />
...<br />
The problem is how can I free the memory allocated in the native c++ dll by the new operator.
Thanks
|
|
|
|
|
i got this exception while calling Timer.Start()
Number must be either non-negative or -1.
Parameter name: dueTime
can any one tell me the reason.
timer interval is set to 180000.
Regards
Shajeel
|
|
|
|
|
Hi
Are the optional parameters are allowed in C#. If yes how they r implemented?
|
|
|
|
|
johnprakasht wrote: Are the optional parameters are allowed in C#.
Yes.
johnprakasht wrote: If yes how they r implemented?
It's a trick of the compiler.
Any other question?
I thought you might ask how to declare them!
with the param keyword.
As in
Console.WriteLine(string format, params object[] arguments);
|
|
|
|
|
If you mean optional parameters like those in VB.Net: No you won't find this in C#. Generally just make overloads of a function with different parameter combinations.
|
|
|
|
|
Optional parameteres not allowed same as VB.net here.
way of implementation is that you must create oveloaded methods as below:
public GetTest(string str1)
{
....
}
public GetTest(string str1,string str2)
{
....
}
|
|
|
|