Created
May 11, 2016 14:10
-
-
Save dante-byte/f95678ae406641972ed2809d61ad4cca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tiy.networking; | |
/** | |
* Created by Donta White on 4/29/2016. | |
*/ | |
public class Message { | |
public String userName; | |
public String userMessage; | |
public String timeStamp; | |
public Message (String clientName, String clientMessage){ | |
userName = clientName; | |
userMessage = clientMessage; | |
} | |
@Override | |
public String toString(){ | |
return (userName + ":" + userMessage); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tiy.networking; | |
import java.net.Socket; | |
/** | |
* Created by Donta White on 4/27/2016. | |
*/ | |
public class SampleThread implements Runnable { | |
Socket clientSocket; | |
SimpleServer chatServer; | |
public void run() { | |
System.out.println("Running " + Thread.currentThread().getId()); | |
try { | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
System.out.println("Done running " + Thread.currentThread().getId()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tiy.networking; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
/** | |
* Created by Donta White on 4/27/2016. | |
*/ | |
public class ServerThread implements Runnable { | |
public Socket clientSocket; | |
public ServerThread(Socket clientSocket){ | |
this.clientSocket = clientSocket; | |
} | |
public void run(){ | |
System.out.println("Running " + Thread.currentThread().getId()); | |
try { | |
System.out.println("Incoming connection from " + clientSocket.getInetAddress().getHostAddress()); | |
BufferedReader inputFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
PrintWriter outputToClient = new PrintWriter(clientSocket.getOutputStream(), true); | |
String inputLine; | |
while ((inputLine = inputFromClient.readLine()) != null) { | |
Message myMessage = new Message(clientSocket.toString(), inputLine); | |
System.out.println("Received message: " + inputLine + " from " + clientSocket.toString()); | |
outputToClient.println("Message received loud and clear"); | |
} | |
System.out.println(); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
//System.out.println("Done running " + Thread.currentThread().getId()); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tiy.networking; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
import java.util.Scanner; | |
/** | |
* Created by Donta White on 4/27/2016. | |
*/ | |
public class SimpleClient { | |
public static void main(String[] args) throws Exception{ | |
Socket clientSocket = new Socket("localhost", 8005); | |
while (true){ | |
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); | |
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
Scanner lineScanner = new Scanner(System.in); | |
System.out.println(">"); | |
String message = lineScanner.nextLine(); | |
if (message.equals("exit")) { | |
clientSocket.close(); | |
//System.exit(0); | |
} | |
out.println(message); | |
if (message.equals("history")) { | |
String serverResponse; | |
while ((serverResponse = input.readLine()) != null) { | |
System.out.println("server:" + serverResponse); | |
if (serverResponse.equals("Message received loud and clear")) | |
System.out.println(serverResponse); | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tiy.networking; | |
import sun.java2d.pipe.SpanShapeRenderer; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** | |
* Created by Donta White on 4/27/2016. | |
*/ | |
public class SimpleServer { | |
//public ArrayList<SpanShapeRenderer.Simple> | |
public static void main(String[] args) throws Exception{ | |
new SimpleServer().startServer(); | |
} | |
public void startServer() throws Exception{ | |
ServerSocket serverListener = new ServerSocket(8005); | |
while (true) { | |
try { | |
Socket clientSocket = serverListener.accept(); | |
ServerThread myThread = new ServerThread(clientSocket); | |
Thread setThread = new Thread(myThread); | |
setThread.start(); | |
} catch (Exception exception) { | |
System.out.println("client closed connecion "); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tiy.networking; | |
import java.text.DecimalFormat; | |
import java.time.Instant; | |
/** | |
* Created by Donta White on 4/27/2016. | |
*/ | |
public class ThreadRunner { | |
public static void main(String[] args) { | |
System.out.println("ThreadRunner running"); | |
int numThreadsStarted = 0; | |
DecimalFormat timerFormatter = new DecimalFormat("###,###"); | |
long startMillis = Instant.now().toEpochMilli(); | |
while (true) { | |
System.out.println("Number of threads started = " + numThreadsStarted); | |
SampleThread localThread = new SampleThread(); | |
//localThread.run(); | |
Thread newThread = new Thread(localThread); | |
newThread.start(); | |
numThreadsStarted++; | |
if (numThreadsStarted > 10) { | |
break; | |
} | |
} | |
long endMillis = Instant.now().toEpochMilli(); | |
System.out.println("Ran in " + timerFormatter.format(endMillis - startMillis) + " ms"); | |
System.out.println("ThreadRunner done!"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment