Hallo,
in meinen Code einen weiteren Thread einbauen der auf Port 843 horcht...
also das ist mein ausgangs Code:
Nur sobalt ich probiere da einen weiteren Thread einzubauen, kommen nur noch fehler .... ich schaff das einfach nicht, vielleicht könnt ihr mir da ein paar Tipps geben...
hier ist mal der von mir geänderte code:
Im SecureServiceThread passiert nichts, also die nachricht wird garnicht erst ausgegeben ...
in meinen Code einen weiteren Thread einbauen der auf Port 843 horcht...
also das ist mein ausgangs Code:
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class EchoServers
{
ServerSocket m_ServerSocket;
ArrayList<Socket> clients = new ArrayList<Socket>();
public EchoServers()
{
try
{
// Create the server socket.
m_ServerSocket = new ServerSocket(2005);
}
catch(IOException ioe)
{
System.out.println("Could not create server socket at 2005. Quitting.");
System.exit(-1);
}
System.out.println("Listening for clients on 2005...");
// Successfully created Server Socket. Now wait for connections.
int id = 0;
while(true)
{
try
{
// Accept incoming connections.
Socket clientSocket = m_ServerSocket.accept();
clients.add(clientSocket);
// accept() will block until a client connects to the server.
// If execution reaches this point, then it means that a client
// socket has been accepted.
// For each client, we will start a service thread to
// service the client requests. This is to demonstrate a
// multithreaded server, although not required for such a
// trivial application. Starting a thread also lets our
// EchoServer accept multiple connections simultaneously.
// Start a service thread
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
catch(IOException ioe)
{
System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
ioe.printStackTrace();
}
}
}
public static void main (String[] args)
{
new EchoServers();
}
class ClientServiceThread extends Thread
{
Socket m_clientSocket;
int m_clientID = -1;
boolean m_bRunThread = true;
ClientServiceThread(Socket s, int clientID)
{
m_clientSocket = s;
m_clientID = clientID;
}
public void run()
{
// Obtain the input stream and the output stream for the socket
// A good practice is to encapsulate them with a BufferedReader
// and a PrintWriter as shown below.
BufferedReader in = null;
PrintWriter out = null;
// Print out details of this connection
System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " +
m_clientSocket.getInetAddress().getHostName());
try
{
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
// At this point, we can read for input and reply with appropriate output.
// Run in a loop until m_bRunThread is set to false
while(m_bRunThread)
{
// read incoming stream
String clientCommand = in.readLine();
System.out.println("Client Says :" + clientCommand);
for (Socket s :clients){
out =new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
if(clientCommand != null)
{
out.println(clientCommand);
out.flush();
}
}
if(clientCommand.equalsIgnoreCase("k"))
{
// Special command. Quit this thread
m_bRunThread = false;
System.out.print("Stopping client thread for client : " + m_clientID);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("FINALE");
// Clean up
try
{
in.close();
out.close();
m_clientSocket.close();
System.out.println("...Stopped");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}
Nur sobalt ich probiere da einen weiteren Thread einzubauen, kommen nur noch fehler .... ich schaff das einfach nicht, vielleicht könnt ihr mir da ein paar Tipps geben...
hier ist mal der von mir geänderte code:
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class EchoServers
{
ServerSocket m_ServerSocket;
ServerSocket m_SecureSocket;
ArrayList<Socket> clients = new ArrayList<Socket>();
ArrayList<Socket> secureclients = new ArrayList<Socket>();
public EchoServers()
{
try
{
// Create the server socket.
m_ServerSocket = new ServerSocket(2005);
}
catch(IOException ioe)
{
System.out.println("Could not create server socket at 2005. Quitting.");
System.exit(-1);
}
System.out.println("Listening for clients on 2005...");
try
{
// Create the server socket.
m_SecureSocket = new ServerSocket(843);
}
catch(IOException ioe)
{
System.out.println("Could not create server socket at 843. Quitting.");
System.exit(-1);
}
System.out.println("Listening for clients on 843...");
// Successfully created Server Socket. Now wait for connections.
int id = 0;
int sid = 0;
while(true)
{
try
{
// Accept incoming connections.
Socket clientSocket = m_ServerSocket.accept();
clients.add(clientSocket);
// accept() will block until a client connects to the server.
// If execution reaches this point, then it means that a client
// socket has been accepted.
// For each client, we will start a service thread to
// service the client requests. This is to demonstrate a
// multithreaded server, although not required for such a
// trivial application. Starting a thread also lets our
// EchoServer accept multiple connections simultaneously.
// Start a service thread
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
catch(IOException ioe)
{
System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
ioe.printStackTrace();
}
try
{
// Accept incoming connections.
Socket secureclientSocket = m_SecureSocket.accept();
secureclients.add(secureclientSocket);
// accept() will block until a client connects to the server.
// If execution reaches this point, then it means that a client
// socket has been accepted.
// For each client, we will start a service thread to
// service the client requests. This is to demonstrate a
// multithreaded server, although not required for such a
// trivial application. Starting a thread also lets our
// EchoServer accept multiple connections simultaneously.
// Start a service thread
SecureServiceThread secThread = new SecureServiceThread(secureclientSocket, sid++);
secThread.start();
}
catch(IOException ioe)
{
System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
ioe.printStackTrace();
}
}
}
public static void main (String[] args)
{
new EchoServers();
}
class SecureServiceThread extends Thread
{
Socket m_secureSocket;
int m_clientID = -1;
boolean m_bRunThread = true;
SecureServiceThread(Socket s, int clientID)
{
System.out.println("HIER PASSIERT NICHTS!!! :(");
m_secureSocket = s;
m_clientID = clientID;
}
}
class ClientServiceThread extends Thread
{
Socket m_clientSocket;
int m_clientID = -1;
boolean m_bRunThread = true;
ClientServiceThread(Socket s, int clientID)
{
m_clientSocket = s;
m_clientID = clientID;
}
public void run()
{
// Obtain the input stream and the output stream for the socket
// A good practice is to encapsulate them with a BufferedReader
// and a PrintWriter as shown below.
BufferedReader in = null;
PrintWriter out = null;
// Print out details of this connection
System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " +
m_clientSocket.getInetAddress().getHostName());
try
{
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
// At this point, we can read for input and reply with appropriate output.
// Run in a loop until m_bRunThread is set to false
while(m_bRunThread)
{
// read incoming stream
String clientCommand = in.readLine();
System.out.println("Client Says :" + clientCommand);
for (Socket s :clients){
out =new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
if(clientCommand != null)
{
out.println(clientCommand);
out.flush();
}
}
if(clientCommand.equalsIgnoreCase("k"))
{
// Special command. Quit this thread
m_bRunThread = false;
System.out.print("Stopping client thread for client : " + m_clientID);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("FINALE");
// Clean up
try
{
in.close();
out.close();
m_clientSocket.close();
System.out.println("...Stopped");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}
Im SecureServiceThread passiert nichts, also die nachricht wird garnicht erst ausgegeben ...