3
這甚至可能嗎?我有一個mac服務器程序,我需要用它來回傳給windows程序。我有客戶端在Java中的工作,但我似乎無法弄清楚如何讓它在VB.net工作...帶有VB.NET客戶端的Java套接字服務器?
這裏是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控制檯(像它的時候然而,當我關閉VB代碼時,我得到了java.net.SocketException:Connection reset錯誤,所以我必須非常接近地計算出t他出去了。
事情是,當談到套接字編程時,我幾乎什麼都不知道,所以如果有人能把我推向正確的方向,那將非常感激!
哇,我很笨。甚至沒有看到。完美工作,謝謝! – Calvin 2012-07-13 14:25:02