日期:2014-05-17 浏览次数:21140 次
private void button1_Click(object sender, EventArgs e)
{
try
{
int port = 2000;
string host = "192.168.0.127";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
c = null;
c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
c.Connect(ipe);//连接到服务器
}
catch (ArgumentNullException a)
{
MessageBox.Show(String.Format("ArgumentNullException:{0}", a));
}
catch (SocketException a)
{
MessageBox.Show(String.Format("SocketException:{0}", a.Message));
}
}
private void button2_Click(object sender, EventArgs e)
{
string sendStr = "hello!This is a socket test";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length,