/***********************************************************************
* 服务端:
* (1)用指定的端口和IP建立EndPoint对象
* (2)建立一个Socket对象
* (3)用Socket对象的Bind()方法绑定IPEndPoint
* (4)用Socket对象的Listen()方法开始监听
* (5)接收到客户端的连接,用Socket对象的Accept()方法创建新的Socket对象和请求的客户端进行通信
* (6)通信结束后,关闭Socket对象
************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace server
{
class Program
{
static void Main(string[] args)
{
int port = 2000;
string host = "192.168.0.104";
IPAddress ip = IPAddress.Parse(host );//把IP地址字符串转换为IPAdderess类型
IPEndPoint ipe = new IPEndPoint(ip,port );//用IP和port初始化IPEndPoint类的新实例
//创建一个socket对像,如果用udp协议,则要用SocketType.Dgram类型的套接字
Socket s = new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
s.Bind(ipe );//为新建的Socket实例绑定IPEndPonit对象
s.Listen(0);//开始监听
L:
Console.WriteLine("等待客户端");
Socket temp = s.Accept();//为新建的连接创建Socket对象
Console.WriteLine("建立连接");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes ,recvBytes.Length ,0);//从客户端接收信息
recvStr += Encoding.ASCII.GetString(recvBytes ,0,bytes );//把byte[]转换成字符串
Console.WriteLine("server get message:{0}",recvStr );
string sendStr = "ok!Client send message successful";
byte[] bs = Encoding.ASCII.GetBytes(sendStr );//把字符串转换成byte[]
temp.Send(bs,bs.Length ,0);//返回信息给客户端
temp.Close();
goto L;
s.Close();
Console.ReadLine();
}
}
}
/***********************************************************************
* 客户端
* (1)建指定的端口和IP建立IPEndPoint对象。
* (2)建立Socket对象
* (3)用Socket对象的Connect()方法,以上面建立的IPEndPoint对象为参数,向服务器发送连接请求
* (4)若连接服务器成功,则用Socket对象的Send方法向服务器发送数据
* (5)用Socket对象的Receive()方法接收服务器返回的信息
* (6)关闭Socket
************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace client
{
class Program
{
static void Main(string[] args)
{
try
{ Send:
int port = 2000;
string host = "192.168.0.104";
IPAddress ip = IPAddress.Parse(host);//将Ip地址由字符串转换为IPAddress类型
IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的IP和端口初始化IPEndPoint类的新实例
//建立Socket对象
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);//连接服务器
string sendStr = "";
Console.WriteLine("请输入要发送的信息:");
sendStr = Console.ReadLine();
byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字符串转换为byte类型
Console.WriteLine("Send Message");
c.Send(bs, bs.Length, 0);//向服务器发送信息
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);//接收服务器返回的信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("Clinet get message:{0}", recvStr);
Console.ReadLine();
c.Close();
goto Send;
}
catch (ArgumentNullException e)
{
Console.WriteLine("argumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException:{0}", e);
}
Console.WriteLine("Press Enter to Exit");
}
}
}