WPF MainWindow closes automatically soon after showed for 1 sec. I have only this mainwindow in the wpf project. There is no error showing when running the application. Without showing any error, the MainWindow simply closes soon after loaded. There is nothing code in App.xaml.cs.
Here is the code written in the MainWindow.
public partial class MainWindow : Window
{
private Socket clientSocket;
private byte[] buffer;
public MainWindow()
{
InitializeComponent();
}
private bool CheckField()
{
if (txtIP.Text == string.Empty)
{
MessageBox.Show("Please specify IP Address");
return false;
}
if (txtPort.Text == string.Empty)
{
MessageBox.Show("Please specify Port Number");
return false;
}
return true;
}
private void btnGreen_Click(object sender, RoutedEventArgs e)
{
if(CheckField()) SendMessage("GREEN");
}
private void btnRed_Click(object sender, RoutedEventArgs e)
{
if (CheckField()) SendMessage("RED");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// if below code commented, window will not close
clientSocket.BeginConnect(new IPEndPoint(IPAddress.Loopback,
3333), new AsyncCallback(ConnectCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ConnectCallback(IAsyncResult ar)
{
clientSocket.EndConnect(ar);
}
public void SendMessage(string xmlstring)
{
try
{
byte[] xmlbuffer = Encoding.ASCII.GetBytes(xmlstring);
clientSocket.BeginSend(xmlbuffer, 0, xmlbuffer.Length,
SocketFlags.None, new AsyncCallback(SendCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SendCallback(IAsyncResult ar)
{
clientSocket.EndSend(ar);
buffer = new byte[clientSocket.ReceiveBufferSize];
}
}
What's wrong in here ???
Related
I have a class where i listen for connections in a separated thread, my problem is that when i try close the socket, comes a exception in Socket.Accept(); that says:
A blocking operation was interrupted by a call to WSACancelBlockingCall
After a seach, i understood that probably this is because i'm closing the socket from another thread (in my case this thread is UI). How i can close and open the socket correctly anytime?
Listener
class Listener
{
private Socket s;
private bool listening = false;
public bool Running
{
get { return listening; }
}
public Listener()
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void BeginListen(int port)
{
s.Bind(new IPEndPoint(IPAddress.Any, port));
s.Listen(100);
listening = true;
while (listening)
{
Socket clientSocket = s.Accept();
}
}
public void StopListen()
{
if (listening == true)
{
s.Close();
listening = false;
}
}
}
frmMain
public partial class frmMain : Form
{
private Listener listener;
private void frmMain_Load(object sender, EventArgs e)
{
listener = new Listener();
}
private void listen()
{
listener.BeginListen(101);
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
listener.StopListen();
}
private void tmrcheck_Tick(object sender, EventArgs e)
{
if (listener.Running)
{
startToolStripMenuItem.Enabled = false;
stopToolStripMenuItem.Enabled = true;
}
if (!listener.Running)
{
startToolStripMenuItem.Enabled = true;
stopToolStripMenuItem.Enabled = false;
}
}
}
I'm learning to code and I'm currently making a server and a client with two-way communication. Is there a way to make the received message appear on the left side of the textbox and the sent message on the right side of the textbox just like in a real chat service. Is there also a way to add the timestamp, when the message was received and when it was sent?
I will attach all the code that I have written and images of the client and server windows.
Server
Client
Server code:
namespace Full_Chatt_Server
{
public partial class Form1 : Form
{
TcpListener listener;
TcpClient klient;
int port;
public Form1()
{
InitializeComponent();
}
private void btnStartServer_Click(object sender, EventArgs e)
{
btnTaEmot.Enabled = false;
port = int.Parse(tbxPort.Text);
try
{
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
StartAccepting();
}
public async void StartAccepting()
{
try
{
klient = await listener.AcceptTcpClientAsync();
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
StartReading(klient);
}
public async void StartReading(TcpClient k)
{
byte[] buffer = new byte[1024];
int n = 0;
try
{
n = await k.GetStream().ReadAsync(buffer, 0, 1024);
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
tbxMessage.AppendText(Encoding.Unicode.GetString(buffer, 0, n) + "\r\n");
StartReading(k);
}
private void btnSend_Click(object sender, EventArgs e)
{
string chatt = tbxChatt.Text;
StartSending(chatt);
tbxChatt.Clear();
tbxMessage.AppendText(chatt + "\r\n");
}
public async void StartSending(string message)
{
if (klient.Connected)
{
byte[] OutStream = Encoding.Unicode.GetBytes(message);
try
{
await klient.GetStream().WriteAsync(OutStream, 0, OutStream.Length);
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
}
}
}
Client code:
namespace Full_Chatt_Klient
{
public partial class Form1 : Form
{
TcpClient klient = new TcpClient();
int port;
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (!klient.Connected)
{
Connect();
}
}
public async void Connect()
{
IPAddress address = IPAddress.Parse(tbxIP.Text);
port = int.Parse(tbxPort.Text);
try
{
await klient.ConnectAsync(address, port);
StartReading(klient);
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
btnSkicka.Enabled = true;
btnAnslut.Enabled = false;
}
private void btnSend_Click(object sender, EventArgs e)
{
string chatt = tbxChatt.Text;
StartSending(chatt);
tbxChatt.Clear();
tbxMessage.AppendText(chatt + "\r\n");
}
public async void StartSending(string message)
{
if (klient.Connected)
{
byte[] OutStream = Encoding.Unicode.GetBytes(message);
try
{
await klient.GetStream().WriteAsync(OutStream, 0, OutStream.Length);
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
}
}
public async void StartReading(TcpClient k)
{
byte[] buffer = new byte[1024];
int n = 0;
try
{
n = await k.GetStream().ReadAsync(buffer, 0, 1024);
}
catch (Exception error)
{
MessageBox.Show(error.Message, Text);
return;
}
tbxMessage.AppendText(Encoding.Unicode.GetString(buffer, 0, n) + "\r\n");
StartReading(k);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (klient != null)
{
klient.Close();
}
}
}
}
Everything is going fine with connection , except when i close the connection and reopen (everything fine till now , data is received fine).
The error pops up when I try to write in the stream :
Cannot access a disposed object: 'System.Net.Sockets.NetworkStream' !!
I have tried using a new instance of client each time I connect
When closing connection : i have tried all these things like _client.GetStream().Close();
_client.Close();
but everything fail
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myform = this;
}
public static Form1 myform;
Client c1;
public void ConnectButton_Click(object sender, EventArgs e)
{
c1 = new Client();
try
{ c1.Connect(IPADRESStext.Text, PORTNUMBERtext.Text); }
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
private void CloseButton_Click(object sender, EventArgs e)
{
c1.Disconnect();
}
}
public sealed partial class Client
{
public void SendData(byte[] data)
{ _sender.SendData(data); }
public event EventHandler<DataReceivedEventArgs> DataReceived;
public void Connect(string IPADRESStxt, string PORTNUMBERtxt)
{
sq = new sequence();
_client = new TcpClient(IPADRESStxt, Int32.Parse(PORTNUMBERtxt));
_stream = _client.GetStream();
_sender = new Sender(_stream);
_receiver = new Receiver(_stream);
_receiver.DataReceived += OnDataReceived;
}
public void Disconnect()
{
_stream.Close();
_client.Close();
//_client.Client.Disconnect(false);
//tcpClient.GetStream().Close();
//tcpClient.Close();
}
private void OnDataReceived(object sender, DataReceivedEventArgs e)
{
var handler = DataReceived;
if (handler != null) DataReceived(this, e); // re-raise event
}
private TcpClient _client;
private NetworkStream _stream;
private Receiver _receiver;
private Sender _sender;
}
public sealed partial class Client
{
private sealed class Sender
{
internal void SendData(byte[] data)
{
_stream.Write(data, 0, data.Length); /// 2 When trying to execute this instruction
}
internal Sender(NetworkStream stream)
{
_stream = stream;
_thread = new Thread(Run);
_thread.Start();
}
private void Run()
{
// Code Code Code
SendData(Sframe); /// 1 Error pops up here
}
private NetworkStream _stream;
private Thread _thread;
}
}
I'm quite new to sockets programming. I hope the problem is presented understandable.
The problem is that when I use Client's button1_click to send textbox's
content - the server only gets the first message. I have no idea what's going wrong.
What might be the problem?
Here is Server:
public partial class Form1 : Form
{
Socket server;
byte[] byteData = new byte[1024];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 20000);
server.Bind(endPoint);
server.Listen(1);
server.BeginAccept(new AsyncCallback(Accept), null);
textBox1.Text = "Server started...";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Accept(IAsyncResult ar)
{
try
{
Socket client = server.EndAccept(ar);
server.BeginAccept(new AsyncCallback(Accept), null);
client.BeginReceive(byteData, 0, byteData.Length,
SocketFlags.None, new AsyncCallback(Receive), client);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Receive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndReceive(ar);
textBox1.Invoke(new Action(delegate ()
{
textBox1.AppendText(Environment.NewLine
+ Encoding.ASCII.GetString(byteData));
}));
byteData = null;
}
}
And here is client:
public partial class Form1 : Form
{
Socket client;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == null)
button1.Enabled = false;
else
button1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ip, 20000);
client.Connect(ipEndPoint);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] byteData = Encoding.ASCII.GetBytes(textBox1.Text);
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(Send), null);
textBox1.Text = String.Empty;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Send(IAsyncResult ar)
{
try
{
client.EndSend(ar);
}
catch (ObjectDisposedException)
{ }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
In Async, you got to re-init the BeginReceive in the server side whenever you want to listen to a message.
Therefore, in your Receive callback, you should re-init your BeginReceive after EndReceive. Otherwise you cannot get the next message:
private void Receive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndReceive(ar);
client.BeginReceive(byteData, 0, byteData.Length, //add BeginReceive again
SocketFlags.None, new AsyncCallback(Receive), client);
textBox1.Invoke(new Action(delegate ()
{
textBox1.AppendText(Environment.NewLine
+ Encoding.ASCII.GetString(byteData));
}));
byteData = null;
}
For more of working example with Async, check this out: Sending a value from server to client with sockets
Im learning how to use Async tcp sockets server/client
Cant figure out why my server is not continue listening to data sent from the client...
The messagebox just appear once, then I need to re-open the server
This is my code ( Client )
public partial class Form1 : Form
{
private Socket client;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(new IPEndPoint(IPAddress.Loopback, 8888), new AsyncCallback(ConnectCallback), null);
}
catch
{
}
}
private void ConnectCallback(IAsyncResult AR)
{
try
{
client.EndConnect(AR);
}
catch
{
}
}
private void SendCallback(IAsyncResult AR)
{
try
{
client.EndSend(AR);
}
catch
{
}
}
private void button1_Click(object sender, EventArgs e)
{
byte[] buffer = Encoding.ASCII.GetBytes("Hello World!");
client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), null);
}
}
Server
private Socket sck, sckc;
private byte[] buffer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck.Bind(new IPEndPoint(IPAddress.Any, 8888));
sck.Listen(0);
sck.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
catch
{
}
}
private void AcceptCallback(IAsyncResult AR)
{
try
{
while (true)
{
sckc = sck.EndAccept(AR);
buffer = new byte[sckc.ReceiveBufferSize];
sckc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
}
catch
{
}
}
private void ReceiveCallback(IAsyncResult AR)
{
try
{
string text = Encoding.ASCII.GetString(buffer);
MessageBox.Show(text);
}
catch
{
}
}
Get rid of the while loop. In the receive callback, you have to call BeginReceive() again so you can listen for the next set of packets:
private void AcceptCallback(IAsyncResult AR)
{
try
{
sckc = sck.EndAccept(AR);
buffer = new byte[sckc.ReceiveBufferSize];
sckc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch
{
}
}
private void ReceiveCallback(IAsyncResult AR)
{
try
{
string text = Encoding.ASCII.GetString(buffer);
MessageBox.Show(text);
sckc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch
{
}
}
If you intend on having multiple connections then you need to maintain a separate buffer for each connection.