well... sorry for making the the thread too long but still there are questions that can be asked through this thread. my last async server was working very well but now i can find out how to change options for ssl. i wish someone has some exprience in it and give me some good explanations:
this is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Newtonsoft.Json;
namespace server_winform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// just defining some of our main variables
static Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static List<myClient> clients = new List<myClient>();
static List<Socket> sclients = new List<Socket>();
ManualResetEvent dowork = new ManualResetEvent(false);
// this generates keys for data connection when game starts
private void button1_Click(object sender, EventArgs e)
{
try
{
richTextBox1.AppendText(Environment.NewLine + "start to setup server" );
IPEndPoint serverEP = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt16(textBox2.Text));
server.Bind(serverEP);
server.Listen(5);
Thread th = new Thread(new ThreadStart(() => listening()));
th.Start();
richTextBox1.AppendText(Environment.NewLine + "server been setup and waiting for clients");
}// end of try
catch(Exception ee)
{
richTextBox1.AppendText(Environment.NewLine+"it was impossible to setup server"+ee.Message);
}// end of catch
}// end of button
public void listening()
{
while (true)
{
dowork.Reset();
server.BeginAccept(10,new AsyncCallback(AcceptCallBack), server);
dowork.WaitOne();
}
}
int count = 0;
public void AcceptCallBack(IAsyncResult ar)
{
// clients.Add(new myClient(server.EndAccept(ar)));
// try
// {
myClient c = new myClient();
Socket handle = (Socket)ar.AsyncState;
byte[] buff ;
Socket hand = handle.EndAccept(out buff,ar);
dowork.Set();
c.socket = hand;
clients.Add(c);
// richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += "client :" +count.ToString() ; }), null);
// richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += Encoding.ASCII.GetString(buff,0,buff.Length); }), null);
richTextBox1.AppendText(Environment.NewLine + "client :" + count.ToString());
richTextBox1.AppendText(Environment.NewLine + Encoding.ASCII.GetString(buff, 0, buff.Length));
hand.BeginSend(buff, 0, buff.Length, 0, new AsyncCallback(sendCallBack), c);
count++;
hand.BeginReceive(c.buffer, 0, c.buffer.Length, SocketFlags.None, new AsyncCallback(receiveIDCallBack), c);
// }
// catch(Exception)
// {
// }
}//end of acceptcallback function
public void receiveIDCallBack(IAsyncResult ar)
{
// myClient obj =(myClient) clients[0];
try
{
myClient c = (myClient)ar.AsyncState;
Socket handler = c.socket;
c.sb.Clear();
c.sb.Append(Encoding.ASCII.GetString(c.buffer));
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
c.sb.Append(Encoding.ASCII.GetString(
c.buffer, 0, bytesRead));
string content = c.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += content; }), null);
// richTextBox1.AppendText(Environment.NewLine+content);
handler.BeginReceive(c.buffer, 0, c.buffer.Length, 0,new AsyncCallback(receiveIDCallBack), c);
}
else
{
handler.BeginReceive(c.buffer, 0, c.buffer.Length, 0,new AsyncCallback(receiveIDCallBack), c);
}
}
// handler.BeginSend(c.buffer, 0, c.buffer.Length, 0, new AsyncCallback(sendCallBack), c);
//richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text =c.sb.ToString(); }), null);
//c.socket.BeginReceive(c.buffer, 0, c.buffer.Length, SocketFlags.None, new AsyncCallback(receiveIDCallBack), c);
}
catch (Exception e)
{
}
}//end of receiveIDCallBack function
public void sendCallBack(IAsyncResult ar)
{
// richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text = "your data sent"; }), null);
richTextBox1.AppendText(Environment.NewLine+"data send");
}// end of sendCallBack function
}//end of class
}//end of namespace