• [原创]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

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

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

  • 相关阅读:
    mongodb基础命令
    mongodb集合的增删
    mongodb的创建删除数据库
    单机版mongodb
    《TCP/IP 详解 卷一》读书笔记-----Ping&Traceroute
    《TCP/IP 详解 卷一》读书笔记 -----第四章 ARP
    《TCP/IP详解 卷一》读书笔记-----第三章 IP
    输入三个整数,xyz,最终以从小到大的方式输出。利用中间变量
    循环语句
    3.输入三个整数,xyz,最终以从小到大的方式输出。利用嵌套。
  • 原文地址:https://www.cnblogs.com/virusswb/p/898007.html
Copyright © 2020-2023  润新知