C/S 模型

应用层:

  保持应用程序之间建立连接所需要的数据记录,为用户服务。

表示层:处理通信信号的表示方法,进行不同的格式之间的翻译,并负责数据的加密与解密,数据的压缩与恢复。

会话层:主要负责两个会话进程之间的通信,即两个会话层实体之间的信息交换,管理数据的交换。

传输层:最关键的一层,向用户提供可靠的端到端服务,它屏蔽了下层的数据通信细节,让用户及应用程序不需要考虑实际的通信方法。

网络层:主要负责路由,选择合适的路径,进行阻塞控制等功能。

数据链路层:在两个主机上建立数据链路连接,向物理层传输数据信号,并对信号进行处理使之无差错并合理的传输。

物理层:定义网络的物理结构,传输的电磁标准,Bit流的编码及网络的事件原则,例如分时复用和分频复用,决定了网络连接类型及物理拓扑结构。简单地说这一层主要负责实际的信号传输。

 

 

 

 客户端代码:Client

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SocketSend
{
class Program
{
static void Main(string[] args)
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip
= IPAddress.Parse(host);
IPEndPoint ipe
= new IPEndPoint(ip, port);
Socket c
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine(
"连接到服务器");
c.Connect(ipe);
string sendStr = "This is a client";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine(
"发送:");
Console.WriteLine(sendStr);
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(
"收到:");
Console.WriteLine(recvStr);
c.Close();

}
catch (ArgumentNullException e)
{
Console.WriteLine(
"ArgumentNullException:{0}", e);
}
catch(SocketException e)
{
Console.WriteLine(
"SocketException:{0}", e);
}
Console.ReadLine();
}
}
}

 

 

 

 服务器端:Server
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SocketReceive
{
class Program
{
static void Main(string[] args)
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip
= IPAddress.Parse(host);
IPEndPoint ipe
= new IPEndPoint(ip, port);
Socket s
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
Console.WriteLine(
"正在监听....");
s.Listen(
0);
Socket temp
= s.Accept();
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);
Console.WriteLine(
"收到 :");
Console.WriteLine(recvStr);
string sendStr = "I have received message from client";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine(
"发送");
Console.WriteLine(sendStr);
temp.Send(bs, bs.Length,
0);
temp.Close();
s.Close();

}
catch (ArgumentNullException e)
{
Console.WriteLine(
"ArgumentNullException:{0}", e);
}
catch (SocketException e )
{
Console.WriteLine(
"SocketException:{0}", e);

}
Console.ReadLine();
}
}
}

 相关模型如下网址:

 

 https://files.cnblogs.com/tianjinquan/Client%e2%80%94Server..pdf

 

 

 TcpClient

 

代码
static void Main(string[] args)
{
IPAddress myIP
=IPAddress.Parse("127.0.0.1");
TcpClient tcp
= new TcpClient();
tcp.Connect(myIP,
4800);
NetworkStream ns
= tcp.GetStream();
byte[] by = new Byte[30];
string send = "";
while(true)
{
Console.Write(
"请输入要传输的字符串:");
send
= Console.ReadLine();
by
= System.Text.Encoding.BigEndianUnicode.GetBytes(send);
ns.Write(by,
0, by.Length);
}
ns.Close();
tcp.Close();
}

 

 

 TcpServer

 

代码
static void Main(string[] args)
{
IPAddress myIP
= IPAddress.Parse("127.0.0.1");
TcpListener s
= new TcpListener(myIP, 4800);
s.Start();
Console.WriteLine(
"监听连接");
TcpClient tcp
= s.AcceptTcpClient();
NetworkStream ns
= tcp.GetStream();
while (true)
{
byte[] by = new Byte[30];
string received = "";
ns.Read(by,
0, by.Length);
received
= System.Text.Encoding.BigEndianUnicode.GetString(by);
Console.WriteLine(
"收到数据" + received.Trim());
}
ns.Close();
tcp.Close();
}

 

 

 

posted @ 2010-10-23 21:53  Space Tian  阅读(1044)  评论(0编辑  收藏  举报