package ch.epfl.lca.sc250; import javax.swing.JOptionPane; import java.io.*; import java.net.*; /** * @author Christophe Trefois * */ public class ServentServer { public static void main(String[] args) { ServentServer s = new ServentServer(); } public ServentServer ( ) { // Open a Socket and listen on incoming offer requests. // try { boolean result; String message; ServerSocket knockSock; Socket sock; int targetport = 13371; BufferedReader inReader; DataOutputStream outStream; knockSock = new ServerSocket(targetport); while ( true ) { sock = knockSock.accept(); outStream = new DataOutputStream(sock.getOutputStream()); inReader = new BufferedReader(new InputStreamReader(sock.getInputStream())); message = inReader.readLine(); String[] parsed = message.split(", "); // We suppose we got and parsed an offer, we then want the pop-up to display result = getOfferResult(sock.getInetAddress().toString(), parsed[0], parsed[1]); if(result) { System.out.println("Yes"); outStream.writeBytes("Yes\n"); } else { System.out.println("No"); outStream.writeBytes("No\n"); } sock.close(); } } catch ( IOException e ) { System.out.println("IO-Error"); e.printStackTrace(); } } /** * When an offer is received, display a Confirm Dialog where the user can choose Yes or No * @param IP The IP we got the offer from * @param amount The amount the other player gives us for a letter * @param position The position of the letter he wants to buy * @return True or False */ public static boolean getOfferResult(String IP, String amount, String position) { boolean chosenValue = false; String msgToDisplay = "Buyer from IP: " + IP + "\nWants to buy Letter at Position " + position + " for " + amount + "$"; int returnValue = JOptionPane.showConfirmDialog(null, msgToDisplay, "You got an Offer !", JOptionPane.YES_NO_OPTION); if(returnValue == JOptionPane.OK_OPTION) { chosenValue = true; } else { chosenValue = false; } return chosenValue; } }