zoukankan      html  css  js  c++  java
  • 基于TCP Socket的简单网络通信

    本实例是一个基于TCP的简单通讯实例,分为服务器端和客户端,服务器端监听客户端的连接请求,客户端将信息发送给服务器端,而服务器端则回复客户端发送的信息以达到通讯测试:

    服务器端代码如下:

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

    namespace SimpleTcpServer
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //接收到的数据
                int receive;
                
    //定义数据缓冲区
                byte [] date=new byte[1024];
                
    //设置终端连接,接收任意请求的IP地址客户终端
                IPEndPoint MyIpEndpoint = new IPEndPoint(IPAddress.Any, 13000);
                
    //新建一个服务器套接字
                Socket MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                
    //绑定服务端套接字
                MySocket.Bind(MyIpEndpoint);
                
    //监听
                MySocket.Listen(10);
                Console.WriteLine(
    "Waitting for a message from client!");

                
    //获取客户端套接字
                Socket Client = MySocket.Accept();
                
    //获取客户端终点
                IPEndPoint ClientEndPoint =(IPEndPoint)Client.RemoteEndPoint;
                Console.WriteLine(
    "Connect with {0} port {1}",ClientEndPoint.Address,ClientEndPoint.Port.ToString());

                
    //将要发送到客户端的信息进行编码
                date = Encoding.ASCII.GetBytes("Welcome to my Server");
                
    //发送信息到客户端
                Client.Send(date, date.Length, SocketFlags.None);

                
    //如果接收的信息长度为0,则自动退出循环
                while (true)
                {
                    receive 
    = Client.Receive(date);
                    
    if (receive==0)
                    {
                        
    break;
                    }
                    
    //接收到的字符
                    string ReceiveString = Encoding.UTF8.GetString(date, 0, receive);
                    Console.WriteLine(ReceiveString);
                    
    //发送信息到客户端
                    Client.Send(date, receive, SocketFlags.None);
                }
                Console.WriteLine(
    "DisConnected with {0}", ClientEndPoint.Address);
                Client.Close();
                MySocket.Close();
            }
        }
    }

    客户端代码如下:

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

    namespace SimpleTcpClient
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //缓冲区大小
                byte[] data=new byte[1024];
                
    string Input, StringData;
                
    int receive;
                
    //设置连接终端
                IPEndPoint MyIpEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000);
                
    //设置连接服务器的套接字
                Socket MyServer=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                
    try
                {
                    
    //连接服务器
                    MyServer.Connect(MyIpEndpoint);
                }
                
    catch
                {
                    Console.WriteLine(
    "Fail to connect to the server!");
                    
    return;
                }
                
    //接收服务器端发来的信息
                receive = MyServer.Receive(data);
                
    //将发来的信息进行转码
                StringData = Encoding.UTF8.GetString(data, 0, receive);
                Console.WriteLine(StringData);

                
    while (true)
                {
                    
    //从客户端输入信息
                    Input = Console.ReadLine();
                    
    if (Input=="exit")
                    {
                        
    break;
                    }
                    
    //发送信息到服务端
                    MyServer.Send(Encoding.UTF8.GetBytes(Input));
                    
    //接收服务端的信息
                    receive = MyServer.Receive(data);
                    StringData 
    = Encoding.UTF8.GetString(data, 0, receive);
                    Console.WriteLine(
    "Server: {0}", StringData);
                }
                
    //释放资源
                Console.WriteLine("Disneccted from Server!");
                MyServer.Shutdown(SocketShutdown.Both);
                MyServer.Close();
            }
        }
    }
    本实例是基于TCP协议的同步网络通讯,后面我们将继续学习基于TCP协议的异步网络通讯。同步网络通讯的优点是简单和易于理解,在通讯量不大的情况下,同步通讯的效率要高于异步通讯,但是同步通讯的缺点也是显而易见的。
  • 相关阅读:
    logic:iterate用法教程
    js实现页面跳转的几种方式
    MyEclipse快捷键两篇文章
    hibernate.cfg.xml配置
    DispatchAction功能用法
    第一个Spring程序
    BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
    【Struts1.2总结】strutsconfig.xml配置
    java中equals和==的区别
    开发Struts程序的两篇文章
  • 原文地址:https://www.cnblogs.com/hanyonglu/p/2020691.html
Copyright © 2011-2022 走看看