|
Mc_Topaz wrote: Your replay is just meaningless. As is yours. If you need the client to bind to a specific address then you should explain what you are trying to achieve in proper detail, rather than making stupid comments.
|
|
|
|
|
It was not meant to be a stupid comment when I replied to you. I tried to be polite and make sure you had understood my initial purpose correctly. However I thought your comment was rude since you didn't reflect any further on my question.
But anyway, now I have explained why I need this.
|
|
|
|
|
Mc_Topaz wrote: I tried to be polite
Then I suggest that in future you try a little harder?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Down vote countered.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
Ok!
I see to be needed to explain why I need this.
On the computer running the TCP-client there are several network cards. One specific network card is connected to a LAN. On that LAN there are a bunch of M-Bus gateways (IP-devices). All M-Bus gateway have a bunch of M-Bus meters connected to itself using a kind of serial interface.
The programming running the TCP-client must request all M-Bus meters separately.
To request a specific M-Bus meter is done with providing the meter's unique address in the application layer.
So the TCP-client sends the request to 255.255.255.255:10001. The gateways are configured to listen on port 10001 and send the request further to the M-Bus meters.
That M-Bus meter that correspond to the actual unique address will replay back to it's master.
The M-Bus gateway now only need to send back the replay to the TCP client. In the gateway's configuration there is possible to specify what port the client will receive data on.
So I thought by specifying port 20001 in the gateways and binding to 20001 on the client this should work.
I didn't use the broadcast IP-address in my example because I wanted to keep the example simple.
modified 1-Feb-15 12:55pm.
|
|
|
|
|
Have you tried SocketOptionName.DontLinger ? Basically, when a socket closes, it enters a TIME_WAIT state and lingers for about 4 minutes. DontLinger tells it to miss this stage out and close immediately.
|
|
|
|
|
Thanks Pete but that didn't work.
I tried:
tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
I still gets the same error
I assume this can also be handled with the TcpClient.LingerState property?
So tcp.LingerState = new LingerOption(true, 1); should just keep the socket open 1ms after I call the Close method?
This didn't work either. Any other suggestions?
|
|
|
|
|
Hmmm, this is bizarre. This code has worked fine for me in the past. Ok, have you typed in netstat -n -a when the connection closes? Use that to see what state that port is at.
|
|
|
|
|
Nice feature. I didn't know that was possible.
I saw that the Local1's IP-address and Port was in state "TimeWait" before and after the call to close.
If it's not is to much to ask: Can you modify my example code as you should have wrote it. Including any SetSocketOptions you feel are necessary?
Thanks for the support Pete!
|
|
|
|
|
Wrap up both your NetworkStream and TcpClient code within a using block[^]. I've encountered problems like this myself when working with sockets or ports and fixed my issues by simply using "using" statements. This has always fixed my open port problem by properly closing the port when I'm finished with it.
Otherwise you have to do a bunch of garbage collection calls and the like to force .NET to do what it will do for you if you simply let it. Let the objects manage themselves by wrapping them up within the simple "using" statement construct. Right now you are fighting .NET by trying to over manage what .NET wishes to manage for you.
Don't fight the using statement, embrace it.
Later....
|
|
|
|
|
Thanks for the suggestions but it failed as well
With netstat -b I still see that 172.168.0.10:20001 172.168.0.18:10002 is still in TIME_WAIT state after I called the Shutdown() method for the TCP-client.
I used this code to test:
static void Test(IPEndPoint local, IPEndPoint remote, byte[] request)
{
byte[] response = new byte[0];
using (TcpClient tcp = new TcpClient())
{
try
{
tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
tcp.LingerState = new LingerOption(false, 0);
tcp.Client.Bind(local);
tcp.Connect(remote);
using (NetworkStream stream = tcp.GetStream())
{
try
{
stream.Write(request, 0, request.Length);
Thread.Sleep(500);
response = new byte[tcp.Available];
stream.Read(response, 0, response.Length);
}
catch (SocketException soe)
{
Console.WriteLine(soe.ErrorCode);
Console.WriteLine(soe.SocketErrorCode);
Console.WriteLine(soe.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
catch (SocketException soe)
{
Console.WriteLine(soe.ErrorCode);
Console.WriteLine(soe.SocketErrorCode);
Console.WriteLine(soe.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (tcp.Connected)
{
tcp.Client.Shutdown(SocketShutdown.Both);
}
}
}
Console.WriteLine("Received: {0} byte(s)", response.Length);
}
Any further suggestions?
/Steffe
|
|
|
|
|
I always start with the socket, and then work into the client. Also because all of this is Asynchronous, I use onCompleted events. I use the socket’s ConnectAsync() method after first setting up an instance of SocketAsyncEventArgs to pass to the method.
I have noticed a few things though specific to your code, you call tcp.GetStream() without even checking if the client is connected first. I would do something like this:
await tcp.ConnectAsync("host", 12345);
if (tcp.Connected)
{
using (var stream = tcp.GetStream())
{
await stream.ReadAsync(buffer, 0, buffer.Length);
}
}
I would try giving my buffer a larger starting size than zero, perhaps 10,000. And when reading and writing to the same memory stream, I always reset the stream position back to zero. Otherwise you may find your self writing off the end of the stream or reading behind it.
|
|
|
|
|
I am writing in Text Box using txtName.Attributes.Add('style','font-family:Chandini'); in Hindi like 'मजहर' but i am unable to save as it is in database. It is begin save as etgj beke in database. How can I save it in database in hindi.
|
|
|
|
|
Adding a font-family to the control doesn't make sure that what so ever language you're going to use, is being stored. What you need to make sure is that the data source you're going to store works with Unicode too. Either this, or the methodology for displaying the text in the database doesn't work with Unicode.
Here is another question thread[^] that you might be interested in. This problem is just with the Unicode formatting of the characters, to display international-languages and their scripts. So basically, just make sure that the database and the method for displaying it, supports the Unicode structures.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
hi
this is my datatable method
public DataTable Data(string colName)
{ OpenDB();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string strCmd = "Data";
SqlCommand cmd = new SqlCommand(strCmd, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@col", SqlDbType.VarChar, 50));
cmd.Parameters[0].Value = colName;
adapter.SelectCommand = cmd;
adapter.Fill(ds);
conn.Close();
return (ds.Tables[0]);
}
i want tis method not to return anything
how to modify???
DataSet ds = new DataSet();
adapter.Fill(ds);
return (ds.Tables[0]);
doesnt help if i remove the above 3 lines of code
thanks in advance
modified 2-Aug-18 21:02pm.
|
|
|
|
|
Change the signature to public void Data(string colName) ?
|
|
|
|
|
thank you
but now i am unable to execute my stored prodedure
i need to execute my procedure but expect no return from sql.
modified 2-Aug-18 21:02pm.
|
|
|
|
|
You are looking for ExecuteNonQuery, probably your command type or the adapter.???
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
public void Data(string colName)
{
OpenDB();
string strCmd = "Data";
SqlCommand cmd = new SqlCommand(strCmd, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@col", SqlDbType.VarChar, 50));
cmd.Parameters[0].Value = colName;
cmd.ExecuteNonQuery();
conn.Close();
}
|
|
|
|
|
Hello there!
I am making a program that lets the user type certain lines in a textbox.
Example:
X: 24+/26~/19+
How can I make the program send these lines out without taking the + as addition and ~ as an enter other than forcing the user to add {} around the signs?
Thanks in advance!
Linus.
modified 1-Feb-15 11:59am.
|
|
|
|
|
It is simple. Take the string in the textbox and replace all special characters with {yourch} by using the string.Replace method.
example:
string x = this.textbox1.text;
x=x.Replace("+", "{+}");
Then send the string x by using the SendKeys method.
Bye!
Lusvardi Gianmarco
|
|
|
|
|
Superb!
Works flawlessly.
Thanks a thousand times buddy!
|
|
|
|
|
You're welcome!
Lusvardi Gianmarco
|
|
|
|
|
Hi,
Everyone
I want to ask that what is an Object Type in c#?
Thanks,
In Advance...
|
|
|
|