• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests.net;
18 
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 
23 import com.badlogic.gdx.ApplicationAdapter;
24 import com.badlogic.gdx.Gdx;
25 import com.badlogic.gdx.Net.Protocol;
26 import com.badlogic.gdx.net.ServerSocket;
27 import com.badlogic.gdx.net.ServerSocketHints;
28 import com.badlogic.gdx.net.Socket;
29 import com.badlogic.gdx.net.SocketHints;
30 import com.badlogic.gdx.tests.utils.GdxTest;
31 
32 /** Demonstrates how to do very simple socket programming. Implements a classic PING-PONG sequence, client connects to server,
33  * sends message, server sends message back to client. Both client and server run locally. We quit as soon as the client received
34  * the PONG message from the server. This example won't work in HTML. Messages are delimited by the new line character, so we can
35  * use a {@link BufferedReader}.
36  * @author badlogic */
37 public class PingPongSocketExample extends GdxTest {
38 	@Override
create()39 	public void create () {
40 		// setup a server thread where we wait for incoming connections
41 		// to the server
42 		new Thread(new Runnable() {
43 			@Override
44 			public void run () {
45 				ServerSocketHints hints = new ServerSocketHints();
46 				ServerSocket server = Gdx.net.newServerSocket(Protocol.TCP, "localhost", 9999, hints);
47 				// wait for the next client connection
48 				Socket client = server.accept(null);
49 				// read message and send it back
50 				try {
51 					String message = new BufferedReader(new InputStreamReader(client.getInputStream())).readLine();
52 					Gdx.app.log("PingPongSocketExample", "got client message: " + message);
53 					client.getOutputStream().write("PONG\n".getBytes());
54 				} catch (IOException e) {
55 					Gdx.app.log("PingPongSocketExample", "an error occured", e);
56 				}
57 			}
58 		}).start();
59 
60 		// create the client send a message, then wait for the
61 		// server to reply
62 		SocketHints hints = new SocketHints();
63 		Socket client = Gdx.net.newClientSocket(Protocol.TCP, "localhost", 9999, hints);
64 		try {
65 			client.getOutputStream().write("PING\n".getBytes());
66 			String response = new BufferedReader(new InputStreamReader(client.getInputStream())).readLine();
67 			Gdx.app.log("PingPongSocketExample", "got server message: " + response);
68 		} catch (IOException e) {
69 			Gdx.app.log("PingPongSocketExample", "an error occured", e);
70 		}
71 	}
72 }
73