tcp助手类是在socket层上建立的,可以直接通过client或server属性设置或获取。
#region usings
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;
using System.Threading;
#endregion
namespace Server
{
public partial class TalkServer : Form
{
#region Parameters
IPEndPoint remoteIpep;
TcpClient tcpc,tcpcb;
TcpListener tcpl;
Thread threadL;
NetworkStream ns;
delegate void AppendItem(object o);
AppendItem AddItem;
Socket connectSocket;
void addItem(object o)
{
listBox.Items.Add(o);
}
#endregion
#region Events
public TalkServer()
{
InitializeComponent();
AddItem = new AppendItem(addItem);
}
private void btServerStart_Click(object sender, EventArgs e)
{
tcpl = new TcpListener(IPAddress.Any, 2008);
try
{
tcpl.Start(10);
threadL = new Thread(new ThreadStart(acceptCnns));
threadL.IsBackground = true;
threadL.Start();
listBox.Items.Add("Server start success");
}
catch (Exception exc)
{
listBox.Items.Add(exc.Message);
throw;
}
}
#endregion
#region Methods
private void acceptCnns()
{
string welcome = "welcome to server!";
byte[] buf = Encoding.Unicode.GetBytes(welcome);
while (true)
{
try
{
tcpc = tcpl.AcceptTcpClient();
remoteIpep = (IPEndPoint ) tcpc.Client.RemoteEndPoint;
ns = tcpc.GetStream();
ns.Write(buf, 0, buf.Length);
ThreadPool.QueueUserWorkItem(new WaitCallback(receiveData));
}
catch (Exception)
{
throw;
}
}
}
private void receiveData(object o)
{
int len = tcpc.Available;
byte[] buf = new byte[1024];
string data = string.Empty;
while (true)
{
len = tcpc.Available;
if (len != 0)
{
ns.Read(buf, 0, len);
data = Encoding.Unicode.GetString(buf, 0, len);
listBox.Invoke(AddItem, data);
}
}
}
#endregion
private void btConnect_Click(object sender, EventArgs e)
{
tcpcb = new TcpClient();
tcpcb.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
try
{
tcpcb.Connect(remoteIpep);
//ns = tcpcb.GetStream();
listBox.Invoke(AddItem, "Connected!");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
}
#region usings
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;
using System.Threading;
#endregion
namespace Client
{
public partial class TalkClient : Form
{
#region Parameters
TcpClient tcpc,tcpc2,tcpcb,tcpcr;
TcpListener tcpl;
NetworkStream ns;
Thread threadR;
Thread threadL;
delegate void AppendItem(object o);
AppendItem AddItem;
void addItem(object o)
{
listBox.Items.Add(o);
}
#endregion
#region Events
public TalkClient()
{
InitializeComponent();
AddItem = new AppendItem(addItem);
}
private void btListen_Click(object sender, EventArgs e)
{
}
private void TalkClient_Load(object sender, EventArgs e)
{
}
private void btCnnect_Click(object sender, EventArgs e)
{
string target = txtTarget.Text.Trim();
IPAddress ipa = Dns.GetHostAddresses(target)[0];
IPEndPoint ipep = new IPEndPoint(ipa, 2008);
tcpc = new TcpClient();
try
{
tcpc.Connect(ipep);
listBox.Items.Add("connected!");
ns = tcpc.GetStream();
tcpcr = tcpc;
threadR = new Thread(new ThreadStart(receiveData));
threadR.IsBackground = true;
threadR.Start();
listBox.Items.Add("thread receive started!");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
tcpc2 = new TcpClient();
tcpc2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
try
{
tcpc2.Connect(ipep);
listBox.Items.Add("Connection 2 connected!");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void btSend_Click(object sender, EventArgs e)
{
string data = txtMessage.Text.Trim();
try
{
byte[] buf = Encoding.Unicode.GetBytes(data);
ns.Write(buf, 0, buf.Length);
}
catch (Exception exc)
{
listBox.Items.Add(exc.Message);
throw;
}
}
#endregion
#region Methods
void receiveData()
{
string data = string.Empty;
byte[] buf = new byte[1024];
int len = 0;
while (true)
{
len = tcpcr.Available;
if ( len != 0)
{
ns.Read(buf, 0, len);
data = Encoding.Unicode.GetString(buf, 0, len);
listBox.Invoke(AddItem, data);
}
}
}
#endregion
}
}
本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2008/08/11/1265309.html,如需转载请自行联系原作者