Example usage for java.net SocketException SocketException

List of usage examples for java.net SocketException SocketException

Introduction

In this page you can find the example usage for java.net SocketException SocketException.

Prototype

public SocketException() 

Source Link

Document

Constructs a new SocketException with no detail message.

Usage

From source file:ste.xtest.net.BugFreeStubURLConnectionBehaviour.java

@Test
public void throw_a_network_error() throws Exception {
    StubURLConnection C = new StubURLConnection(new URL(TEST_URL_DUMMY));
    IOException e = new UnknownHostException("a.host.com");
    then(C.error(e)).isSameAs(C);/*from  w  w  w .j a va2  s .  c  om*/

    try {
        C.connect();
        fail("error not thrown");
    } catch (IOException x) {
        then(x).isSameAs(e);
    }

    e = new SocketException();
    then(C.error(e)).isSameAs(C);

    try {
        C.connect();
        fail("error not thrown");
    } catch (IOException x) {
        then(x).isSameAs(e);
    }
}

From source file:com.predic8.membrane.core.transport.http.HttpEndpointListener.java

void setIdleStatus(Socket socket, boolean isIdle) throws IOException {
    if (isIdle) {
        if (closed) {
            socket.close();//from  w  ww . j  a v a2 s  . com
            throw new SocketException();
        }
        idleSockets.put(socket, Boolean.TRUE);
    } else {
        idleSockets.remove(socket);
    }
}

From source file:client.ReceivingData.java

