构建一个套接字程序,客户端发送2个向量,服务器执行点积并将结果作为响应发送给客户端

问题描述 投票:0回答:1

我必须构建一个服务器-客户端程序,其中客户端发送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);
       }
    }     

任何帮助将不胜感激。

java sockets serversocket
1个回答
0
投票
  • 由于要允许任何大小]的向量,您在客户端中使用数组的方法不起作用,因为a.length无法反映输入数组之前的长度。您可以改用ArrayList。当然,需要一个停止条件来读取除元素编号以外的其他元素;您可以使用findInLine()和零宽度正向查找在输入行中查找另一个整数。
  • 您可以通过预先向服务器发送向量长度来使服务器使用数组。
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());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.