Click here to Skip to main content
15,890,527 members
Home / Discussions / C#
   

C#

 
AnswerRe: UserControl Graphics Question Pin
BoneSoft16-Jun-06 10:12
BoneSoft16-Jun-06 10:12 
GeneralRe: UserControl Graphics Question Pin
melkor1217-Jun-06 9:06
melkor1217-Jun-06 9:06 
Questionhow to get/set ASCII code of a character? Pin
largs16-Jun-06 5:59
largs16-Jun-06 5:59 
AnswerRe: how to get/set ASCII code of a character? Pin
Guffa16-Jun-06 7:32
Guffa16-Jun-06 7:32 
AnswerRe: how to get/set ASCII code of a character? Pin
Le centriste16-Jun-06 7:33
Le centriste16-Jun-06 7:33 
AnswerRe: how to get/set ASCII code of a character? Pin
Rizwan Majeed16-Jun-06 8:37
professionalRizwan Majeed16-Jun-06 8:37 
GeneralRe: how to get/set ASCII code of a character? Pin
largs16-Jun-06 18:54
largs16-Jun-06 18:54 
QuestionHow to tell when socket is disconnected Pin
Glenn E. Lanier II16-Jun-06 5:42
Glenn E. Lanier II16-Jun-06 5:42 
Hi, I'm working on a very simple socket communication program. It needs to connect, send a string, and disconnect. However, I need to know if the connect is terminated by the remote client before I try to disconnect (this is a requirement of the remote client software before my code is "certified").

I have the following code:
<br />
EndPoint ep = new IPEndPoint(IPAddress.Parse(sServerIP), iPort);<br />
Socket sockBatch = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);	<br />
sockBatch.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 30000);<br />
sockBatch.Connect(ep);<br />
if (sockBatch.Connected)<br />
{<br />
   Encoding ASCII = Encoding.ASCII;<br />
   Byte[] byteDataLine = Encoding.ASCII.GetBytes(sSendData.ToCharArray());<br />
   sockBatch.Send( byteDataLine, byteDataLine.Length, 0 );<br />
<br />
   Byte[] RecvBytes = new Byte[256];<br />
   Int32 bytes = sockBatch.Receive(RecvBytes, RecvBytes.Length, 0);<br />
   StringBuilder sbResponse = new StringBuilder();<br />
   sbResponse.Append(ASCII.GetString(RecvBytes, 0, bytes));<br />
   string sResponsePacket = string.Empty;<br />
   while ((sockBatch.Connected) && (bytes > 0) && (-1 == sbResponse.ToString().IndexOf(sEndMarker)))<br />
   {<br />
      bytes = sockBatch.Receive(RecvBytes, RecvBytes.Length, 0);<br />
      sResponsePacket = ASCII.GetString(RecvBytes, 0, bytes);<br />
      sbResponse.Append(sResponsePacket);<br />
   }<br />
}<br />
<br />
loggerBatch.Info("TEST DELAY before disconnect socket");<br />
for (int iWait = 0; iWait < 1 * 60; iWait++)<br />
{<br />
   Thread.Sleep(1000);<br />
}<br />
<br />
bool bPoll = sockBatch.Poll(10000, SelectMode.SelectWrite);<br />
loggerBatch.Info("Batch Socket Write poll: {0}", bPoll);<br />
<br />
bool bPoll1 = sockBatch.Poll(10000, SelectMode.SelectError);<br />
loggerBatch.Info("Batch Socket Error poll: {0}", bPoll1);<br />
<br />
loggerBatch.Info("Batch Socket is connected: {0}", sockBatch.Connected);<br />
if (bPoll && sockBatch.Connected)<br />
{<br />
   loggerBatch.Info("Poll says connected");<br />
   try<br />
   {<br />
	byteDataLine = Encoding.ASCII.GetBytes("AYT".ToCharArray());<br />
	sockBatch.Send( byteDataLine, byteDataLine.Length, 0 );<br />
   }<br />
   catch (Exception ex)<br />
   {<br />
	logger.Info("AYT Error: {0}", ex.ToString());<br />
   }<br />
<br />
   loggerBatch.Info("Disconnect from socket");<br />
   sockBatch.Shutdown(SocketShutdown.Both);<br />
   sockBatch.Close();<br />
}<br />
else<br />
{<br />
   loggerBatch.Info("Socket was disconnected.");<br />
}<br />


