i created a socket based client server application ... this is what it basically has to do .... Define a socket-based client server application. The server must be able to respond to multiple clients simultaneously. The server generates a single positive integer value N, to be used for each client that connects. When connecting, the client randomly generates an integer M. The client then sends positive integer values to server M. The transmission ended by sending the value -1. Each time a client finishes transmitting, the server writes to the screen the number of values greater than N received so far from all clients. If necessary, manage synchronization issues.
This is what I managed to do .... I have a doubt though .. When I run the client it tells me how many positive integers it sends, for example 40 .... The server will then tell me how many of these are greater than the number N generated by the client, for example 10 .... But if I run the client again , this will add the previous integers greater than N with the new number of integers greater than N giving me an incorrect result. Now I wonder ... is this normal or did I do something wrong?
package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(5555);
int N;
N = (int)(Math.random()*100);
System.out.println("Server online, il numero N che adopera è : "+N);
Accumulatore a = new Accumulatore();
while(true){
Socket client = s.accept();
ThreadServer t = new ThreadServer(client,N,a);
t.start();
}
// TODO code application logic here
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
// TODO code application logic here
}
}
}
package server;
import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ThreadServer extends Thread{
Socket s ;
int M;
Accumulatore acc;
public ThreadServer(Socket s, int M, Accumulatore acc) {
this.s = s;
this.M = M;
this.acc = acc;
}
@Override
public void run(){
try {
DataInputStream data = new DataInputStream(new BufferedInputStream(s.getInputStream()));
int n;
while((n = data.readInt())!=-1){
if(n>M){
acc.incrementa();
}
}
System.out.println("Valori più grandi di "+M +" ricevuti : "+ acc.getn());
data.close(); //////diocaneeeeeeeee
s.close();
} catch (IOException ex) {
Logger.getLogger(ThreadServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
package client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client {
public static void main(String[] args) {
try(Socket client = new Socket("localhost",5555);
OutputStream i1 = client.getOutputStream();
DataOutputStream out = new DataOutputStream(i1);) {
int M;
M= (int)(Math.random()*100);
System.out.println("Invierò "+M+" interi positivi");
// TODO code application logic here
for(int i = 0; i<M; i++){
int l;
l = (int)(Math.random()*100);
out.writeInt(l);
out.flush();
}
out.writeInt(-1);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
package server;
public class Accumulatore {
private int n = 0;
synchronized public void incrementa(){
n++;
}
public int getn(){
return n;
}
}
As @Armali wrote in the comment - it depends on the application requirement whether it is wrong or not. The application logic has one instance of the classs Accumulatore
. This instance is created before any connection from client and sum up all comming connection. The server uses the same Accumulatore
instance when a new connection is created.
If the numbers should be counted separately for each connection you need to create a separate Accumulatore
instance when a new connection is received in the main
method. Something like that:
...
N = (int)(Math.random()*100);
System.out.println("Server online, il numero N che adopera è : "+N);
while(true) {
Socket client = s.accept();
Accumulatore a = new Accumulatore();
ThreadServer t = new ThreadServer(client,N,a);
t.start();
...