Ok, I studied a bit on asynchronous TCP network connection. I tried making one but failed. What I want to do is make sure the server or client is always ready to receive a chat and is able to send a chat any time. I do not want them to be on alternate mode.
e.g. Server Send while client waits to receive thus client can't send at that time. I do not want that!
Did this on Windows Application. Once I connected, the system resource just shot to 100% =/
Server Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace AsyncServerChat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Socket g_server_conn;
private byte[] g_bmsg;
private bool check = false;
private void Form1_Load(object sender, EventArgs e)
{
IPEndPoint local_ep = new IPEndPoint(IPAddress.Any, 9050);
Socket winsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
winsock.Bind(local_ep);
winsock.Listen(5);
winsock.BeginAccept(new AsyncCallback(Accept), winsock);
}
private void Accept(IAsyncResult iar)
{
Socket server_conn =(Socket) iar.AsyncState;
g_server_conn = server_conn.EndAccept(iar);
//label1.Text = "Connected. . .";
while (g_server_conn.Connected && check == false)
{
g_bmsg = new byte[1024];
check = true;
g_server_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(Recieve), g_server_conn);
}
}
private void Send(IAsyncResult iar)
{
Socket server_conn = (Socket)iar.AsyncState;
server_conn.EndSend(iar);
}
private void Recieve(IAsyncResult iar)
{
Socket server_conn =(Socket) iar.AsyncState;
server_conn.EndReceive(iar);
if (g_bmsg.Length != 0)
{
label1.Text = Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length);
check = false;
}
}
private void sendButton_Click(object sender, EventArgs e)
{
string strmsg = textBox1.Text;
byte[] bmsg= Encoding.ASCII.GetBytes(strmsg);
g_server_conn.BeginSend(bmsg, 0, bmsg.Length, SocketFlags.None, new AsyncCallback(Send), g_server_conn);
}
}
}
Client
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace AsyncClientChat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket g_client_conn;
byte[] g_bmsg;
private bool check = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void connectButton_Click(object sender, EventArgs e)
{
IPEndPoint remote_ep = new IPEndPoint(IPAddress.Parse(textBox1.Text), 9050);
g_client_conn = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
g_client_conn.BeginConnect(remote_ep, new AsyncCallback(Connect), g_client_conn);
}
private void Connect(IAsyncResult iar)
{
Socket client_conn =(Socket) iar.AsyncState;
client_conn.EndConnect(iar);
while (g_client_conn.Connected)
{
g_bmsg = new byte[1024];
check = true;
g_client_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(Recieve), g_client_conn);
}
}
private void Send(IAsyncResult iar)
{
Socket client_conn = (Socket)iar.AsyncState;
client_conn.EndSend(iar);
}
private void Recieve(IAsyncResult iar)
{
Socket client_conn = (Socket)iar.AsyncState;
client_conn.EndReceive(iar);
if (g_bmsg.Length != 0)
{
label1.Text = Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length);
check = false;
}
}
}
}