The forums are currently locked and only available for read only access
ophe
Almost a full-blooded keyboard-warrior
Medlem
Forum Posts: 173
Member Since:
July 30, 2003
July 30, 2003
Offline
Hej hej!
Jag har satt up en server till vilken flera klienter kan ansluta. Servern använder sig av trådar och kommunikationen mellan servertrådarna och klienterna fungerar felfritt.. jag har dock tänk att göra så att man kan chatta, vilket gör att trådarna måste kunna kommunicera med varandra... hur gör man det? Alltså... Clienterna kan skicka text till och från Servern, och severn kan svara, men jag lyckas inte göra så att servern skickar ut det som alla skriver till alla clienter. En broakcast function som går att köra från KKMultiServerThread vore antagligen en lösning, men jag vet ej hur...
Server startas med den här.
import java.net.*;
import java.io.*;
public class KKMultiServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening)
new KKMultiServerThread(serverSocket.accept()).start();
serverSocket.close();
}
}
Servertrådarna för som skapas för varje anslutande client
import java.net.*;
import java.io.*;
public class KKMultiServerThread extends Thread {
private Socket socket = null;
public KKMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run() {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String inputLine, outputLine;
// h = hide: Don't show on clientside, with response
// n = no response: Show on clientside, no response
// s = show: Show on clientside, with response
//login
inputLine="";
while (inputLine.length()<1) {
outputLine = "sEnter your nickname";
out.println(outputLine);
inputLine = in.readLine();
}
out.println("n" + inputLine + ", is logged in. ");
System.out.println(inputLine + " logged in");
out.println("s--------------- You are now logged in ---------------");
// Chat
while ((inputLine = in.readLine()) != null) {
outputLine=inputLine;
out.println("s"+outputLine);
if (outputLine.equals("Bye")){
break;
}
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
System.out.println("Connection Broken");
}
}
}
Här är koden för klienten
import java.io.*;
import java.net.*;
public class KnockKnockClient {
public static void main(String[] args) throws IOException {
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("130.236.229.182", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: server.");
System.exit(1);
}
//System.out.println("pHej".substring(1,inputLine.length()));
// h = hide: Don't show on clientside, with response
// n = no response: Show on clientside, no response
// s = show: Show on clientside, with response
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
if ((!fromServer.equals("null"))&&(fromServer.length()>1)){
System.out.println("Server: " + fromServer.substring(1,fromServer.length()));
}
if ((fromServer.charAt(0)=='h')||(fromServer.charAt(0)=='s')){
fromUser = stdIn.readLine();
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1030
Currently Online:
Guest(s) 291
Currently Browsing this Page:
1 Guest(s)
1 Guest(s)
Top Posters:
Andreas Galistel: 16287
Jonas Klar: 15897
ilg@dd: 10810
Nyhet: 10607
Mind: 10550
Ctrl: 10355
Gueno: 9881
Guest: 9344
Snorch: 8881
Callister: 8468
Newest Members:
PetrbonFU PetrbonFU
Karine Bembry
Dolores Mcdaniels
Anibal McLeish
Francisca Alt
Alfie Everhart
Lester Huitt
Orlando Jorgensen
Mikki Lundgren
Dakota Kozlowski
Forum Stats:
Groups: 11
Forums: 59
Topics: 146630
Posts: 1300967
Member Stats:
Guest Posters: 2
Members: 79425
Moderators: 0
Admins: 11
Administrators: nordicadmin, Henrik Berntsson, Anton Karmehed, Carl Holmberg, Joel Oscarsson, Mikael Linnér, Mikael Schwartz, Andreas Paulsson, Nickebjrk, Mattias Pettersson, EmxL
© Simple:Press —