client1.java
import java.io.*;
import java.net.*;
class client1 {
public static void main (String [] args) {
Socket connectToServer;
OutputStream out;
BufferedReader in;
String msg;
String result;
try {
connectToServer = new Socket ("localhost", 5555);
out = connectToServer.getOutputStream ( );
in = new BufferedReader (new InputStreamReader (connectToServer.getInputStream ( )));
msg = "sum" + "\n";
out.write (msg.getBytes ( ), 0, msg.length ( ));
msg = "5" + "\n";
out.write (msg.getBytes ( ), 0, msg.length ( ));
result = in.readLine ( );
System.out.println (result);
connectToServer.close ( );
} catch (Exception ex) {System.out.println (ex);}
}
}
server1.java
import java.io.*;
import java.net.*;
class server1 {
public static void main (String [] args) {
ServerSocket c;
Socket cs;
OutputStream out;
BufferedReader in;
String msg;
String name;
int parameter;
int result=0;
try {
c = new ServerSocket (5555);
cs = c.accept ( );
out = cs.getOutputStream ( );
in = new BufferedReader (new InputStreamReader (cs.getInputStream ( )));
name = in.readLine( );
parameter = Integer.parseInt (in.readLine ( ));
result = name(parameter);
out.write (result.getBytes ( ), 0, result.length ( ));
} catch (Exception ex) {System.out.println (ex);}
}
static int sum (int n)
{
return (n + 5);
}
}
我想从服务器调用 sum 方法,并且我从服务器除外来支持函数的结果,但是有一些错误我真的很累需要一些帮助我的朋友
对不起,我的英语不好