@Override
public void run() {

    String result;//  www  .j  a va2  s  .  c o  m
    JSONArray jsonMainArr;
    JSONObject childJSONObject;

    try {
        while (GlobalVariables.daemon) {

            result = in.readLine();
            System.out.println("[DEBUG] Got: " + result);

            jsonMainArr = new JSONArray(result);

            String action = jsonMainArr.getJSONObject(0).getString("action");

            switch (action) {

            case "bet_change":
                final String nick = jsonMainArr.getJSONObject(1).getString("player");
                final int bet = jsonMainArr.getJSONObject(1).getInt("bet");
                final int chips = jsonMainArr.getJSONObject(1).getInt("chips");

                for (final Player player : GlobalVariables.players) {
                    if (player.getNick().equals(nick)) {
                        player.setBet(bet);
                        player.setChips(chips);
                    }
                }

                break;

            case "first_connection":
                GlobalVariables.startChips = jsonMainArr.getJSONObject(1).getInt("chips");
                GlobalVariables.smallBlind = jsonMainArr.getJSONObject(2).getInt("chips");
                GlobalVariables.bigBlind = jsonMainArr.getJSONObject(3).getInt("chips");
                ((MainPanel) GlobalVariables.panel).gameData();
                break;

            case "cards":

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    childJSONObject = jsonMainArr.getJSONObject(i);
                    final String rank = childJSONObject.getString("rank");
                    final String suit = childJSONObject.getString("suit");
                    GlobalVariables.hand[i - 1] = new Card(suit, rank);
                }

                GlobalVariables.panel.repaint();
                break;

            case "exchange_cards":

                GlobalVariables.hand = new Card[4];

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    childJSONObject = jsonMainArr.getJSONObject(i);
                    final String rank = childJSONObject.getString("rank");
                    final String suit = childJSONObject.getString("suit");
                    GlobalVariables.hand[i - 1] = new Card(suit, rank);
                }

                GlobalVariables.panel.repaint();
                break;

            case "players_info":

                ((MainPanel) GlobalVariables.panel).clean();

                final JLabel[] playerPositions = ((MainPanel) GlobalVariables.panel)
                        .getPlayerPositions(jsonMainArr.length() - 1);
                String[] otherPlayers = new String[jsonMainArr.length() - 1];
                Integer[] chipsArray = new Integer[jsonMainArr.length() - 1];
                String dealerPlayer = "";

                GlobalVariables.players = new ArrayList<>();

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    otherPlayers[i - 1] = jsonMainArr.getJSONObject(i).getString("player");
                    chipsArray[i - 1] = jsonMainArr.getJSONObject(i).getInt("chips");

                    if (jsonMainArr.getJSONObject(i).getBoolean("dealer")) {
                        dealerPlayer = otherPlayers[i - 1];
                    }
                }

                while (!(otherPlayers[0].equals(GlobalVariables.getMyNick()))) {
                    Helpers.shiftRight(otherPlayers, 1);
                    Helpers.shiftRight(chipsArray, 1);
                }

                for (int i = 0; i < jsonMainArr.length() - 1; i++) {
                    if (dealerPlayer.equals(otherPlayers[i])) {
                        GlobalVariables.players
                                .add(new Player(playerPositions[i], otherPlayers[i], chipsArray[i], true));
                    } else {
                        GlobalVariables.players
                                .add(new Player(playerPositions[i], otherPlayers[i], chipsArray[i], false));
                    }
                }

                GlobalVariables.me = GlobalVariables.players.get(0);

                (GlobalVariables.buttonPanel).hideAll();

                break;

            case "available_choices":

                (GlobalVariables.buttonPanel).hideAll();

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    childJSONObject = jsonMainArr.getJSONObject(i);
                    final String choice = childJSONObject.getString("choice");
                    GlobalVariables.buttonPanel.showButton(choice);
                }

                break;

            case "pot":
                GlobalVariables.pot = jsonMainArr.getJSONObject(1).getInt("pot_size");
                GlobalVariables.panel.repaint();
                break;

            case "game_already_started":
                new MyDialog("Game already started").setVisible(true);
                throw new SocketException();

            case "you_lost":
                new MyDialog("You Lost").setVisible(true);
                throw new SocketException();

            case "you_won":
                new MyDialog("You Won").setVisible(true);
                throw new SocketException();

            case "the_same_nick":
                new MyDialog("Please choose another nick").setVisible(true);
                throw new SocketException();

            case "message":
                final String message = jsonMainArr.getJSONObject(1).getString("message");
                new MyDialog(message).setVisible(true);
                break;

            case "connection_successful":
                new MyDialog("Connection successful").setVisible(true);
                break;

            case "show_cards":
                final String cards = jsonMainArr.getJSONObject(1).getString("cards");
                new MyDialog(cards).setVisible(true);
                break;

            default:
                System.out.println("[FATAL] Unknown command" + action);
            }

        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }

    new MyDialog("Lost connection with server").setVisible(true);
    GlobalVariables.daemon = false;
    GlobalVariables.connected = false;
    (GlobalVariables.buttonPanel).hideAll();

    for (final Player player : GlobalVariables.players) {
        player.setNick("");
    }

    GlobalVariables.bigBlind = 0;
    GlobalVariables.smallBlind = 0;
    GlobalVariables.me.setNick("ME");
    GlobalVariables.connect.setEnabled(true);
    GlobalVariables.chosen = new boolean[] { false, false, false, false };
    GlobalVariables.coordinates = new int[4][4];
    GlobalVariables.pot = 0;
    GlobalVariables.hand = new Card[4];
    GlobalVariables.panel.repaint();
    ((MainPanel) GlobalVariables.panel).gameData();

}

From source file:uk.me.sa.lightswitch.android.net.TestRequestMessage.java

@Test(expected = RemoteMessageException.class)
public void testRemoteSocketException() throws Exception {
    /*//  w w w  . j a v  a  2 s  . co  m
     * https://code.google.com/p/powermock/issues/detail?id=366
     * 
     * whenNew(DatagramSocket.class).withAnyArguments().thenThrow(new SocketException());
     */

    final AtomicBoolean workaroundPowerMockBug = new AtomicBoolean(true);
    whenNew(DatagramSocket.class).withAnyArguments().thenAnswer(new Answer<DatagramSocket>() {
        @Override
        public DatagramSocket answer(InvocationOnMock invocation) throws Throwable {
            if (workaroundPowerMockBug.get())
                return null;
            throw new SocketException();
        }
    });
    workaroundPowerMockBug.set(false);

    new RequestMessage("secret4", Light.LEFT).sendTo("test.node.invalid");
}