Server

import java.io.*;

import java.net.*;

import java.math.*;

class ptb2_server

{

public static void main(String[] args) throws IOException

{

DatagramSocket ServerSocket=new DatagramSocket(6789);

while(true)

{

byte [] receiveData=new byte [1024];

DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);

ServerSocket.receive(receivePacket);

double a=Double.parseDouble(new String(receivePacket.getData()).trim());

System.out.print("a:"+a);

ServerSocket.receive(receivePacket);

double b=Double.parseDouble(new String(receivePacket.getData()).trim());

System.out.print("b:"+b);

ServerSocket.receive(receivePacket);

double c=Double.parseDouble(new String(receivePacket.getData()).trim());

System.out.print("c:"+c);

InetAddress IPAddress=receivePacket.getAddress();

int port=receivePacket.getPort();

String st=ptb2(a,b,c);

byte[] sendData=new byte [1024];

sendData=st.getBytes();

DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,port);

ServerSocket.send(sendPacket);

}

}

public static String ptb2(double a,double b,double c)

{

double delta=b*b-4*a*c;

if(delta<0)

return "Phuong trinh vo nghiem";

else

if(delta==0)

{

double x=(double)-b/(2*a);

return "PT co nghiem kep x1=x2="+x;

}

else

{

double x1=(-b-Math.sqrt(delta))/(2*a);

double x2=(-b+Math.sqrt(delta))/(2*a);

return "PT co 2 nghiem phan biet x1="+x1+";x2="+x2;

}

}

}

Bạn đang đọc truyện trên: TruyenTop.Vip

Tags: #hieu