Hi All,
I tried create the tcpclient socket program. But my program has exception with receivesocket() function aften connected server.Please somebody can help me.
Exception:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
Receive Socket Error: System.Threading.ThreadAbortException: Thread was being aborted.
at System.IO.__ConsoleStream.WriteFile(SafeFileHandle handle, Byte* bytes, Int32 numBytesToWrite, Int32& numBytesWritten, IntPtr mustBeZero)
at System.IO.__ConsoleStream.WriteFileNative(SafeFileHandle hFile, Byte[] bytes, Int32 offset, Int32 count, Int32 mustBeZero, Int32& errorCode)
at System.IO.__ConsoleStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
at System.IO.TextWriter.WriteLine(String value)
at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
at System.Console.WriteLine(String value)
at Xilath.GameObject.GameSocket.receiveSocket() in C:\WindowsApplication1\WindowsApplication1\GameObject\GameSocket.cs:line 93
Thread - caught ThreadAbortException - resetting.
Exception message: Thread was being aborted.
Coding
NetworkStream ns;
StreamReader sr;
TcpClient clientsocket;
bool connected;
Thread receive;
string serveraddress = "127.0.0.1";
int serverport = 4002;
Form form;
bool readData;
public String errorMessage;
public GameSocket(Form form)
{
this.form = form;
Connect();
}
private void Connect()//<===========35
{
socketConnection();
//Send_Message("newuser");
//RegisterWithServer();
if (connected)
{
//Console.WriteLine("Receive");
ThreadStart myThreadDelegate = new ThreadStart(receiveSocket);
receive = new Thread(new ThreadStart(myThreadDelegate));
receive.Start();
Thread.Sleep(100);
receive.Abort();
receive.Join();
}
}//<==================47
public void socketConnection()
{
try
{
clientsocket = new TcpClient(serveraddress, serverport);
ns = clientsocket.GetStream();
sr = new StreamReader(ns);
connected = true;
}
catch (Exception e)
{
//Thread.ResetAbort();
Console.WriteLine("Connect Error: " + e);
}
}
private void receiveSocket() //65
{
try
{
/*if (ns.DataAvailable)
{
if (ns.CanRead)
{
Byte[] buffer = new Byte[ns.Length];
ns.Read(buffer, 0, buffer.Length);
string chatter = System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens = chatter.Split(new Char[] { ',' });
if (tokens[0] == "CHAT")
{
}
}
}*/
while (!readData && ns.CanRead)
{
//if data available then read from the stream
Console.WriteLine("Running");
if (ns.DataAvailable)
{
Console.WriteLine("0");
errorMessage = "connect data";
form.Dispose();
Console.WriteLine("0");
Byte[] buffer = new Byte[2048];
ns.Read(buffer, 0, buffer.Length);
string chatter = System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens = chatter.Split(new Char[] { ',' });
//Console.WriteLine("4" + tokens[0]);
//Console.WriteLine("4" + tokens[1]);
/* if (tokens[0] == "CHAT")
{
}*/
readData = true;
}
}
}
catch(ThreadAbortException e)
{
/* StackTrace st = new StackTrace(true); // true means get line numbers.
foreach(StackFrame f in st.GetFrames()){
Console.Write("CAll Stack: "+f);
}*/
Console.WriteLine("Receive Socket Error: " + e);
//form.Dispose();
Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
Console.WriteLine("Exception message: {0}", e.Message);
errorMessage = e.Message;
Thread.ResetAbort();
}
Console.WriteLine("Thread - still alive and working.");
// Thread.Sleep(1000);
Console.WriteLine("Thread - finished working.");
}//99

Socket Problem?
xlordt
KAAU
hi louthy,
sorry, formDispose() is remark coding no runing.
RDH123
Hi micvos,
i tried remove receive.abort(). But the exception still appeared.Can you give me some example
Thanks for you help.
spshah
First of all Remove this line:
receive.Abort(); // It causes the abortion of the receive thread...
Secondly, to be honest, following portion of Code is un predictable at you have commented half of the Code and we never know what version of code you are trying and what version you have posted.
Anyhow the ThreadAbortException was due to receive.Abort() simple. Double check you code and make sure all things are going fine, Debug you code and see when the Thread is aborted.....
Best Regards,
Rizwan
Christoph Hornung
RufusLDK
I haven't really looked deeply at what's going on, but why are you disposing the form in your recieveSocket method Surely disposing the form will cause your threads to be aborted Hence the error
form.Dispose();
bhaskar27in