Tolong pilih kategori sesuai, jenis posting (diskusi atau bukan) dan sertakan tag/topik yang sesuai seperti komputer, java, php, mysql, dll. Promosi atau posting tidak pada tempatnya akan kami hapus!
- Bagi Anda yang ingin mendaftar, baca link berikut:
http://diskusiweb.com/discussion/50491/how-to-registrasi-diskusiweb-com-baca-ini-terlebih-dahulu
- Cara menyisipkan kode program supaya tampil rapi dan terformat dengan baik di diskusiweb.com: http://www.diskusiweb.com/discussion/50415/cara-menyisipkan-kode-program-di-diskusiweb-com
- Cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

Chatting Java Room + Private (Class Client)

CLIENT


package clientconsul;
 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author ridwansyah
*/
public class Client implements Runnable {
 
static Socket clientSocket = null;
static PrintStream os = null;
static BufferedReader is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
 
public static void main(String[] args) {
 
int port_number=6666;
String host="localhost";
 
if (args.length < 2)
{
System.out.println("Host = "+host+", port_number = "+port_number);
} else {
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
 
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 
} catch (UnknownHostException e){
System.err.println("Host tidak ditemukan "+host);
} catch (IOException e) {
System.err.println("Tidak dapat menampilkan I/O "+host);
}
 
if (clientSocket != null && os != null && is != null){
 
try {
new Thread(new Client()).start();
while (!closed){
os.println(inputLine.readLine()+'\n');
}
os.close();
is.close();
clientSocket.close();
 
}catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
 
@Override
public void run(){
String responseLine;
try{
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1) break;
}
closed=true;
} catch (IOException e){
System.err.println("IOException: " + e);
}
}
}




Smoga dapat membantu :D
Tagged:
Sign In or Register to comment.