Output of above
<br />
2006-06-16 10:19:59.7241|INFO|batch|TEST DELAY before disconnect socket<br />
2006-06-16 10:20:59.7233|INFO|batch|Batch Socket Write poll: True<br />
2006-06-16 10:20:59.7390|INFO|batch|Batch Socket Error poll: False<br />
2006-06-16 10:20:59.7390|INFO|batch|Batch Socket is connected: True<br />
2006-06-16 10:20:59.7390|INFO|batch|Poll says connected<br />
2006-06-16 10:20:59.7546|INFO|main|AYT Error: System.Net.Sockets.SocketException: An established connection was aborted by the softw<br />
are in your host machine<br />
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)<br />
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 size, SocketFlags socketFlags)<br />
[snip]<br />
2006-06-16 10:20:59.7546|INFO|batch|Disconnect from socket<br />


I know that while I am in the delay section the socket connection is terminated (TCP FIN and/or TCP RST). However, the Connected property gives me nothing (I realize it only reports state at last activity), but the Poll method supposedly will let me know the current state. I get an error when I try to send the AYT (for testing), but this is unacceptable for production as I can't send unexpected data down the line.



Any ideas on what to try? Confused | :confused:

TIA
QuestionSetting the default button on a user control Pin
GazzaJ16-Jun-06 5:40
GazzaJ16-Jun-06 5:40 
QuestionMoon phase calculation library? Pin
Radoslav Bielik16-Jun-06 5:38
Radoslav Bielik16-Jun-06 5:38 
AnswerRe: Moon phase calculation library? Pin
Dan Neely16-Jun-06 5:59
Dan Neely16-Jun-06 5:59 
AnswerRe: Moon phase calculation library? Pin
Ravi Bhavnani16-Jun-06 7:08
professionalRavi Bhavnani16-Jun-06 7:08 
GeneralRe: Moon phase calculation library? Pin
Radoslav Bielik16-Jun-06 11:19
Radoslav Bielik16-Jun-06 11:19 
Questioncould i write a program that change keyboard function with C# Pin
largs16-Jun-06 5:35
largs16-Jun-06 5:35 
QuestionBind to a private member variable of my own class. [modified] Pin
User 209307316-Jun-06 5:25
User 209307316-Jun-06 5:25 
QuestionConnectionString Problem of DLL Pin
Chikuu16-Jun-06 5:02
Chikuu16-Jun-06 5:02 
Question******** into the wind? Pin
Malcolm Smart16-Jun-06 4:43
Malcolm Smart16-Jun-06 4:43 
AnswerRe: ******** into the wind? Pin
Josh Smith16-Jun-06 4:48
Josh Smith16-Jun-06 4:48 
AnswerRe: ******** into the wind? Pin
BoneSoft16-Jun-06 10:29
BoneSoft16-Jun-06 10:29 
QuestionPanel_paint method Pin
reshsilk16-Jun-06 4:21
reshsilk16-Jun-06 4:21 
AnswerRe: Panel_paint method Pin
Josh Smith16-Jun-06 4:26
Josh Smith16-Jun-06 4:26 
GeneralRe: Panel_paint method Pin
Guffa16-Jun-06 5:00
Guffa16-Jun-06 5:00 
GeneralRe: Panel_paint method Pin
reshsilk16-Jun-06 5:08
reshsilk16-Jun-06 5:08 
Questiondatatype error datagridview Pin
adamoz16-Jun-06 4:17
adamoz16-Jun-06 4:17 
QuestionScreen Capture Image bit depth question.. [modified] Pin
Greg Ellis16-Jun-06 4:16
Greg Ellis16-Jun-06 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.