|
It worked , i had to tweak it a bit but it worked. Thanks again
|
|
|
|
|
Ah but tweaking is where the fun is, that is where you learn stuff. Give the answer an upvote if you want to show appreciation.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hi,
I writing code for send and received data by use UDP client the send data is ok but problem in received data
1- I send this data "S11E"to program and in program display received data in label i see only "S" display in label why not display "S11E"
2- I put received code inside code button this mean cannot received any data until press the button .
can be Mark program that reads data automatically
used this code for recived data
byte[] reciveddata= udpClient.Receive(ref ipend);
string returnData = Encoding.ASCII.GetString(reciveddata);
label3.Text = ""+returnData;
in reciveddata only one byte but I send string data .
where is problem?
my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace etherent_send_and_recive_data
{
public partial class Form1 : Form
{
UdpClient udpClient;
IPEndPoint ipend;
public Form1()
{
InitializeComponent();
udpClient = new UdpClient();
}
private void send_data_through_etherent( string send_data_ETH)
{
byte[] senddataETH = ASCIIEncoding.ASCII.GetBytes(send_data_ETH);
udpClient.Send(senddataETH,4);
}
private void button1_Click(object sender, EventArgs e)
{
int port;
button1.Text = "disconnected";
port = int.Parse(textBox2.Text);
ipend = new IPEndPoint(IPAddress.Parse(textBox1.Text), port);
try
{
udpClient.Connect(ipend);
button1.Text = "Connected";
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
}
private void switch1_StateChanged(object sender, NationalInstruments.UI.ActionEventArgs e)
{
if (switch1.Value == true) send_data_through_etherent("S11E");
else send_data_through_etherent("S10E");
try
{
byte[] reciveddata= udpClient.Receive(ref ipend);
string returnData = Encoding.ASCII.GetString(reciveddata);
label3.Text = ""+returnData;
}
catch (Exception e1)
{
MessageBox.Show("ERROR");
}
}
}
}
modified 8-May-15 3:51am.
|
|
|
|
|
Your code relies on things we don't have access to, but this worked for me, if it works for you too then maybe analyse the differences (button1 connects and button2 sends)
public partial class Form1 : Form
{
Thread listenThread;
UdpClient udpClient;
IPEndPoint ipend;
bool listening;
public Form1()
{
InitializeComponent();
udpClient = new UdpClient();
}
private void send_data_through_etherent(string send_data_ETH)
{
byte[] senddataETH = ASCIIEncoding.ASCII.GetBytes(send_data_ETH);
udpClient.Send(senddataETH, 4);
}
private void button1_Click(object sender, EventArgs e)
{
int port;
button1.Text = "disconnected";
port = int.Parse(textBox2.Text);
ipend = new IPEndPoint(IPAddress.Parse(textBox1.Text), port);
try
{
udpClient.Connect(ipend);
button1.Text = "Connected";
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
listenThread = new System.Threading.Thread(StartListening);
listenThread.Start();
}
public void StartListening()
{
UdpClient c = new UdpClient(ipend);
string data = string.Empty;
do
{
byte[] reciveddata = c.Receive(ref ipend);
data = Encoding.ASCII.GetString(reciveddata);
System.Diagnostics.Debug.WriteLine(data);
if (listening)
{
label3.Invoke((MethodInvoker)delegate { label3.Text += data; });
}
} while (data != "exit");
}
private void button2_Click(object sender, EventArgs e)
{
send_data_through_etherent("S11E");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
listening = false;
send_data_through_etherent("exit");
}
}
|
|
|
|
|
Think you Dear for help me
Sir when use your code display error message
in code
UdpClient c = new UdpClient(ipend);
in
public void StartListening()
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
Additional information: The requested address is not valid in its context
How can resolve this problem
Regards
|
|
|
|
|
Make sure the IP is in textbox 1 (127.0.0.1 - it connects to itself), an unused port is in textbox2, then click button2 to connect and button2 to send.
|
|
|
|
|
thank you for Reply
Yes I Put IP in textbox1 and port in textbox2 and for check write
MessageBox.Show(textBox1.Text + " " + textBox2.Text);
and when press button the message display IP and port is correct after that also the program connected successfully
the problem when the program input to StartListening() and stop direct in code
UdpClient c = new UdpClient(ipend);
also I have anther problem the form cannot exit from button x
where the problem ?
|
|
|
|
|
Not sure what's happening TBH, it works on my machine. The listener needs to work for the form to exit properly, if it's not working then it can't listen for the command to stop.
|
|
|
|
|
Sir Now resolve the problem
I write this code
UdpClient c = new UdpClient(textBox1.Text, port);
but no any data received in Lable3
Regards
|
|
|
|
|
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???
|
|
|
|
|