Form1.cs
***********************************************************
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;
namespace Sensor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
NetClient net;
private void Form1_Load(object sender, EventArgs e)
{
net = new NetClient();
}
private void button1_Click(object sender, EventArgs e)
{
net.ConnectToHost("172.20.130.48",3000);
}
private void button2_Click(object sender, EventArgs e)
{
net.WriteData("client send hello to server");
}
}
}
NetClient.cs
***********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Windows.Forms;
namespace Sensor
{
class NetClient
{
//网络的客户端连接
public NetClient()
{
//构造函数
}
Socket client;
string ip;
int port;
byte[] buff = new byte[1024];
public void ConnectToHost(string ip,int port)
{
//这个可以直接用来连接公网IP
this.ip = ip;
this.port = port;
Socket cli = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
cli.BeginConnect(ip, port, new AsyncCallback(Connected), cli); //开始连接(异步的)
}
private void Connected(IAsyncResult ias)
{
//回调函数:IAsyncResult ias表示回调
client = (Socket)ias.AsyncState;
if (client.Connected)
{
Console.WriteLine("连接成功!!!");
//label1.Text = String.Format("连接成功!!!");
client.BeginReceive(buff,0,buff.Length,0,new AsyncCallback(ReadyRead),client); //开始接受数据到buff缓冲区,用ReadyRead返回
}
else
{
Console.WriteLine("连接失败!!!");
}
}
private void ReadyRead(IAsyncResult ias)
{
//异步的读
Socket cli = (Socket)ias.AsyncState;
int length = cli.EndReceive(ias);//读完
if (length > 0)
{
//>0则对数据处理
byte[] tmp = new byte[length];
Array.Copy(buff, tmp, length);//拷贝到buff
Console.WriteLine(Encoding.Default.GetString(tmp));
cli.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(ReadyRead), client);
}
else
{
Console.WriteLine("长度<0 ->网络断开");
}
}
public void WriteData(string data)
{
//异步发送数据
byte[] value = Encoding.Default.GetBytes(data);
this.client.BeginSend(value,0,value.Length,0, new AsyncCallback(ReadyWrite), client);
}
private void ReadyWrite(IAsyncResult ias)
{
Socket cli = (Socket)ias.AsyncState;
int len = cli.EndSend(ias);
}
}
}
***********************************************************
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;
namespace Sensor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
NetClient net;
private void Form1_Load(object sender, EventArgs e)
{
net = new NetClient();
}
private void button1_Click(object sender, EventArgs e)
{
net.ConnectToHost("172.20.130.48",3000);
}
private void button2_Click(object sender, EventArgs e)
{
net.WriteData("client send hello to server");
}
}
}
NetClient.cs
***********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Windows.Forms;
namespace Sensor
{
class NetClient
{
//网络的客户端连接
public NetClient()
{
//构造函数
}
Socket client;
string ip;
int port;
byte[] buff = new byte[1024];
public void ConnectToHost(string ip,int port)
{
//这个可以直接用来连接公网IP
this.ip = ip;
this.port = port;
Socket cli = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
cli.BeginConnect(ip, port, new AsyncCallback(Connected), cli); //开始连接(异步的)
}
private void Connected(IAsyncResult ias)
{
//回调函数:IAsyncResult ias表示回调
client = (Socket)ias.AsyncState;
if (client.Connected)
{
Console.WriteLine("连接成功!!!");
//label1.Text = String.Format("连接成功!!!");
client.BeginReceive(buff,0,buff.Length,0,new AsyncCallback(ReadyRead),client); //开始接受数据到buff缓冲区,用ReadyRead返回
}
else
{
Console.WriteLine("连接失败!!!");
}
}
private void ReadyRead(IAsyncResult ias)
{
//异步的读
Socket cli = (Socket)ias.AsyncState;
int length = cli.EndReceive(ias);//读完
if (length > 0)
{
//>0则对数据处理
byte[] tmp = new byte[length];
Array.Copy(buff, tmp, length);//拷贝到buff
Console.WriteLine(Encoding.Default.GetString(tmp));
cli.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(ReadyRead), client);
}
else
{
Console.WriteLine("长度<0 ->网络断开");
}
}
public void WriteData(string data)
{
//异步发送数据
byte[] value = Encoding.Default.GetBytes(data);
this.client.BeginSend(value,0,value.Length,0, new AsyncCallback(ReadyWrite), client);
}
private void ReadyWrite(IAsyncResult ias)
{
Socket cli = (Socket)ias.AsyncState;
int len = cli.EndSend(ias);
}
}
}