简体   繁体   English

使用VB.NET客户端的Java套接字服务器?

[英]Java socket server with VB.NET client?

Is this even possible? 这甚至可能吗? I have a java server program on a mac that I need to use to communicate back to a windows program. 我在mac上有一个java服务器程序,我需要用它来与Windows程序进行通信。 I have the client working in Java, but I can't seem to figure out how to get it to work in VB.net... 我让客户端使用Java,但我似乎无法弄清楚如何让它在VB.net中运行...

Here is the Java code... 这是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);
    }
}
}

And here is my VB.NET code that I have so far... 这是我到目前为止的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

Now, if I run the vb code, it hangs... and nothing appears in my console on the mac (Like it does when I use the java client), however, when I close the VB code, I get the java.net.SocketException: Connection reset error, so I must be getting pretty close to figuring this out. 现在,如果我运行vb代码,它会挂起......我的控制台上没有任何东西出现在Mac上(就像我使用java客户端时那样),但是,当我关闭VB代码时,我得到了java.net .SocketException:连接重置错误,所以我必须非常接近解决这个问题。

The thing is, I know almost nothing when it comes to socket programming, so if someone could push me in the right direction, it would be very appreciated! 问题是,我对套接字编程几乎一无所知,所以如果有人能够把我推向正确的方向,那将非常感激!

Is this even possible? 这甚至可能吗?

Yes. 是。

I don't see you writing Carriage Return (13) character anywhere in your VB.NET example, yet you expect it to appear on a Java side in order to print something to the console. 我没有看到你在VB.NET示例中的任何地方编写Carriage Return(13)字符,但是你希望它出现在Java端以便向控制台打印一些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM