TP-Software <tp********@users.sourceforgE.net> wrote:
<snip>
Hmm... I don't get the same problem myself. My test code is below. It
basically writes stuff for 10 seconds before the client's main thread
finishes, which closes the connection and the test server throws an
understandable exception. No reading exceptions though.
TestClient.cs:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
public class TestClient
{
static void Main()
{
Socket skt = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPHostEntry ipa = Dns.Resolve("localhost");
IPEndPoint ip = new IPEndPoint (ipa.AddressList[0], 1234);
skt.Connect (ip);
State state = new State();
state.data = new byte[60];
state.skt = skt;
skt.BeginReceive (state.data, 0, state.data.Length, 0,
new AsyncCallback (AsyncReadCallBack), state);
Thread.Sleep(10000);
}
private static void AsyncReadCallBack(IAsyncResult asyncResult)
{
State state = (State) asyncResult.AsyncState;
Socket socket = state.skt;
int bytesRead = socket.EndReceive(asyncResult);
if (bytesRead >= 0)
{
Console.WriteLine (Encoding.ASCII.GetString
(state.data, 0, bytesRead));
socket.BeginReceive(state.data, 0, state.data.Length, 0,
new AsyncCallback(AsyncReadCallBack), state);
}
else
{
Console.WriteLine ("Finished.");
socket.Close();
}
}
class State
{
internal byte[] data;
internal Socket skt;
}
}
TestServer.cs:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
public class TestServer
{
static void Main()
{
byte[] data = Encoding.ASCII.GetBytes
("abcdefghijklmnopqrstuvwxyzabcdefghijklm"+
"nopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener listener = new TcpListener(ipAddress, 1234);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
while (true)
{
stream.Write (data, 0, data.Length);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too