Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Java SSL套接字服务器输入流不正确_C#_Java_Sockets_Ssl - Fatal编程技术网

C# Java SSL套接字服务器输入流不正确

C# Java SSL套接字服务器输入流不正确,c#,java,sockets,ssl,C#,Java,Sockets,Ssl,正在尝试编写Java服务器C#客户端。面临无法从流中正确读取和写入Java数据的问题。只能手动操作。有没有办法提高阅读和写作能力 爪哇 C# 我不知道您所说的“不可能”或“手动”是什么意思,但当以任何语言从TCP套接字读取时,必须注意读取API返回的计数。你不能假设它填满了缓冲区。只需要读取一个或多个字节 public static void main(String[] args) throws Exception { System.out.println("Opening server

正在尝试编写Java服务器C#客户端。面临无法从流中正确读取和写入Java数据的问题。只能手动操作。有没有办法提高阅读和写作能力

爪哇

C#


我不知道您所说的“不可能”或“手动”是什么意思,但当以任何语言从TCP套接字读取时,必须注意读取API返回的计数。你不能假设它填满了缓冲区。只需要读取一个或多个字节

public static void main(String[] args) throws Exception {
    System.out.println("Opening server socket...");
    System.out.println(System.currentTimeMillis());
    char[] passphrase = "password".toCharArray();
    System.out.println"---------------------");
    System.out.println(java.security.KeyStore.getDefaultType());
    try
    {
        KeyStore keystore1 = KeyStore.getInstance("jks");
        FileInputStream fis = new FileInputStream("mykeystore.jks");
        keystore1.load(fis, passphrase);
        fis.close();
        KeyStore ksTrust = KeyStore.getInstance("jks");
        FileInputStream fis2 =  new FileInputStream("mykeystore.jks");
        ksTrust.load(fis2, passphrase);
        KeyManagerFactory kmf =    KeyManagerFactory.getInstance("SunX509");
        kmf.init(keystore1, passphrase);
        TrustManagerFactory tmf =   TrustManagerFactory.getInstance("SunX509");
        tmf.init(ksTrust);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        SSLServerSocketFactory ssf = 
        (SSLServerSocketFactory) sslContext.getServerSocketFactory();
        SSLServerSocket ss = 
        (SSLServerSocket) ssf.createServerSocket(PORT_WORK);

        while (true) {
            SSLSocket s = (SSLSocket) ss.accept();
            s.startHandshake();
            s.setTcpNoDelay(true);

            byte[]b = new byte[2048];
            s.getInputStream().read(b);
            System.out.println(new String(b).trim());

            s.getOutputStream().write("Test".getBytes("UTF-8"));

            b = new byte[2048];
            s.getInputStream().read(b);
            System.out.println(new String(b).trim());   

            s.close();
            System.out.println("-------||END||-------");
        }
    } catch (Exception e) {
        System.out.println("Cannot open\r\n" + e);
    }
}
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
    if (sslPolicyErrors != SslPolicyErrors.None)
    Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
    return true;
}    

static void Main(string[] args)
{
    TcpClient client = new TcpClient() { SendTimeout = 1000, ReceiveTimeout = 1000 };
    IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4446);

    client.Connect(serverEndPoint);
    client.NoDelay = true;
    Console.WriteLine("Client connected.");

    SslStream sslStream = new SslStream(client.GetStream(), 
false,ValidateServerCertificate, null);
    try
    {
        sslStream.AuthenticateAsClient("Test");
        try
        {
            Console.WriteLine("---------------------");
            char[] c = new char[2048];
            StreamReader sr = new StreamReader(sslStream);
            StreamWriter sw = new StreamWriter(sslStream);
            sw.WriteLine("Hardly one"); sw.Flush();
            Console.WriteLine(new string(c, 0, sr.Read(c, 0, c.Length)));
            sw.WriteLine("Simple two"); sw.Flush();
        }
        catch (Exception e){Console.WriteLine(e.Message);}
    }
    catch (Exception error)
    {
        Console.WriteLine("Exception: {0}", error.Message);
        if (error.InnerException != null)
        {
            Console.WriteLine("Inner exception: {0}", error.InnerException.Message);
        }
        Console.WriteLine("Authentication failed - closing the connection.");
    }                                                                        
    client.Close();
    Console.WriteLine("---------------------");
    Console.WriteLine("Client disconnected.");
    Console.ReadKey();
}