|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis is a simple MSN Messenger like chat application using socket programming. It allows the users to send and receive messenges by running two Simple Messengers. TherThere are two specific features that other than the regular MSN Messenger: BackgroundOne day when I was working on the socket programming, I need to monitor the data transmission. Although the Visual Studio already have the debugger for the user to monitor the data, I still need a third application to cache the data and take a look at them. This comes with the idea for the Simple Messenger.You can use this program as a chat application when you run two Simple Messengers. If you connect your computer to a device, another software, a web application, etc., you will be able to send/receive data via one Simple Messenger. In my case, I just connect my computer to a device which has Ethernet connection and I need to monitor the data sent by the device without interrupting it. The main purpose of this article here is to share this program with you and for me to learn the socket programming and multiple threads. KeyValuePairA helper class wraps a public class KeyValuePair
{
public Socket socket;
public byte[] dataBuffer = new byte[1];
}
ConnectionThe connection is slightly different between the Server Mode and the Client Mode. First look at the Server Mode: if (radioButton1.Checked == true)//Server Mode
{
//Console.WriteLine("Server Mode");
try
{
//create the listening socket...
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, Convert.ToInt16(port));
server.Bind(ipLocal);//bind to local IP Address...
server.Listen(10);//start listening...
//create the call back for any client connections...
server.BeginAccept(new AsyncCallback(OnClientConnect), null);
/* ... */
}
catch (SocketException se)
{
MessageBox.Show("Server Connect Error.\r\n" + se.ToString());
}
The server needs to watch for the connection that if there is any client trying to connect to it. public void OnClientConnect(IAsyncResult asyn)
{
try
{
server = server.EndAccept(asyn);
WaitForData(server);
}
/* ... */
}
Before the server socket receives any data, we must prepare for it ahead. Here the public void WaitForData(Socket soc)
{
try
{
if (asyncCallBack == null) asyncCallBack = new AsyncCallback(OnDataReceived);
KeyValuePair aKeyValuePair = new KeyValuePair();
aKeyValuePair.socket = soc;
// now start to listen for incoming data...
aKeyValuePair.dataBuffer = new byte[soc.ReceiveBufferSize];
soc.BeginReceive(aKeyValuePair.dataBuffer, 0, aKeyValuePair.dataBuffer.Length,
SocketFlags.None, asyncCallBack, aKeyValuePair);
}
/* ... */
}
Now look at the Client Mode: else if (radioButton2.Checked == true)//Client Mode
{
//Console.WriteLine("Client Mode");
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ipAddr), Convert.ToInt16(port));
client.Connect(ipe);
clientListener = new Thread(OnDataReceived);
isEndClientListener = false;
clientListener.Start();
/* ... */
}
catch (SocketException se)
{
MessageBox.Show("Client Connect Error.\r\n" + se.ToString());
}
Data ReceiveSince the For the Server Mode: KeyValuePair aKeyValuePair = (KeyValuePair)asyn.AsyncState;
//end receive...
int iRx = 0;
iRx = aKeyValuePair.socket.EndReceive(asyn);
if (iRx != 0)
{
byte[] recv = aKeyValuePair.dataBuffer;
}
For the Client Mode: byte[] recv = new byte[client.ReceiveBufferSize];//you can define your own size
int iRx = client.Receive(recv );
if (iRx != 0)
{
//recv should contains the received data.
}
Data SendSending the data is as easy as calling the soc.Send(dataBytes);//'soc' could either be the server or the client
Points of InterestEverytime I use the MSN Messenger to chat with my friends, I'm worried that if my conversation will be recorded by 'MSN' or not. Now I'm happy and 'safe' to use my own chat application to chat with my friends, er... within a local network. However, the main benefit of this program is that I learned how to play with the Ethernet and the sockets. I hope this will benefit you as well. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||