1.从原始聊天程序开始

 Socket原义为插座,socket非常类似于电话插座。
我们所见到的一切网络通讯程序基本上都基于socket来建设。
比如HTTP使用80端口 FTP使用的是21端口,ORACLE等数据库都有端口,
他们最基本的原理就是socket
 
在Internet上有很多这样的主机,这些主机一般运行了多个服务软件,同时提供几种服务。每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务。Socket正如其英文原意那样,象一个多孔插座。一台主机犹如布满各种插座的房间,每个插座有一个编号,有的插座提供220伏交流电, 有的提供110伏交流电,有的则提供有线电视节目。 客户软件将插头插到不同编号的插座,就可以得到不同的服务。
所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄。应用程序通常通过"套接字"向网络发出请求或者应答网络请求。
 
IPAddress 提供网际协议IP地址
TCPLISTENER 从TCP网络客户端监听连接(主要用来描述客户端的一些特性?)
IPENDPOINT 将网络端点表示为IP地址和端口号(设置监听端口号)
NETWORKSTREAM 提供用于网络访问的基础数据流
 
SOCKET
 
ContractedBlock.gifExpandedBlockStart.gif服务器端
Int32 port = 5555;
            IPAddress ip 
= IPAddress.Parse("127.0.0.1");

            IPEndPoint point 
= new IPEndPoint(ip, port);

            Socket server 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            server.Bind(point);
            server.Listen(
10);

            
byte[] byteReceive = new byte[256];
            
byte[] byteSend = null;
            
string strReceive = null;
            
string strSend = null;
            Console.WriteLine(
"info : Wait for a Connection");
            Socket client 
= server.Accept();//获得一个客户端请求,理解为用这个socket对象保持于该请求者通信
            
ExpandedBlockStart.gifContractedBlock.gif            
while (client.Receive(byteReceive, byteReceive.Length, 0> 0{
                strReceive 
= System.Text.Encoding.UTF8.GetString(byteReceive);
                Console.WriteLine(
"info : 得到一个客户端连接");
                
if (strReceive.IndexOf('\0'>= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    strReceive 
= strReceive.Remove(strReceive.IndexOf('\0'));
                }

                Console.WriteLine(
"info : 内容是 -- {0}",strReceive);

                Console.WriteLine(
"info :  请输入回复信息");
                strSend 
= Console.ReadLine();
                byteSend 
= System.Text.Encoding.UTF8.GetBytes(strSend);
                client.Send(byteSend);
//使用保持请求者信息的socket对象与客户机通信
                Console.WriteLine("info :  发送回复成功");
                Array.Clear(byteReceive, 
0, byteReceive.Length);
            }
 
 
 
ContractedBlock.gifExpandedBlockStart.gif服务器端
Int32 port = 5555;
            IPAddress address 
= IPAddress.Parse("127.0.0.1");

            IPEndPoint point 
= new IPEndPoint(address, port);

            Socket client 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(point);

            Console.WriteLine(
"info :  连接成功");

            
byte[] byteReceive = new byte[256];
            
byte[] byteSend = null;
            
string strReceive = null;
            
string strSend = null;
            Console.WriteLine(
"info :  输入发送给服务器端的信息");
            strSend 
= Console.ReadLine();
            byteSend 
= System.Text.Encoding.UTF8.GetBytes(strSend);

            client.Send(byteSend);

ExpandedBlockStart.gifContractedBlock.gif            
while (client.Receive(byteReceive, byteReceive.Length, 0> 0{
                strReceive 
= System.Text.Encoding.UTF8.GetString(byteReceive);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (strReceive.IndexOf("/0"> 0{
                    strReceive 
= strReceive.Remove(strReceive.IndexOf("/0"));
                }

                Console.WriteLine(
"info :  服务器端回馈的消息是-- {0}", strReceive);

                Console.WriteLine(
"info :  输入回送给服务器端的信息");
                Array.Clear(byteReceive, 
0, byteReceive.Length);
                strSend 
= Console.ReadLine();
                byteSend 
= System.Text.Encoding.UTF8.GetBytes(strSend);
                client.Send(byteSend);
                Console.WriteLine(
"info :  回送给服务器成功");
            }
 
 

转载于:https://www.cnblogs.com/jianjialin/archive/2009/01/09/1372857.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值