I couldn't find my answer in some topics. I have a client/server code. I have a client side and server side in a separate programs . This is client side:
try
{
byte[] bytes = new byte[1024];
string text = "";
text = uitbtextmsg.Text;
byte[] msg = Encoding.ASCII.GetBytes(text);
int bytesSent = User.Send(msg);
int bytesRec = User.Receive(bytes);
uirtbmsg.Text = uirtbmsg.Text + '\n' + " " + text;
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
This is for when I am connected to server and I want to chat with that socket. This is my connection:
byte[] bytes = new byte[1024];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[2];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
Socket User = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
User.Connect(remoteEP);
And this is server side:
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[2];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10000);
while (true)
{
Socket handler = listener.Accept();
data = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
TextRecieved = data;
Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
textbox.Start();
break;
}
//uirtbreport.Text = data;
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
When client first sends a message to server this procedure is ok! But when it send another message it will throw an Exception:
System.Net.Socket.SocketException(0x80004005):An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.Socket.Receive(Byte[] buffer)\r\n at Client.Form1.uibtnsendmsg_Click(Object sender, EventArgs e) in ...