[英]Beginner Java Socket Programming Errors
我開始編寫我的第一個Java網絡程序,長話短說我很難確保我采取正確的方法。 我們的教授給了我們一個服務器程序來測試這個UDP客戶端,但是我遇到了一些我似乎無法壓制的錯誤。 具體來說,我得到IO異常,“連接被拒絕”或“無路由到主機”異常。
public class Lab2Client {
/**
* @param args[1] == server name, args[2] == server port, args[3] == myport
*/
public static void main(String[] args) {
//Serverport is set to 10085, our client is 10086
try {
Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));
System.out.println("Server connection Completed\n");
DataOutputStream output = new DataOutputStream(echoSocket.getOutputStream());
byte[] toSend = new byte[5];
toSend[0] = 12; toSend[1] = 34;//Code Number
toSend[2] = 15;//GroupId
toSend[3] = 86;toSend[4] = 100;//Port number in Little Endian Order
output.write(toSend);
System.out.println("Sent Request. Waiting for reply...\n");
DataInputStream input = new DataInputStream(echoSocket.getInputStream());
byte[] toRecieve = new byte[]{0,0,0,0,0,0,0,0};
input.read(toRecieve);
checkMessage(toRecieve);
}
catch (UnknownHostException e) {
System.err.println("Servername Incorrect!");
System.exit(1);
}
catch (IOException e){
System.err.println("IO Exception. Exiting...");
System.err.println(e);
System.exit(1);
}
}
關於用Java接收消息的實現,我也有一些問題。 我將獲得一個包含以下任一項的數據報:
a) 3個格式化字節(對問題不重要)以及IP和端口號
要么
b) 3個格式化字節和一個端口。
使用DataInputStream正確的方法嗎? 我知道使用一個包含9個元素的數組是懶惰的,而不是動態分配一個5或9的數組,但是現在我只是想讓它工作。 話雖這么說,有沒有人會為此建議一種不同的方法?
您不需要使用DataOutputStream
包裝Socket.getOuputStream()
返回的流 - 它已經是DataOutputStream
在這一行:
Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));
我想它應該是args [1],而不是args [0]。
在這里,您必須將整數值轉換為其字節表示:
toSend[3] = 10086 & 0xFF;toSend[4] = 10086>>8; //Port number in Little Endian Order
回答你的問題:案例b,因為你沒有發送IP
以為我會為后人留下這個。 問題很簡單,我很快就沒注意到它。
我正在測試的正確程序使用UDP協議,這個程序是用TCP編寫的。 更正的代碼是:
public class Lab2Client {
/**
* @param args[0] == server name, args[1] == server port, args[2] == myport
*/
public static void main(String[] args) {
//Serverport is 10085, our client is 10086
try {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(args[0]);
int portToSend = Integer.parseInt(args[2]);
System.out.println("Clent Socket Created");
byte[] toSend = new byte[5];
toSend[0] = 0x12; toSend[1] = 0x34;//Code Number
toSend[2] = 15;//GroupId, f in hex
toSend[3] = 0x27;toSend[4] = 0x66;
System.out.println("Byte Array Constructed");
DatagramPacket sendPacket = new DatagramPacket(toSend, toSend.length, IPAddress, Integer.parseInt(args[1]));
clientSocket.send(sendPacket);
System.out.println("Sent Request. Waiting for reply...\n");
DataInputStream input = new DataInputStream(echoSocket.getInputStream());
toRecieve can either be an error message, a return of what we sent,
or a byte stream full of IP info and port numbers.
the "heavy" byte stream is either 4 for IPv4 of 16 for IPv6, 2 bytes for port,
and the magic number (2 bytes) for a total of 9-20 bytes*/
byte[] toRecieve = new byte[9];
DatagramPacket receivePacket = new DatagramPacket(toRecieve, toRecieve.length);
clientSocket.receive(receivePacket);
checkMessage(toRecieve);
} //and so on and so forth...
感謝@Serge的幫助,雖然沒人能按我的要求正確回答我的問題。 你建議的字節轉移也很重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.