• .net Socket 通信简单实例(初级入门)


    c/s控制台应用程序,Server、Client分别在两个项目中

    服务端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    
    namespace SocketServer
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    int port = 4000;
                    string host = "127.0.0.1";
                    IPAddress ipa = IPAddress.Parse(host);//将IP字符串转换为IP地址的实例
                    IPEndPoint ipe = new IPEndPoint(ipa, port);//将网络端点表示为ip地址和端口号
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket类
                    s.Bind(ipe);//绑定端口号
                    s.Listen(0);//开始监听
                    Console.WriteLine("wait for connect");
                    Socket tmp = s.Accept();//为新连接创建新的socket
                    Console.WriteLine("get a connect");
                    string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = tmp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接收信息
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    Console.WriteLine("get message:{0}", recvStr);//把从客户端传来的信息显示出来
                    string sendStr = "Yeah! Client send message sucessful";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    tmp.Send(bs, bs.Length, 0);//返回客户端成功信息
                    tmp.Close();
                    s.Close();
    
                }
                catch (ArgumentNullException ex)
                {
                    Console.WriteLine(ex);
    
                }
                catch (SocketException ex)
                { Console.WriteLine(ex); }
    
                Console.WriteLine("press enter to exit");
                Console.ReadLine();
            }
        }
    }
    View Code

    客户端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    namespace SocketClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    int port = 4000;
                    string host = "127.0.0.1";
                    IPAddress ipa = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ipa, port);//把ip和端口转化为ipendpoint实例
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket
                    Console.WriteLine("connecting.....");
                    s.Connect(ipe);//连接到服务器
                    string sendStr = "Hello! Socket Test";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    Console.WriteLine("SendMessage");
                    s.Send(bs, bs.Length, 0);//发送测试信息
                    string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = s.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接收返回信息
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    Console.WriteLine("ClientGetMessage:{0}", recvStr);//显示服务器返回信息
                    s.Close();
                }
                catch (ArgumentNullException ex)
                { Console.WriteLine(ex); }
                catch (SocketException ex)
                { Console.WriteLine(ex); }
                Console.WriteLine("press enter to exit");
                Console.ReadLine();
            }
    
        }
    }
    View Code

    效果图 :

    注意事项:先运行server,在运行client.(怎么发布大家都知道的)

    源码:http://download.csdn.net/detail/long_3160221/6929581

    .net技术交流群:70895254

  • 相关阅读:
    clear ,refresh,free
    记录一次vxworks下使用NFS组件的过程
    [dart学习]第七篇:类(构造函数)
    [dart学习]第六篇:流程控制语句
    [沉痛哀悼宁滨院士]
    [dart学习]第五篇:操作符
    [dart学习]第四篇:函数
    [dart学习]第三篇:dart变量介绍 (二)
    [dart学习]第二篇:dart变量介绍 (一)
    [dart学习]第一篇:windows下安装配置dart编译环境,写出helloworld
  • 原文地址:https://www.cnblogs.com/fly12300/p/3552698.html
Copyright © 2020-2023  润新知