• [原创]socket,c#,.net,dns,client,server,console Virus


    client

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

    namespace ConsoleSocketClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                byte[] data=new byte[1024];
                string input;
                string stringData;

                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.43"), 8000);
                Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    serverSocket.Connect(serverEndPoint);
                }
                catch (SocketException sockex)
                {
                    Console.WriteLine("Can not connect to server.\n"+sockex.Message);
                    return;
                }
                int rece = serverSocket.Receive(data);
                stringData = Encoding.ASCII.GetString(data,0,rece);
                Console.WriteLine(stringData);
                while (true)
                {
                    input = Console.ReadLine();
                    if (input == "exit")
                    {
                        break;
                    }
                    serverSocket.Send(Encoding.ASCII.GetBytes(input));
                    data = new byte[1024];
                    rece = serverSocket.Receive(data);
                    stringData = Encoding.ASCII.GetString(data);
                    Console.WriteLine(stringData);

                }
                Console.WriteLine("Disconnected from server, the connection will disconnected in ten second.....");
                //System.Threading.Thread.Sleep(10000);
                for (int i = 0; i < 10; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    Console.WriteLine(i.ToString());
                }
                    serverSocket.Shutdown(SocketShutdown.Both);
                serverSocket.Close();
            }
        }
    }

    //.................................................................................

    server:


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

    namespace ConsoleSocketServer
    {
        class Program
        {
            static void Main(string[] args)
            {
                int rece;
                byte[] data = new byte[1024];
                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8000);
                //IPEndPoint ipPoint = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], 8000);
                Socket serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                serverSock.Bind(ipEndPoint);
                serverSock.Listen(5);
                Console.WriteLine("Waiting for a client.............");
                Socket clientSock = serverSock.Accept();
                IPEndPoint clientEndPoint =(IPEndPoint) clientSock.RemoteEndPoint;
                Console.WriteLine("Connected with {0} at port {1}", clientEndPoint.Address, clientEndPoint.Port);

                string welcome = "Welcome to my test server.";
                data = System.Text.Encoding.ASCII.GetBytes(welcome);
                clientSock.Send(data,data.Length,SocketFlags.None);
                while (true)
                {
                    data = new byte[1024];
                    rece = clientSock.Receive(data);
                    if (rece == 0)
                    {
                        break;
                    }
                    Console.WriteLine(Encoding.ASCII.GetString(data,0,rece));
                    clientSock.Send(data, rece, SocketFlags.None);
                }
                Console.WriteLine("Disconnected from {0}", clientEndPoint.Address);
                clientSock.Close();
                serverSock.Close();
            }
        }
    }





    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    安装mysql_cluster报错: Data::Dumper丢失
    win10 nodejs指定ionic版本安装(npm方式)
    java项目报错: org.springframework.beans.factory.BeanCreationException找不到mapper.xml文件
    java项目跑起来报错: 程序报 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 错误
    ~/.bashrc文件写错, 导致Linux全部命令丢失
    tomcat热启动没问题, 访问项目报500解决办法
    安装OpenOffice
    redhat6.4 elasticsearch1.7.3安装配置
    MySQL新建用户保存的时报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    zabbix zabbix_agentd.conf详解
  • 原文地址:https://www.cnblogs.com/virusswb/p/898007.html
Copyright © 2020-2023  润新知