Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C# Sockets Problem


  • Please log in to reply

#1
W-Unit

W-Unit

    Member

  • Member
  • PipPipPip
  • 170 posts
Hi all,
So I'm trying to program a pretty basic (console) server in C# as well as an equally simple GUI client program.
Here's the weird thing. When I use telnet to connect to my server for testing, I can freely disconnect or close out of telnet without causing any problems.
However, when I try using the GUI to connect, I get a System.Net.Sockets.SocketException if I close out of the GUI. The exception is as follows:
Unhandled Exception: System.Net.Sockets.SocketException: An existing connection
was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer)
   at F_2_fish_Communicator_Server.Server.listenForInput(Object sender)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart(Object obj)
...So naturally I try to fix this problem using a try/catch statement. After telling it to catch a SocketException didn't work, I changed it to catch any Exception, and it still doesn't seem to do so.
int length = -1;
try
{
	length = fromClient.Receive(sData);
}
catch (Exception e)
{
	Console.WriteLine("Exception: {0}", e.ToString());
}
thisLine += Encoding.ASCII.GetString(sData, 0, length);

Again, this exception ONLY occurs when using the GUI client to connect; telnet does not cause the "catch" block to execute or anything of the sort.

Any ideas as to why this is happening and/or how to fix it are greatly appreciated.
  • 0

Advertisements


#2
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Hi W-Unit,

From what I can see, you're getting the exception because you're closing the GUI mid-connection. I'm not very familiar with C#, but I think you have to get the program to automatically close the connection if the GUI is closed?
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP