Skip to content

Instantly share code, notes, and snippets.

@NusZzz
Last active January 4, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NusZzz/8687055 to your computer and use it in GitHub Desktop.
Save NusZzz/8687055 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.net.*;
public class Client{
public static void main(String[] args) {
String hostname = "localhost";
int port = 5555;
Socket clientSocket = null;
DataOutputStream out = null;
BufferedReader in = null;
try {
clientSocket = new Socket(hostname, port);
out = new DataOutputStream(clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Cannot connect");
} catch (IOException e) {
System.err.println("IOException");
}
if (clientSocket == null || out == null || in == null) {
System.err.println( "Somethisg in wrong. One variable in null." );
return;
}
try {
while (true) {
System.out.print("Enter an integer: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String keyboardInput = br.readLine();
out.writeBytes(keyboardInput + "\n" );
out.flush();
String responseFromServer = in.readLine();
System.out.println("Server: " + responseFromServer);
}
} catch (UnknownHostException e) {
System.err.println("Cannot connect");
} catch (IOException e) {
System.err.println("IOException");
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String args[]) {
int port = 5555;
Server server = new Server(port);
server.startServer();
}
ServerSocket echoserver = null;
Socket clientSocket = null;
int numOfConnection = 0;
int port;
public Server(int port) {
this.port = port;
}
public void stopServer() {
System.out.println( "Server cleaning up." );
System.exit(0);
}
public void startServer() {
try {
echoserver = new ServerSocket(port);
}catch (IOException e) {
System.out.println(e);
}
Scanner input = new Scanner(System.in);
System.out.print("Enter the secret number: ");
int secret = input.nextInt();
System.out.println("Secret in "+ secret);
System.out.println("Let's begin the game!");
while (true) {
try {
clientSocket = echoserver.accept();
numOfConnection ++;
ServerConnection oneconnection = new ServerConnection(clientSocket, numOfConnection, secret);
new Thread(oneconnection).start();
}
catch (IOException e) {
System.out.println(e);
}
}
}
}
class ServerConnection implements Runnable {
BufferedReader in;
PrintStream out;
Socket clientSocket;
Server server;
int clientcount,secret;
public ServerConnection(Socket clientSocket, int clientcount, int secret) {
this.clientSocket = clientSocket;
this.clientcount = clientcount;
this.server = server;
this.secret = secret;
System.out.println( "Player #" + clientcount + " Accepted from " + clientSocket.getRemoteSocketAddress().toString() );
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintStream(clientSocket.getOutputStream());
}catch (IOException e) {
System.out.println(e);
}
}
public void run() {
String readLineFromClient;
try {
while (true) {
readLineFromClient = in.readLine();
int input = Integer.parseInt(readLineFromClient);
System.out.println( "Received from Player #" + clientcount + ": " + input);
String reply = null;
if(input>secret){
reply = "Too Much";
out.println("" + reply);
out.flush();
}else if(input<secret){
reply = "Too less";
out.println("" + reply);
out.flush();
}else{
reply = "You WIN WIN WIN WIN";
out.println("" + reply);
out.flush();
System.out.println("Player #"+ clientcount + " WIN WIN WIN WIN WIN.");
clientSocket.close();
System.exit(0);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
try{
if(clientSocket != null) {
clientSocket.close();
System.out.println("xxxx Connection CLOSED" + " for Client #" + clientcount);
}
}catch (IOException ex) {
System.out.println("Server IOException.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment