network-scoket

server:

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

namespace net_server
{
    class Program
    {
        static void Main(string[] args)
        {
            int get = 0;
            const int size = 512;
            Console.WriteLine("Server is Running...");
            IPAddress ip = new IPAddress(new byte []{127,0,0,1});
            //IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0]; //另一种获取ip的方法
            TcpListener listener = new TcpListener(ip, 8500);
            listener.Start();  
            Console.WriteLine("start listenering ...");
           
                TcpClient remoteclient = listener.AcceptTcpClient();//获取侦听,返回一个TcpClient
                Console.WriteLine("client connected!{0}<---{1}", remoteclient.Client.LocalEndPoint, remoteclient.Client.RemoteEndPoint);
           
                NetworkStream streamtoclient = remoteclient.GetStream();//得到客户端的数据流
              do
            {
                 byte[] buffer = new byte[size];
                 try
                 {
                     lock (streamtoclient)
                     {
                         get = streamtoclient.Read(buffer, 0, size);//获得长度,这个方法是同步的,只有客户端发送数据才会执行,否则便会一直等待
                     }
                     Console.WriteLine("get ,{0} byte", get);

                     //获得请求字符创
                     string msg = Encoding.Unicode.GetString(buffer, 0, size);
                     Console.WriteLine("read:{0}", msg);

                     msg = msg.ToUpper();
                     buffer = Encoding.Unicode.GetBytes(msg);
                     lock (streamtoclient)
                     {
                         streamtoclient.Write(buffer, 0, buffer.Length);
                     }
                     Console.WriteLine("send:{0}", msg);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.Message);
                     break;
                 }
            } while (true);
              streamtoclient.Dispose();
              remoteclient.Close();
            
            Console.WriteLine("\n\n输入\"Q\"键退出。");
            ConsoleKey key;
            do
            {
                key = Console.ReadKey(true).Key;
            } while (key != ConsoleKey.Q);
        }
    }
}

 

 

client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace net_client
{
    class Program
    {
        static void Main(string[] args)
        {
            const int size =  512;
            Console.WriteLine("Client is running...");
            TcpClient client ;
                try
                {
                    client = new TcpClient();
                    client.Connect("127.0.0.1", 8500);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
                Console.WriteLine("Server connected!{0}--->{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
                
                NetworkStream streamtoserver = client.GetStream();
                ConsoleKey keys;
                do
                {   
                    keys = Console.ReadKey (true).Key;
                    if (keys == ConsoleKey.A)//当按下A键可以开始输入发送内容
                    {
                        string msg = Console.ReadLine();
                        byte[] buffer = Encoding.Unicode.GetBytes(msg);
                        try
                        {
                            lock (streamtoserver)
                            {
                                streamtoserver.Write(buffer, 0, buffer.Length);
                            }
                            Console.WriteLine("send :{0}", msg);

                            int byteread = 0;
                            buffer = new byte[size];
                            lock(streamtoserver )
                            {
                                byteread = streamtoserver .Read (buffer ,0,size);
                            }
                           
                            msg = Encoding .Unicode.GetString (buffer ,0,byteread );
                            Console .WriteLine ("server con:{0}",msg );
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }while(keys!=ConsoleKey.E);
                streamtoserver.Dispose();
                client.Close();

                //Console.WriteLine("\n\n输入\"Q\"键退出。");
                //ConsoleKey key;
                //do
                //{
                //    key = Console.ReadKey(true).Key;
                //} while (key != ConsoleKey.Q);
            
        }
    }
}

 

posted @ 2013-06-25 19:54  南修子  阅读(198)  评论(0编辑  收藏  举报