Java 客户端-服务器程序中的静态方法没有正确分配它接收到的变量
Java static method in client-server program not assigning correctly variables it receives
我制作的客户端-服务器程序有问题。客户端向服务器发送一个字符串,服务器应该识别它是'+'、'-'、'*'还是'/'并将其发送回客户端。问题是我做了一个静态方法应该识别符号,但它不起作用。它没有给我编译错误,所以我不知道我做错了什么。
客户:
import java.net.*;
import java.io.*;
public class Client1{
public static void main(String[] args){
Socket client;
InetSocketAddress isa;
int letto;
int dim_buffer = 100;
byte[] buffer;
InputStream fromServer;
OutputStream toServer;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String dataToServer = "";
String dataFromServer = "";
try{
client = new Socket();
isa = new InetSocketAddress(InetAddress.getLocalHost(), Integer.parseInt(args[0]));
client.connect(isa);
do{
fromServer = client.getInputStream();
toServer = client.getOutputStream();
dataToServer = br.readLine();
toServer.write(dataToServer.getBytes(), 0, dataToServer.length());
buffer = new byte[dim_buffer];
letto = fromServer.read(buffer);
if(letto > 0){
dataFromServer = new String(buffer, 0, letto);
System.out.println("DATA FROM SERVER : " +dataFromServer);
}else
dataToServer = ".";
}while(!(dataToServer.equals(".")));
client.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
服务器
import java.net.*;
import java.io.*;
public class Server1{
public static void main(String[] args){
ServerSocket server;
Socket client;
InputStream fromClient;
OutputStream toClient;
int dim_buffer = 100;
byte[] buffer;
String dataToClient = "";
String dataFromClient = "";
int letto;
char operatore = '0';
try{
server = new ServerSocket(0);
System.out.println("Porta server " + server.getLocalPort());
while(true){
client = server.accept();
System.out.println("Connected with " + client.getInetAddress() + "; " + client.getPort());
do{
fromClient = client.getInputStream();
toClient = client.getOutputStream();
buffer = new byte[dim_buffer];
letto = fromClient.read(buffer);
if(letto > 0){
dataFromClient = new String(buffer, 0, letto);
System.out.println("DATO SENT FROM CLIENT: " + dataFromClient);
setOperatore(dataFromClient,operatore);
System.out.println("OP: " + Character.toString(operatore));
dataToClient = dataFromClient.toUpperCase();
toClient.write(dataToClient.getBytes(), 0, dataToClient.length());
}else
dataFromClient = ".";
}while(!(dataFromClient.equals(".")));
client.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
private static void setOperatore(String fromClientString, char operatore){
switch(fromClientString){
case "+":
operatore = fromClientString.charAt(0);
break;
case "-":
operatore = fromClientString.charAt(0);
break;
case "*":
operatore = fromClientString.charAt(0);
break;
case "/":
operatore = fromClientString.charAt(0);
break;
default:
break;
}
}
}
如果我为客户端打开一个终端,为服务器打开一个终端并在客户端终端上写“+”,这就是打印出来的内容。
服务器端:
java Server1
Porta server 45225
Connected with /127.0.0.1; 55342
DATO SENT FROM CLIENT: +
OP: 0
客户端:
java Client1 45225
+
DATA FROM SERVER : +
它应该打印
运营商:+
而是打印 OP: 0
你应该让你的静态方法 return 成为你想要的值。在 Java.
中无法从方法中填充变量
private static char getOperatore(String fromClientString){
char operatore;
switch(fromClientString){
case "+":
operatore = fromClientString.charAt(0);
break;
case "-":
operatore = fromClientString.charAt(0);
break;
case "*":
operatore = fromClientString.charAt(0);
break;
case "/":
operatore = fromClientString.charAt(0);
break;
default:
break;
}
return operatore;
}
我制作的客户端-服务器程序有问题。客户端向服务器发送一个字符串,服务器应该识别它是'+'、'-'、'*'还是'/'并将其发送回客户端。问题是我做了一个静态方法应该识别符号,但它不起作用。它没有给我编译错误,所以我不知道我做错了什么。
客户:
import java.net.*;
import java.io.*;
public class Client1{
public static void main(String[] args){
Socket client;
InetSocketAddress isa;
int letto;
int dim_buffer = 100;
byte[] buffer;
InputStream fromServer;
OutputStream toServer;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String dataToServer = "";
String dataFromServer = "";
try{
client = new Socket();
isa = new InetSocketAddress(InetAddress.getLocalHost(), Integer.parseInt(args[0]));
client.connect(isa);
do{
fromServer = client.getInputStream();
toServer = client.getOutputStream();
dataToServer = br.readLine();
toServer.write(dataToServer.getBytes(), 0, dataToServer.length());
buffer = new byte[dim_buffer];
letto = fromServer.read(buffer);
if(letto > 0){
dataFromServer = new String(buffer, 0, letto);
System.out.println("DATA FROM SERVER : " +dataFromServer);
}else
dataToServer = ".";
}while(!(dataToServer.equals(".")));
client.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
服务器
import java.net.*;
import java.io.*;
public class Server1{
public static void main(String[] args){
ServerSocket server;
Socket client;
InputStream fromClient;
OutputStream toClient;
int dim_buffer = 100;
byte[] buffer;
String dataToClient = "";
String dataFromClient = "";
int letto;
char operatore = '0';
try{
server = new ServerSocket(0);
System.out.println("Porta server " + server.getLocalPort());
while(true){
client = server.accept();
System.out.println("Connected with " + client.getInetAddress() + "; " + client.getPort());
do{
fromClient = client.getInputStream();
toClient = client.getOutputStream();
buffer = new byte[dim_buffer];
letto = fromClient.read(buffer);
if(letto > 0){
dataFromClient = new String(buffer, 0, letto);
System.out.println("DATO SENT FROM CLIENT: " + dataFromClient);
setOperatore(dataFromClient,operatore);
System.out.println("OP: " + Character.toString(operatore));
dataToClient = dataFromClient.toUpperCase();
toClient.write(dataToClient.getBytes(), 0, dataToClient.length());
}else
dataFromClient = ".";
}while(!(dataFromClient.equals(".")));
client.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
private static void setOperatore(String fromClientString, char operatore){
switch(fromClientString){
case "+":
operatore = fromClientString.charAt(0);
break;
case "-":
operatore = fromClientString.charAt(0);
break;
case "*":
operatore = fromClientString.charAt(0);
break;
case "/":
operatore = fromClientString.charAt(0);
break;
default:
break;
}
}
}
如果我为客户端打开一个终端,为服务器打开一个终端并在客户端终端上写“+”,这就是打印出来的内容。
服务器端:
java Server1
Porta server 45225
Connected with /127.0.0.1; 55342
DATO SENT FROM CLIENT: +
OP: 0
客户端:
java Client1 45225
+
DATA FROM SERVER : +
它应该打印 运营商:+ 而是打印 OP: 0
你应该让你的静态方法 return 成为你想要的值。在 Java.
中无法从方法中填充变量 private static char getOperatore(String fromClientString){
char operatore;
switch(fromClientString){
case "+":
operatore = fromClientString.charAt(0);
break;
case "-":
operatore = fromClientString.charAt(0);
break;
case "*":
operatore = fromClientString.charAt(0);
break;
case "/":
operatore = fromClientString.charAt(0);
break;
default:
break;
}
return operatore;
}