|
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...
|
|
|
|
|
Hi,
The Object (or object, in C# is the same), is the main type for all reference types (which are classes, like List<t>, Controls, FileInfo and so on). Every class, whether you create it or not, implicitly derives from object even if it derives from another one and I'm going to explain you why. Even if you create a class and make it derive from another, that other class must derive from some other class. If you dig through the hierarchy of a class, you'll find that the very base class is object.
In simple words: any reference type can be converted in object. More exactly, all reference types are objects.
So object is the base class of any other class, even if specified another one to derive from.
Have you understood it more deeply? From the question I can understand you're a beginner. On Codeproject you can find useful tutorials for beginners in C# such as Diving in OOP[^]. Please read them as they are really useful.
To make it even more simple: object can encapsulate every reference type you use. You can say that FileInfo is an object implicitly, but you'll need an explicit conversion to say the contrary: that an object is a FileInfo.
Hope this helps.
LG
Lusvardi Gianmarco
|
|
|
|
|
Not quite: An Object is the base class on which all classes (reference types and value types) are based. Object is not limited to reference types, value types are derived from Object as well.
https://msdn.microsoft.com/en-us/library/9kkx3h3c.aspx[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Yes, you're right. Becuase ValueType, the class all value types derive from, derives from Object. Yes, I forgot to mention this aspect.
Lusvardi Gianmarco
|
|
|
|
|
An Object is the base class on which all classes (reference types and value types) are based - as explained by Lusvardi Gianmarco (Jymmy097) - except that Object is not limited to reference types, value types are derived from Object as well.
But I'm not sure that is what you are trying to find out. Can you explain your question in more detail?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
we are using constant constantAlphaImage we are able to draw but without transparency.
imagingImage allow us to apply transparency to png but dont allow to draw.
We are looking to do this effect on the screenshot [url=http://postimg.org/image/bl2e8iy2v/full/][img]http://s7.postimg.org/hm035lkp7/overlaping.png[/img][/url]
Where 1 lap overlap the other chaning the color.
|
|
|
|
|
OK, so what's the question?
|
|
|
|
|
Given the limits of the Compact FrameWork drawing library which include use of transparency for only one color [^], and the fact that people generally have trouble with using the Alpha channel:
"Unfortunately, the NETCF bitmap loader creates a bitmap with the same pixel format as the display when loading a bitmap file or resource and loses the alpha channel information (if present)." [^].
The MSDN blog linked to above may be your best starting point, and it does have usable code.
Looks like you are in for some hairy use of P/Invoke. Note that I don't have a Win Phone and have never programmed to CF.
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
modified 1-Feb-15 6:38am.
|
|
|
|