Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Exception:System.Net.Sockets.SocketException:

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
larspeter@faerge.dk - 14 Sep 2007 11:07 GMT
Hi all.

I have a problem with TcpClient ... I am conneting to a server with
TcpClient and returning the answer through a webservice.

It actully all works fine. BUT if I make a lot of (re)connection
(hitting the submit button) then I start to recieve a an error:

IOException:System.IO.IOException: Der kunne ikke læses data fra
transportforbindelsen (LPF: could not read data from the transport
connection): An existing connection was forcibly closed by the remote
host ---> System.Net.Sockets.SocketException: En eksisterende
forbindelse blev tvangsafbrudt af en ekstern vært ved
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags) ved
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size) --- Slut på staksporing af indre undtagelser --- ved
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size) ved SocketApplication.TrafikSocket.Connect(String server,
Int32 port, String message) i c:\www\WebSocket\App_Code
\TrafikSocket.cs:linje 63

It starts failing in the line: Int32 bytes = stream.Read(data, 0,
data.Length); The program that I am sending the reqeust to has to be
restartet before the system answers again. Nothing in that program is
hanging and the Port is listining...

Any one has any idea where to debug that problem?! Currently I have no
idea :-(

       public string Connect(String server, Int32 port, String
message)
       {

           // variablerne er benyttes
           TcpClient client = null;
           NetworkStream stream = null;

           try
           {

               client = new TcpClient(server, port);

               // Translate the passed message into ASCII and store
it as a Byte array.
               Byte[] data =
System.Text.Encoding.UTF8.GetBytes(message);

               // Get a client stream for reading and writing.
               // Stream stream = client.GetStream();
               stream = client.GetStream();

               // String to store the response UTF7 representation.
               String responseData = String.Empty;

               // Send the message to the connected TcpServer.
               if (stream.CanWrite)
               {
                   stream.Write(data, 0, data.Length);

                   // Receive the TcpServer.response.
                   // Buffer to store the response bytes.
                   data = new Byte[256];

                   // Read the first batch of the TcpServer response
bytes.
                   if (stream.CanRead)
                   {
                       Int32 bytes = stream.Read(data, 0,
data.Length);
                       responseData =
System.Text.Encoding.UTF7.GetString(data, 0, bytes);
                   }
               }

               // Close everything.
               stream.Close();
               client.Close();

               return responseData;
           }
...
UL-Tomten - 14 Sep 2007 13:42 GMT
On Sep 14, 12:07 pm, larspe...@faerge.dk wrote:

> The program that I am sending the reqeust to has to be restartet before
> the system answers again.

This suggests the problem is in the server, and not your client. I
assume restarting the client doesn't change anything?

(Disclaimer: I didn't look at your code.)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.