dimanche 3 mai 2015
Socket Wie ein einfacher Multithreads Service mit Telnet als Client mit Observable/Observer gelöst....
Posted on 11:35 by verona
z.B. als Client: telnet 192.168.121.35 7777
untere code als ausführbare JAR-File erstellen
danach aufruf mit: java -jar ChattService.jar -p 7777
:oops:
untere code als ausführbare JAR-File erstellen
danach aufruf mit: java -jar ChattService.jar -p 7777
:oops:
Java Code:
-
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
import java.io.PrintWriter;
-
import java.net.ServerSocket;
-
import java.net.Socket;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
import java.util.Observable;
-
import java.util.Observer;
-
import java.util.Scanner;
-
-
-
-
-
/**
-
* @author Adriano Zuccarello
-
*
-
*/
-
-
-
-
private long nofThreads = 0;
-
private int mPort = 7575;
-
-
public ChatThreadService() {
-
}
-
this();
-
mPort = setup(pArgs);
-
}
-
-
/**
-
* setup
-
*
-
* @param pArgs
-
* -p <port> [-n <name>] default: -n user.name
-
*/
-
for (int i = 0; i < pArgs.length; i++) {
-
if ((pArgs[i].endsWith("-p") && ((i + 1) < pArgs.length))) {
-
try {
-
}
-
}
-
}
-
-
-
if (mPort <= 0) {
-
}
-
-
return mPort;
-
}
-
-
public void start() {
-
try {
-
-
try {
-
while( (incomingSocket = serviceSocket.accept()) != null) {
-
-
ClientThread threadClient = new ClientThread(incomingSocket, nofThreads);
-
-
this.addObserver(threadClient);
-
-
thread.start();
-
-
broadcast("Client-"+ nofThreads +" logged in");
-
-
}
-
}
-
finally {
-
serviceSocket.close();
-
}
-
-
ioex.printStackTrace();
-
}
-
}
-
-
private void removeObserver(ClientThread pCTh) {
-
this.deleteObserver(pCTh);
-
--nofThreads;
-
}
-
-
// add HH:mm:ss and \n to the message
-
this.setChanged();
-
}
-
-
-
-
//Chat-Client Thread
-
private long mCounter;
-
-
this.mSocket = pSocket;
-
this.mCounter = pCount;
-
-
-
}
-
-
-
@Override
-
public void run() {
-
try {
-
try {
-
mChatterName = mSocket.getInetAddress().getHostAddress() +" Chatter-"+ mCounter +": ";
-
-
printMessage(mSocket.getOutputStream(), "Hello "+ mChatterName +"! Enter BYE to exit.\n", true);
-
-
Scanner inScanner = new Scanner(inStream);
-
-
boolean done = false;
-
while(!done && inScanner.hasNextLine()) {
-
done = line.trim().equalsIgnoreCase("BYE") ? true : false;
-
broadcast(mChatterName + line);
-
}
-
-
inScanner.close();
-
}
-
finally {
-
removeObserver(this);
-
mSocket.close();
-
}
-
-
ioex.printStackTrace();
-
}
-
}
-
-
pwout.println(msg);
-
}
-
-
-
@Override
-
try {
-
if(observerArgs != null && mChatterName != null && !observerArgs.contains(mChatterName)) { /* don't show your self */
-
printMessage(mSocket.getOutputStream(), observerArgs, true);
-
}
-
e.printStackTrace();
-
}
-
}
-
-
}//End Chat-Client Thread
-
-
-
-
-
-
-
ChatThreadService thService = new ChatThreadService(args);
-
thService.start();
-
}
-
-
-
}
Socket Wie ein einfacher Multithreads Service mit Telnet als Client mit Observable/Observer gelöst....
Inscription à :
Publier les commentaires (Atom)
0 commentaires:
Enregistrer un commentaire