I need to send and receive data between a client and a server. The server will be managing multiple clients at once, so it needs to be able to send and receive data with little latency.
At the moment I can both send and receive data from the client to the server but it is done procedural, so if any data is lost the server will freeze until the client sends new data. The issue is that if the server is returning data then both the server and client will freeze, waiting for data from the other.
My current methods to receive and send data
public void run(){
while(true){
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("SERVER > " +new String(packet.getData()));
}
}
public void sendData(byte[] data){
DatagramPacket packet = new DatagramPacket(data, data.length, 6969);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
Ideally I would like something like ActionListener that can let me get an action whenever data is received. At the moment it will run a couple of ticks every time it receives data.
This is where the use of threads comes in. I'm not a big on java when it comes to coding but I think this will help you. This is the server side code
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(8000);
while(true) {
Socket socket = s.accept();
HandleClient c = new HandleClient(socket);
Thread t = new Thread(c);
t.start();
}
}
HandleClient code
public class HandleClient implements Runnable{
Socket socket;
HandleClient(Socket socket){
this.socket = socket;
}
public void run() {
try{
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
double radius;
while(true){
radius = inputFromClient.readDouble();
double area = radius * radius * Math.PI;
outputToClient.writeDouble(area);
}
}catch(Exception e) {}
}
}
And client side
public static void main(String[] args){
try{
//System.out.println(add);
Socket socket = new Socket("localhost",8000);
Scanner input = new Scanner(System.in);
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
double radius;
System.out.println("Client running at IP address: "+socket.getLocalAddress().toString() +"and port number: "+socket.getLocalPort());
System.out.println("Enter the value of radius");
radius = input.nextDouble();
System.out.println("Writing input");
outputToServer.writeDouble(radius);
System.out.println(inputFromServer.readDouble());
}catch (Exception E){}
}
It's not the same as what you are working on but it's just a template to use threads to handle requests coming from clients. You can modify this to make your project work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.