我是 Java 编程新手,我编写了一个简单的服务器(VB.NET)/客户端(Java)程序。来自 Java 的文本已成功发送到 VB.Net,但在 Java 中未收到来自 VB.Net 的响应
我错过了什么吗?
这是我的代码
VB.NET(服务器)
Imports System.Net.Sockets, System.Text
Public Class Form1
Dim server As New TcpListener(9999)
Dim client As New TcpClient
Dim stream As NetworkStream
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Waiting...."
server.Start()
client = server.AcceptTcpClient
'Receive msg'
stream = client.GetStream()
Dim r_byt(client.ReceiveBufferSize) As Byte
stream.Read(r_byt, 0, client.ReceiveBufferSize)
Dim str As String = Encoding.ASCII.GetString(r_byt)
Label1.Text = str
'Send msg'
Dim s_byt() As Byte = Encoding.UTF8.GetBytes("got it")
stream.Write(s_byt, 0, s_byt.Length)
stream.Close()
End Sub
End Class
Java(客户端)
import java.io.*;
import java.net.*;
public class frmClient {
public static void main(String[] args) throws Exception{
frmClient myCli = new frmClient();
myCli.run();
}
public void run() throws Exception{
Socket socket = new Socket("192.168.0.100", 9999);
PrintStream stream = new PrintStream(socket.getOutputStream());
stream.println("Hello Server...");
BufferedReader buffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String string = buffer.readLine();
System.out.println(string);
}
}