[英]Java Client-server communication issues (3rd day with Java)
我幾天前在大學開始學習Java(主題是“服務器編程”),我們開始編寫兩個代碼作為客戶端和服務器,因此客戶端向服務器發送了一個帶有3個數值的字符串,將不得不應用數學公式(我不記得什么公式,但這沒關系)然后將結果返回給客戶端,客戶端必須在屏幕上打印它或其他什么。
這是我的代碼。 我使用了老師教授的一些課程,如Socket和ServerSocket,但我知道代碼中有一些錯誤,我找不到原因。 它在客戶端聯系服務器並且服務器為其打開套接字后停止工作。
客戶代碼:
int capital = 300000;
int interes = 4;
int plazos = 360;
String IP_servidor = "127.0.0.1";
int puerto_servidor = 8801;
try{
// Connect with server
Socket servidor = new Socket (IP_servidor,puerto_servidor); // Se crea un socket para el servidor, con su IP para mandarle luego datos
//Server connected
//Preparing data
String datos = capital + ";" + interes + ";" + plazos;
//Data prepared
//Sending data
PrintWriter conexion = new PrintWriter(servidor.getOutputStream(),true); //Object "conexion" will be the one to which we'll write data the server will receive
conexion.println(datos);
//Data sent
// Server processes data and returns it
//Receiving data
Socket recepcion = new Socket (IP_servidor,puerto_servidor);
BufferedReader lector = new BufferedReader (new InputStreamReader (recepcion.getInputStream()));
String operacion = new String("");
operacion = lector.readLine();
//Data received back
// End
}
catch (IOException Err){
System.out.println("Error");
System.out.println(Err.getMessage());
}
和服務器的代碼:
int puerto_servidor = 8801;
try{
// Server starts and connects with client
ServerSocket miServidor = new ServerSocket(puerto_servidor);
System.out.println("Socket created");
Socket cliente = miServidor.accept();
System.out.println("Petition from " + cliente.getInetAddress() + " received...");
// Server started and client connected
//Receiving data
Socket cliente2 = new Socket (cliente.getInetAddress(),puerto_servidor); // Creating socket for client, so we can send data there after this
BufferedReader lector = new BufferedReader (new InputStreamReader (cliente2.getInputStream()));
String operacion = new String("");
operacion = lector.readLine(); //Copy from the client to a string to decode afterwards
System.out.println("Received string \"" + operacion + ".");
//Data received
//Preparing data
String[] datos;
datos = operacion.split(";"); //En 'datos' (0,1,2) se guardan separados los valores que se recibieron
capital = Integer.parseInt(datos[0]);
interes = Integer.parseInt(datos[1]);
plazos = Integer.parseInt(datos[2]);
System.out.println("String was cut to 3 pieces: \"" + capital + "\", \"" + interes + "\" y \"" + plazos + "\".");
int resultado = capital*interes*plazos;
//Data prepared
// Se devuelve el resultado al cliente
PrintWriter way_back = new PrintWriter(cliente.getOutputStream(),true);
way_back.println("Result is: " + resultado);
System.out.println("Result was returned");
// Se ha devuelto el resultado
// End
}
catch (IOException Err){
System.out.println("Error");
System.out.println(Err.getMessage());
}
(兩個代碼都在main函數內)另外,我知道服務器代碼中的System.out.println(...)只顯示在服務器中,而客戶端則相同。 我只是這樣做,所以我可以看到應用程序的進展:)
我希望你能幫助我。 我知道有一個錯誤(好吧......超過1哈哈)但我不確定在哪里,有2個單獨的項目/應用意味着2個代碼共享線路,我必須混合一些功能或其他什么,以及一個星期左右,老師將無法幫助我。 我可以等待,但我真的希望這可以讓你覺得有成就感,正如你可能理解的那樣。
我不是要求一個有效的代碼,而是一些方向,所以我可以自己修復代碼,就像我在課堂上一樣。
再見!
為什么要為server-> client comms創建一個新套接字?
使用accept()調用返回的套接字!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.