[英]Communication between server (ANSI C) and client (JAVA) using sockets
[英]Communication between Client and Server using Sockets
好的,這是今天早些時候修訂的問題,我提供了有助於解釋該問題的代碼。 我正在從客戶端向服務器發送兩條消息。 然后,服務器接收消息並進行處理。 服務器最終嘗試將消息發送回客戶端(請注意服務器代碼“ testmessage”),這是我遇到的問題。 我不是在客戶端接收消息,還是從服務器端錯誤地發送消息。
public class ClientConnection {
String address, language, message;
int portNumber;
Socket clientSocket = null;
public ClientConnection(String lan, String mes, String add, int pn) throws IOException{
address = add;
portNumber = pn;
language = lan;
message = mes;
}
public String createAndSend() throws IOException{
// Create and connect the socket
Socket clientSocket = null;
clientSocket = new Socket(address, portNumber);
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Send first message - Message is being correctly received
pw.write(language+"\n");
pw.flush();
// Send off the data
// Send the second message - Message is being correctly received
pw.write(message);
pw.flush();
pw.close();
// Send off the data
// NOTE: Either I am not receiving the message correctly or I am not sending it from the server properly.
String translatedMessage = br.readLine();
br.close();
//Log.d("application_name",translatedMessage); Trying to check the contents begin returned from the server.
return translatedMessage;
}
服務器代碼:
public class ServerConnection {
public static void main(String[] args) throws Exception {
// Delete - Using while loop to keep connection open permanently.
boolean status = false;
while( !status){
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
// Delete - Working as of here, connection is established and program runs awaiting connection on 4444
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String language = br.readLine();
String message = br.readLine();
// Test - Works
System.out.println(language);
// Test - Works
System.out.println(message);
// Delete - Working as of here, both messages are passed and applied. Messages are received as sent from client.
TranslateMessage tm = new TranslateMessage();
String translatedMessage = tm.translateMessage(language, message);
// NOTE: This seems to be where I am going wrong, either I am not sending the message correctly or I am not receiving it correctly..
// PrintWriter writer = new PrintWriter(new BufferedOutputStream(clientSocket.getOutputStream()));
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true);
// Send translation back
System.out.println(translatedMessage);
// pw.write(translatedMessage+"\n");
pw.write("Return test"); // Test message!
pw.flush();
// Send off the data
pw.close();
br.close();
clientSocket.close();
serverSocket.close();
}
}
}
代碼有點混亂,我可以看到一些重復的地方,我在出現問題的地方發表了評論。
謝謝你的幫助!
您正在使用BufferedReader.readLine()從服務器讀取響應,但是在測試用例中,您發送的字符串未以\\ n或\\ r \\ n終止,因此它將無法獲得該行。我可以從文檔中看出...
public String readLine()
throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
另一個建議...
在編寫這樣的請求響應協議時,我不會依靠行尾來終止請求或響應。 通常,我會使用完全格式化的JSON字符串,或者我的首選是二進制協議,其中所有請求和響應都以二進制計數為前綴(通常為4字節,bigendian /網絡字節順序)。 然后,客戶端和服務器讀取這4個字節,然后讀取隨后的字節數。 這可以處理通常在網絡連接上發生的數據包碎片,還有助於避免惡意用戶發送永不終止的長字符串而引起的DOS攻擊。
在Java中,可以使用ByteBuffer.order()處理雙端數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.