代码改变世界

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

2007-09-19 09:29  Virus-BeautyCode  阅读(1147)  评论(0编辑  收藏  举报
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();
        }
    }
}