The Artima Developer Community
Sponsored Link

Java Answers Forum
How to make multithreaded chat application

1 reply on 1 page. Most recent reply: Dec 7, 2011 3:07 PM by Bojan Jordanovski

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Bojan Jordanovski

Posts: 2
Nickname: maliprinc
Registered: Dec, 2011

How to make multithreaded chat application Posted: Dec 7, 2011 2:58 PM
Reply to this message Reply
Advertisement
I want to make server/client chat,for a many clients,but each of them must has a Inbox(for private messages,for communication "client to client"),every client must first register(username,password) and I have a base (for example. txt file) which will keep the names of all users and their messages.
Please help.


Bojan Jordanovski

Posts: 2
Nickname: maliprinc
Registered: Dec, 2011

Re: How to make multithreaded chat application Posted: Dec 7, 2011 3:07 PM
Reply to this message Reply
Code for Server:

import java.io.*;
import java.net.*;

public class ChatServer{


static KlijentNit klijenti[] = new KlijentNit[10];

public static void main(String args[]) {

int port = 2222;
if(args.length>0)port = Integer.parseInt(args[0]);
Socket clientSocket = null;
try {

ServerSocket serverSocket = new ServerSocket(port);
while(true){
clientSocket = serverSocket.accept();
for(int i=0; i<=klijenti.length; i++){
if(klijenti==null)
{

klijenti = new KlijentNit(clientSocket, klijenti);
klijenti.start();
break;
}


}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}

Code for Client:

import java.io.*;
import java.net.*;

//klasa ChatKlijent implementira "Runnable", kako bi mogla da se pokrene kao nit
public class ChatKlient implements Runnable {


static Socket clientSocket = null;
static PrintStream os = null;
static BufferedReader is = null;
static BufferedReader ulazKonzola = null;
static boolean kraj = false;
static char username;
static char password;
public static void main(String[] args) {
try {
int port = 2222;
if(args.length>0)port = Integer.parseInt(args[0]);
clientSocket = new Socket("localhost",port); ulazKonzola = new BufferedReader(new
InputStreamReader(System.in));

os = new PrintStream(clientSocket.getOutputStream());
is = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
new Thread(new ChatKlient()).start();
while (!kraj) {
os.println(ulazKonzola.readLine());
}

clientSocket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host ");
}catch (IOException e) {
System.err.println("IOException: " + e);
}
}

public void run() {
String linijaOdServera;
try {
while ((linijaOdServera = is.readLine()) != null) {
System.out.println(linijaOdServera);

if (linijaOdServera.indexOf("*** Dovidjenja") == 0) {
kraj = true;
return;
}
}
} catch (IOException e) {
System.err.println("IOException: " + e


);
}
}
}
Code for ClientThread:

import java.io.*;
import java.net.*;

public class KlijentNit extends Thread {

BufferedReader is = null;
PrintStream os = null;
Socket soket = null;
KlijentNit[] klijenti;

public KlijentNit(Socket soket, KlijentNit[] klijent) {
this.soket = soket;
this.klijenti = klijent;
}
public void run() {
String linija;
String ime;
try {
is = new BufferedReader(new InputStreamReader(soket.getInputStream()));
os = new PrintStream(soket.getOutputStream());

os.println("Unesite ime.");
ime = is.readLine();

os.println("Dobrodosao/la " + ime + ".\nZa izlaz unesite /quit");
for (int i = 0; i <= 9; i++) {
if (klijenti != null && klijenti != this) {
klijenti.os.println("*** Novi korisnik: " + ime + " je usao u chat sobu !!! ***");
}
}

while (true) {
linija = is.readLine();
if (linija.startsWith("/quit")) {
break;
}
for (int i = 0; i <= 9; i++) {
if (klijenti != null) {
klijenti.os.println("<" + ime + "> " + linija);
}
}
}
for (int i = 0; i <= 9; i++) {
if (klijenti != null && klijenti != this) {
klijenti.os.println("*** Korisnik " + ime + " izlazi iz chat sobe !!! ***");
}
}
os.println("*** Dovidjenja " + ime + " ***");


soket.close();
} catch (IOException ex) {
ex.printStackTrace();
for (int i = 0; i <= 9; i++) {
if (klijenti == this) {
klijenti = null;
}
}

}
}
}

Flat View: This topic has 1 reply on 1 page
Topic: unexpected_message error while SSLHandshake Previous Topic   Next Topic Topic: Set length of an int.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use