我必须构建一个服务器 - 客户端程序,客户端发送2个向量(任何大小)和服务器执行点产品操作并将结果作为对客户端的响应发送。
示例:
Client:
V1= [5, 3,-1]
V2= [1, 2, 3]
Server:
Calculate the dot product as (1*5+2*3+3*-1) =8
Sends 8 to the client
这是处理客户端请求的服务器类 -
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author Ehab Safaa
*/
public class server {
public static void main (String args []) throws IOException{
int a[] = null;
int b[] = null;
int temp1;
int temp2;
int n = a.length;
ServerSocket s1 = new ServerSocket (1342);
Socket ss =s1.accept();
Scanner sc1= new Scanner(ss.getInputStream());
for (int i = 0; i < a.length; i++) {
a[i]=sc1.nextInt();}
Scanner sc2= new Scanner(ss.getInputStream());
for(int i=0 ;i < b.length; i++)
b[i]=sc2.nextInt();
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[n] * b[n];
}
PrintStream p =new PrintStream(ss.getOutputStream());
}
}
这是向服务器发送请求的客户端类 -
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author Ehab Safaa
*/
public class client {
public static void main (String args [] ) throws IOException{
int a[] = null ;
int b[] = null;
int temp1 ;
int temp2;
int n = a.length;
Scanner sc =new Scanner(System.in);
Socket s =new Socket("127.0.0.1",1342);
Scanner sc1= new Scanner(System.in);
System.out.println("enter the first array");
for (int i = 0; i < a.length; i++) {
a[i]=sc1.nextInt();}
Scanner sc2= new Scanner(System.in);
System.out.println("enter the second array");
for (int i = 0; i < b.length; i++) {
b[i]=sc1.nextInt();
}
PrintStream p = new PrintStream(s.getOutputStream());
p.println(a);
PrintStream p1 = new PrintStream(s.getOutputStream());
p1.println(b);
temp1=sc1.nextInt();
System.out.println(temp1);
temp2=sc2.nextInt();
System.out.println(temp2);
}
}
任何帮助将不胜感激。
- 您想允许任何大小的向量 ,您使用客户端中的数组无法正常工作,因为
a.length
在输入数组之前无法反映长度。您可以改用ArrayList
。当然,需要读取数量以外的元素的停止条件;您可以在输入行中使用findInLine()
和一个零宽的阳性lookahead寻找另一个整数。 - 您可以通过提前发送向量长度来允许服务器使用数组。
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class server
{
public static void main (String args []) throws IOException
{
ServerSocket s1 = new ServerSocket(1342);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream()).useDelimiter("[\[,\s\]]+");
int n = sc.nextInt(); // get length sent from client
int a[] = new int[n];
int b[] = new int[n];
for (int i = 0; i < a.length; i++) a[i] = sc.nextInt();
for (int i = 0; i < b.length; i++) b[i] = sc.nextInt();
int sum = 0;
for (int i = 0; i < a.length; i++) sum += a[i] * b[i];
PrintStream p = new PrintStream(ss.getOutputStream());
p.print(sum);
}
}
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.ArrayList;
public class client
{
public static void main (String args [] ) throws IOException
{
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
Socket s = new Socket("127.0.0.1", 1342);
System.out.println("enter the first array");
while (sc.findInLine("(?=[-+\d])") != null) a.add(sc.nextInt());
sc.nextLine();
System.out.println("enter the second array");
while (sc.findInLine("(?=[-+\d])") != null) b.add(sc.nextInt());
PrintStream p = new PrintStream(s.getOutputStream());
p.println(a.size()); // make it easier for the server
p.println(a);
p.println(b);
System.out.println((new Scanner(s.getInputStream())).nextInt());
}
}