Commit eac30866 authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

fixed bugged; can recieve multiple clients;

parent 378651d7
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jonathan
*/
class Client implements Runnable {
private int ID;
private String threadName;
private Socket connectionSocket;
private Thread t;
private boolean active;
private String clientSentence;
private String capitalizedSentence;
public Client(int _id,Socket _client)
{
ID = _id;
connectionSocket = _client;
threadName = "Client-Listener-"+ID;
active = true;
System.out.println("Created thread " + threadName);
}
@Override
public void run() {
System.out.println("Running thread " + threadName);
while(active)
{
DataOutputStream outToClient = null;
try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
if (clientSentence != null){
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
}
else{
active = false;
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
outToClient.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("Thread " + threadName + " exiting...");
}
public void start(){
if (t == null)
{
t = new Thread(this, threadName);
t.start();
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jonathan
*/
public class ConnectionHandler implements Runnable {
private Thread t;
private String threadName;
private ServerSocket welcomeSocket;
private Vector<Client> clients;
private boolean active;
public ConnectionHandler(){
threadName = "Connection-Handler";
active = true;
try {
welcomeSocket = new ServerSocket(6789);
clients = new Vector<>();
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Created thread " + threadName);
}
@Override
public void run() {
System.out.println("Running thread " + threadName);
while(active){
try {
clients.add(new Client(clients.size(),welcomeSocket.accept()));
clients.lastElement().start();
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Thread " + threadName + " exiting...");
}
public void start(){
if (t == null){
t = new Thread(this, threadName);
t.start();
}
}
public void setActive(boolean _active)
{
active = _active;
}
}
......@@ -16,20 +16,7 @@ class TCPServer
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
for(;;)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
ConnectionHandler CH = new ConnectionHandler();
CH.start();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment