java與C# Socket的區別、他們之間的通信

先看java代碼

java服務器端:

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class 服務器 {

    public static void main(String[] args) {
        try {
            //在本機創建一個服務器 端口號爲61666
            ServerSocket soo=new ServerSocket(61666);
            System.out.println("java創建服務器成功...");
            Socket so=soo.accept();//等待連接 連接成功才能往下執行
            InputStream in=so.getInputStream();//獲取socket的輸入流
            byte[]by=new byte[1024];
            while(true) {
                int len=in.read(by);
                System.out.println("java服務器收到消息 長度+" + len);
                System.out.println(new String(by,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

java客戶端:

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class 客戶端 {

    public static void main(String[] args) {
        try {
            //第一個參數的服務器的ip 第二個參數是端口號
            Socket so=new Socket("127.0.0.1",61666);
            System.out.println("java連接服務器成功...");
            OutputStream out=so.getOutputStream();//獲取socket的輸出流
            Scanner in=new Scanner(System.in);
            while(true) {
                String s=in.nextLine();
                out.write(s.getBytes());
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

這裏寫圖片描述
這裏寫圖片描述

再看C#的代碼

C#這裏寫了兩個方法 這裏只講第二個方法
C#服務器端:

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

namespace 服務器
{
    class Program
    {
        static void 方法1() {
            Console.WriteLine("方法1");
            //第一個參數是指定socket對象使用的尋址方案,即IPV4或IPV6;
            //第二個參數socket對象的套接字的類型,此處stream是表示流式套接字
            //第三個參數socket對象支持的協議,TCP協議或UDP協議
            Socket soo = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//參數這樣寫就好了

            IPAddress ip = IPAddress.Parse("127.0.0.1");//本機ip作爲服務器

            IPEndPoint ipend = new IPEndPoint(ip, 61666);//將網絡終結點表示爲 IP 地址和端口號

            soo.Bind(ipend);//用socket對像的Bind()方法綁定IPEndPoint 綁定主機端口號

            //上面這一段相當於java的ServerSocket soo=new ServerSocket(61666);
            //soo.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 61666));

            soo.Listen(10);//掛起的連接隊列的最大長度。貌似可以不寫。不是最大連接數


            Console.WriteLine("C#創建服務器成功...");


            Socket so = soo.Accept();//等待用戶連接 和java一樣
            byte[] by = new byte[1024];
            while (true)
            {
                int len = so.Receive(by);//Send發送 Receive接收
                Console.WriteLine("C#服務器收到消息 長度+" + len);
                Console.WriteLine(Encoding.Default.GetString(by, 0, len));
            }
        }
        static void 方法2()
        {
            Console.WriteLine("方法2");
            TcpListener listener = new TcpListener(61666);//已過時
            //TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 61666);

            listener.Start();//啓動服務器
            //上面這一段相當於java的ServerSocket soo=new ServerSocket(61666);

            Console.WriteLine("C#創建服務器成功...");

            Socket so = listener.AcceptSocket();//等待用戶連接 和java一毛一樣
            NetworkStream io = new NetworkStream(so);
            byte[] by = new byte[1024];
            while (true)
            {
                //接收一組byte 存儲到字節數組by裏
                //從by[0]處開始存儲該數據 要讀取1024個字節
                //len爲實際讀取到的字節數
                int len = io.Read(by,0,10);

                Console.WriteLine("C#服務器收到消息 長度+" + len);
                Console.WriteLine(Encoding.Default.GetString(by, 0, len));
            }
        }
        static void Main(string[] args)
        {
            方法2();
        }
    }
}

C#客戶端:

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

namespace 客戶端
{
    class Program
    {
        static void 方法1() {
            Console.WriteLine("方法1");
            Socket so = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            so.Connect("127.0.0.1", 61666);
            //相當於java的 Socket so=new Socket("127.0.0.1",61666);

            //so.Connect(IPAddress.Parse("127.0.0.1"), 61666);//也行
            Console.WriteLine("C#連接服務器成功...");
            while (true)
            {
                string s = Console.ReadLine();
                byte[] by = Encoding.Default.GetBytes(s);
                so.Send(by);//Send發送 Receive接收
            }
        }
        static void 方法2() {
            Console.WriteLine("方法2");
            TcpClient client = new TcpClient("127.0.0.1", 61666);//相當於java的 Socket so=new Socket("127.0.0.1",61666);
            NetworkStream io = client.GetStream();
            Console.WriteLine("C#連接服務器成功...");
            while (true)
            {
                string s = Console.ReadLine();
                byte[] by = Encoding.Default.GetBytes(s);//把字符串s轉換成字節數組
                io.Write(by, 0, by.Length);
                io.Flush();
            }
        }
        static void Main(string[] args)
        {
            方法2();
        }
    }
}

這裏寫圖片描述

他們之間也能通信

直接運行就行了 代碼不變
C#服務器 java客戶端
這裏寫圖片描述
java服務器 C#客戶端
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章