b/s:
private void Button1_Click(object sender, System.EventArgs e)
{
this.Connect(this.TextBox1.Text.Trim());
}
private void Connect(string objectIP)
{
try
{
if( objectSock != null && objectSock.Connected )
{
objectSock.Shutdown( SocketShutdown.Both );
System.Threading.Thread.Sleep( 10 );
objectSock.Close();
}
objectSock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
IPEndPoint objectEndPoint = new IPEndPoint(IPAddress.Parse(objectIP),5665);
EndPoint ep=objectEndPoint as EndPoint;
AsyncCallback onConnect = new AsyncCallback(OnConnect);
objectSock.BeginConnect(ep,onConnect,objectSock);
}
catch( Exception ex )
{
string cc=ex.Message;
}
}
public void OnConnect( IAsyncResult ar )
{
Socket sock = (Socket)ar.AsyncState;
try
{
if( sock.Connected)
{
Byte[] sendmsg= System.Text.Encoding.Unicode.GetBytes(printstring.ToCharArray());
sock.Send(sendmsg,0,sendmsg.Length,SocketFlags.None);
System.Threading.Thread.Sleep(100);
}
}
catch
{
}
}
c/s:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Xml;
namespace Recruit
{
/// <summary>
/// frmWork 的摘要说明。
/// </summary>
public class frmWork : System.Windows.Forms.Form
{
public string name="";
private Socket port;//本地的5665的Socket连接
private IPEndPoint point;//本地的IP地址
private Thread thread;//端口监听线程
private Hashtable hst=new Hashtable();
private byte [] charRecieved;//获取的字符串
DataPort.JKYTestPort.TestPort tp=new DataPort.JKYTestPort.TestPort();
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button4;
// private string data="";
[STAThread]
static void Main()
{
Application.Run(new frmWork());
}
public frmWork()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmWork));
this.button4 = new System.Windows.Forms.Button();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button4
//
this.button4.Location = new System.Drawing.Point(32, 8);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 23);
this.button4.TabIndex = 7;
this.button4.Text = "Winkillword";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 8;
this.button1.Text = "隐藏窗口";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// frmWork
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(240, 45);
this.Controls.Add(this.button1);
this.Controls.Add(this.button4);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "frmWork";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Winkillword";
this.Load += new System.EventHandler(this.frmWork_Load);
this.ResumeLayout(false);
}
#endregion
private void frmWork_Load(object sender, System.EventArgs e)
{
#region 获取当前的IP地址并绑定端口
this.GetLocalIP();
#endregion
#region 启动端口监听线程
thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ListenPort));
thread.Name="端口监听线程";
thread.Start();
#endregion
#region 配置数据包大小
int tmp=2048;
this.charRecieved=new Byte[tmp];
#endregion
}
/// <summary>
/// 获取当前的IP
/// </summary>
private void GetLocalIP()
{
IPAddress [] aryLocalAddr = null;
string strHostName = "";
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName( strHostName );
aryLocalAddr = ipEntry.AddressList;
point=new IPEndPoint(aryLocalAddr[0],5665);
}
#region 获取5665端口的数据流
/// <summary>
/// 侦听5665端口
/// </summary>
private void ListenPort()
{
int max=10;
port = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
port.Bind(point);
port.Listen( max );
AsyncCallback OnPortConnect= new AsyncCallback(PortConnected);
port.BeginAccept(OnPortConnect,port);
}
/// <summary>
/// 当有连接请求时
/// </summary>
/// <param name="ar"></param>
private void PortConnected( IAsyncResult ar)
{
button4_Click(null,null);
Socket tmp = (Socket)ar.AsyncState;
AsyncCallback OnPortConnect= new AsyncCallback(PortConnected);
tmp.BeginAccept(OnPortConnect, tmp );
}
#endregion
private void button4_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process[] process=System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach(System.Diagnostics.Process p in process)
{
p.Kill();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Show();
this.notifyIcon1.Visible=false;
}
}
}