|
For sending data i use sockets a bit like this for UDP
IPEndPoint IPE = new IPEndPoint(IPAddress.Parse(this.ServerIP), this.ServerPort);
this.Soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.Soc.Connect(IPE);
Soc.Send(this.Request, SocketFlags.None);
buffer = new byte[2048];
int Size = Soc.Receive(buffer);
this.Reply = new byte[Size];
Soc.Close();
The server needs to look a bit like this
IP4receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IP4receiveEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), ClientPort);
IP4receiveSocket.Bind(IP4receiveEndPoint);
IPAddress IPA = IPAddress.Parse("0.0.0.0");
byte []Buf=new byte[1024];
whilst (ServiceIsRunning)
{
EndPoint RemoteEndPoint = new IPEndPoint(IPA, 0);
int RequestSize = IP4receiveSocket.ReceiveFrom(Buf, ref RemoteEndPoint);
IP4receiveSocket.SendTo(SomeByteData, RemoteEndPoint );
}
if you are using threads to send then watchout for requests that just hanging even if you set time-outs.
You could try killing the thread using an .Abort() but this won't kill them all so the best way is to use the killing thread to call code like shown below and passing in the socket.
public static void ShutMeDown(Socket Soc)
{
try{Soc.Shutdown(SocketShutdown.Both);} catch {;}
try { Soc.Close(); }catch { ;}
}
Hope this helps
|
|
|
|
|
1. there's lots of precedents in .NET itself for having classes that are non-generic (can be instantiated), and that have static "utility" methods.
2. when you write a generic class like this (deliberately abbreviated) example:
public class Node<T> : IEquatable<Node<T>> where T : IEquatable<T>
{
public List<Node<T>> Nodes { set; get; }
public int Level { set; get; }
public int ID { set; get; }
public T Value { set; get; }
public static Func<Node<T>, Node<T>, bool> GatherFunc;
public static Dictionary<int, List<int>> Gather(Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
{
}
} 3. The issue is that when you want to invoke those static function in the generic class:
a. you can't use Extension methods with generic classes
b. you have to use a rather elaborate syntax to invoke those methods, like:
var result1 = Node<TestClass>.Gather(XNode, Node<TestClass>.GatherMode.Values);
var result2 = Node<TestClass>.Gather(XNode, Node<TestClass>.GatherMode.Names); c. yes, you can using a 'Using directive statement, for example:
using node = May_1_2015_MultiList.Node<May_1_2015_MultiList.TestClass>; and, as you can see, that requires you fully qualify the references, even though all the classes, including Node, are in the same NameSpace.
Is there any other way to invoke the static methods using a simpler syntax ?
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
modified 8-May-15 9:32am.
|
|
|
|
|
Hi Bill,
not sure if I'm missing your point - but why don't you do something like this?:
public enum GatherMode
{
Values,
Names
}
public class TestClass : IEquatable<TestClass>
{ }
public static class NodeExtensions
{
public static Dictionary<int, List<int>> Gather<T>(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
where T : IEquatable<T>
{
if (func == null)
func = (n1, n2) => n1.GatherFunc<T>(n2);
return null;
}
public static bool GatherFunc<T>(this Node<T> node, Node<T> otherNode)
where T : IEquatable<T>
{
return true;
}
}
public class Node<T> : IEquatable<Node<T> where T : IEquatable<T>
{
private void test()
{
var XNode = new Node<TestClass>();
var result1 = XNode.Gather(GatherMode.Values);
var result2 = XNode.Gather(GatherMode.Names);
}
}
/Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thanks for taking the time to reply, Sascha. What you suggest cannot be done:
1. If you declare the static class that holds the static Extension with a generic constraint <T> that would not compile: "Extension method can only be declared in non-generic, non-nested static class."
2. If you declare the static class that holds the static Extension with no generic constraint, then the compiler will throw an error because it cannot resolve the generic parameter.
I view this as similar to the compiler constraint that when you create a dynamic instance of Func, or Action, you have to give it Types which can be resolved: which is actually a very cool feature
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
|
|
|
|
|
Would you have to make changes to what I posted which won't compile? Because what I posted compiles for me.
edit: after implementing IEquatable of course
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
modified 8-May-15 6:01am.
|
|
|
|
|
Hi Sacha, You are correct ! I am so delighted to find out I'm wrong
But, what's puzzling is that if I take your code example and omit the generic constraint on the 'Gather method I get the error message stating 'T does not implement IEquatable ... an error message I never saw in my experiments.
I assumed that because the 'Node class itself is constrained in the way shown in my code example ... implements IEquatable, and the 'TestClass is also constrained and implements IEquatable, that it was not necessary for me to also constrain the 'Gather method.
I am off to the gym to try and sauna and swim my way out of the 96F, feels like 106F, tropical heat of the day here (GMT+7), but, when I return, I look forward to reviewing my code and figuring out what happened.
Big thank you !
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
|
|
|
|
|
Hi Bill!
BillWoodruff wrote: I assumed that because the 'Node class itself is constrained in the way shown in my code example ... implements IEquatable, and the 'TestClass is also constrained and implements IEquatable, that it was not necessary for me to also constrain the 'Gather method. That's true if the Gather-method is a method of Node<T> but not when trying to implement it as an extension method.
BillWoodruff wrote: I am off to the gym to try and sauna and swim my way out of the 96F Now I know where these visions come from
Just kidding, hope you had a good time!
BillWoodruff wrote: Big thank you ! My pleasure!
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
NB: As per the code Sascha posted, you put the generic parameter and constraints on the extension method itself, not the containing class.
In other words:
public static class NodeExtensions<T>
{
public static Dictionary<int, List<int>> Gather(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
{
...
}
}
public static class NodeExtensions
{
public static Dictionary<int, List<int>> Gather(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
{
...
}
}
public static class NodeExtensions
{
public static Dictionary<int, List<int>> Gather<T>(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null) where T : IEquatable<T>
{
...
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am making a project and the code works as long as I change the drive letter to the usb drive I am using to the drive letter when it plugged into a different machine.
I am trying to get it so no matter what drive it is in or if the file is copied to a internal drive that it detects where the database source file is.
any help would be appreciated.
below is the line of code I am trying to get to detect the drive letter needed(AttachecDBFilename = Drive letter):
SqlConnection con = new SqlConnection@"DataSource=.\SQLEXPRESS;AttachDbFilename=J:\\NewDB\NewDB\WindowsFormsApplication1\NewDB1.mdf;Integrated Security=True;User Instance=True");
|
|
|
|
|
It's best to ask the user where the database file to use is as searching all drives can take quite a long time if the folder structure is large.
|
|
|
|
|
As Dave says, ask the user.
While it is technically possible to do what you want, it isn't advisable.
Firstly, because not all "drives" will be available all the time, although the drive letter is - removable media drives such as CD / DVD / backup devices work like this.
Secondly because for some users there are multiple drive connections onto networked storage - I have 7 disk letters to various folders on my 16TB NAS, and searching each one of them is going to take you a very, very long time!
Ask the user: and remember his answer. Next time, check there first and if the file is fine continue. If not, ask him again.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Assuming the database file is in the same directory as your application, use |DataDirectory| as the path:
SqlConnection con = new SqlConnection@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|NewDB1.mdf;Integrated Security=True;User Instance=True");
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance[^]
This connection string syntax is supported by the SqlClient and OleDb managed providers.
By default, the |DataDirectory| variable will be expanded as follow:
- For applications placed in a directory on the user machine, this will be the app's (.exe) folder;
- For apps running under ClickOnce, this will be a special data folder created by ClickOnce;
- For Web apps, this will be the
App_Data folder;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hai,
I creat a setup project for my project using visual studio 2010.. It will be run in my system,but it will not run a setup file different sysytem... please give me a solution..
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So we need to know how you created the setup program, what it did, any errors it gives, and so forth.
We can't tell from here - we only get what you write to work from!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Countered down vote.
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
Brisingr Aerowing wrote: Countered down vote. Why? He posted that as an answer and it clearly is not an answer. He asked questions and should have marked it as such.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Jayamanickam wrote: but it will not run a setup file different sysytem Why won't it?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hello!
To start off, I'm really new to programming/coding. I know a little but not too much.
So I'm creating a web browser, and I want to add tabs.
What do I have to do to have tabs? What part of the code do I need to post?
Thanks in advance!
|
|
|
|
|
Did you try it with a Tab-Control?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Currently there's no function for tabs. There's simply a button with no code.
I 'simply' need help building this code.
|
|
|
|
|
I assume you currently have a Form and a WebBrowser-Control placed on there, right? To make it multi-tabbed you would have to place a Tab-Control on the Form and WebBrowser-Controls into the Tab-Pages of the Tab-Control. This won't yet automatically allow you to open as many new tabs as you want while running your application but you should do it like this as a first step.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
We are refactoring a component / framework for concurrency testing to fit better in BDD. This is the current project:
http://www.crawler-lib.net/crawler-lib-concurrency-testing
We want to have a more fluent API that fits better in BDD scenarios with Given/When/Then.
Do you test concurrency in your unit tests or would you like to? Please give us information how you do this now and how you want it. Thanks
|
|
|
|
|
I usually test the work that the thread has to execute, not the threaded environment. I try to keep the units that are tested as small and simple as possible, otherwise it'd be crawling with (threading) dependencies making testing too complex or even impossible.
Some good tips can be found here;
http://www.alexecollins.com/5-tips-unit-testing-threaded-code/
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Example: If you develop a Reader/Writer Lock component (I know this class exists already, but for the understanding), you can't test the component without threads. The synchronization mechanism is the essential behavior of this component.
The article you mentioned uses a Java framework with essentially the same approach what we have developed now. They name it "Executors" but this is not the point. We are far beyond that. For now we want a framework that can be used for BDD in a more elegant way.
These are fragments from the ConcurrencyTester we are developing now. In fact it is a BDD Test with BDDfy, NUnit and Shoudy testing the .NET ReaderWriterLock (as a sample):
[Test]
public void MultipleReaders()
{
using (concurrencyTester = new ConcurrencyTester())
{
this.Given(c => c.new_reader_writer_lock())
.And(c => c.i_start_read_operation(1))
.And(c => c.i_wait_till_read_operation_is_pending(1))
.And(c => c.i_start_write_operation(1))
.And(c => c.i_start_read_operation(2))
.And(c => c.i_wait_till_read_operation_is_pending(2))
.And(c => c.i_start_write_operation(2))
.When(c => c.release_read_operations(new[] { 1, 2 }))
.And(c => c.release_write_operations(new[] { 1, 2 }))
.And(c => c.wait_for_test_end())
.Then(c => c.write_operations_should_not_intersect_with_read_operations())
.And(c => c.write_operations_should_not_intersect_with_other_write_operations())
.BDDfy();
concurrencyTester.Marks.DumpTimeline();
}
}
In the user story scenario it uses steps like:
void i_start_write_operation(int number)
{
concurrencyTester.Start(
c =>
{
readerWriterLock.EnterWriteLock();
using (concurrencyTester.Marks["Write"].BeginSection())
{
concurrencyTester.Events[WriteOperationPending, number].Wait();
}
readerWriterLock.ExitWriteLock();
}, "Write Operation #{0}", number);
}
To describe things on a different thread.
After the test run has ended (we are multi threaded) we check what should be and what not:
void write_operations_should_not_intersect_with_read_operations()
{
foreach (var intersect in this.concurrencyTester.Marks["Write"].Sections.SelectMany(readSection => readSection.Relations.Intersect))
{
intersect.Mark.Name.ShouldNotBe("Read");
}
}
So it should become very easy to write concurrency tests with this framework. I'm now interested in test cases where concurrency / multi threaded tests are needed.
|
|
|
|
|