Java套接字服务器与VB.NET客户端?

3

这真的可能吗?我有一个在Mac上运行的Java服务器程序,需要与Windows程序进行通信。我已经用Java编写了客户端,但似乎无法弄清如何在VB.net中使其工作...

以下是Java代码...

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class socketClient {

public static void main(String[] args) {
    /**
     * Define a host server
     */
    String host = "10.1.1.194";
    /**
     * Define a port
     */
    int port = 19999;

    StringBuffer instr = new StringBuffer();
    String TimeStamp;
    System.out.println("SocketClient initialized");

    try {

         //Obtain an address object of the server
        InetAddress address = InetAddress.getByName(host);


        //Establish a socket connection
        Socket connection = new Socket(address, port);

        //Instantiate a BufferedOutputStream object  
        BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());

        /**
         * Instantiate an OutputStreamWriter object with the optional
         * character encoding.
         */
        OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

        TimeStamp = new java.util.Date().toString();
        String process = "Initiate file transfer on " + host + " port " + port
                + " at " + TimeStamp + (char) 13;

        //Write across the socket connection and flush the buffer
        osw.write(process);
        osw.flush();


        /**
         * Instantiate a BufferedInputStream object for reading /**
         * Instantiate a BufferedInputStream object for reading incoming
         * socket streams.
         */
        BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
        /**
         * Instantiate an InputStreamReader with the optional character
         * encoding.
         */
        InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");


        //Read the socket's InputStream and append to a StringBuffer
        int c;
        while ((c = isr.read()) != 13) {
            instr.append((char) c);
        }

        //Close the socket connection.
        connection.close();
        System.out.println(instr);
    } 
    catch (IOException f) 
    {
        System.out.println("IOException: " + f);
    } 
    catch (Exception g) 
    {
        System.out.println("Exception: " + g);
    }
}
}

这是我目前为止的VB.NET代码...

Private clientSocket As Socket
Private host As String = "10.1.1.194"
Private port As Integer = 19999

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Try


        Dim add As New IPEndPoint(IPAddress.Parse(host), port)

        clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

        clientSocket.Connect(add)

        Dim netStream As New NetworkStream(clientSocket, True)



        Dim outstream As Byte() = System.Text.Encoding.ASCII.GetBytes("Initiate file transfer from " _
        & System.Environment.MachineName & " on " & host & " port " & port & " at " & TimeOfDay)


        netStream.Write(outstream, 0, outstream.Length)
        netStream.Flush()

    Catch ex As Exception
        Debug.Write(ex.Message)
    End Try


    End Sub

现在,如果我运行VB代码,它会挂起... 在我的Mac控制台上没有任何内容出现(就像我使用Java客户端时一样),然而,当我关闭VB代码时,我会得到java.net.SocketException:连接重置错误,所以我必须接近解决这个问题了。
问题是,当涉及到套接字编程时,我几乎一无所知,因此,如果有人能指导我正确的方向,我将不胜感激!
1个回答

2

这真的可能吗?

是的。

在您的VB.NET示例中,我没有看到您在任何地方编写回车(13)字符,但是您希望它出现在Java端,以便打印内容到控制台。


哇,我真笨。甚至没有看到那个。完美地工作了,谢谢! - Calvin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接