私はJavaプログラミングが初めてで、単純なサーバー(VB.NET)/クライアント(Java)プログラムを作成しました。Java からのテキストは VB.Net に正常に送信されますが、VB.Net からの応答は Java で受信されません。
何か不足していますか?
これが私のコードです
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);
}
}