首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Visual C# 2005 Socket 網路程式設計(三):同步/非同步模式、Client/Server 完整通訊範例

     編輯:關於C語言

Socket 兩種模式

  • 同步:Send/Receive 封鎖直到完成
  • 非同步:立即返回,稍後透過回調處理

完整通訊範例:Client 發送測試字串,Server 接收並回應

Client 端


// Client 端
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace socketsample
{
    class Class1
    {
        static void Main()
        {
            try
            {
                int port = 2000;
                string host = "127.0.0.1";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                Socket c = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                c.Connect(ipe);

                string sendStr = "hello!This is a socket test";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                c.Send(bs, bs.Length, 0);

                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes = c.Receive(recvBytes, recvBytes.Length, 0);
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine(recvStr);
                c.Close();
            }
            catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); }
            catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); }
            Console.ReadLine();
        }
    }
}

Server 端


// Server 端
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Project1
{
    class Class2
    {
        static void Main()
        {
            try
            {
                int port = 2000;
                string host = "127.0.0.1";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                Socket s = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                s.Bind(ipe);
                s.Listen(0);
                Socket temp = s.Accept();

                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine(recvStr);

                string sendStr = "Ok!Sucess!";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                temp.Send(bs, bs.Length, 0);
                temp.Shutdown(SocketShutdown.Both);
                temp.Close();
                s.Shutdown(SocketShutdown.Both);
                s.Close();
            }
            catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); }
            catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); }
            Console.ReadLine();
        }
    }
}

環境:VS Express 2005 .NET 2.0 測試通過。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved