PBO: Tugas Network Programming

Tugas PBO kali ini adalah membuat program Chatting menggunakan Socket dan ServerSocket berdasrkan contoh program berikut ini:

 Class ServerKu

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

public class ServerKu implements Runnable{
	private Socket sc;
	private int a;
	public static final int PORT=9090;
	public static boolean selesai=false;
	public ServerKu(Socket soc, int cn) {
		this.sc=soc;
		this.a=cn;
	}
	public void run(){
		try {
			InputStream inst= sc.getInputStream();
			InputStreamReader isr=new InputStreamReader(inst);
			BufferedReader brd=new BufferedReader(isr);
			OutputStream os=sc.getOutputStream();
			PrintWriter pwr=new PrintWriter(os);
			pwr.println("Selamat datang di serverku");
			pwr.flush();
			while(true){
				String st= brd.readLine();
				if(st.trim().equalsIgnoreCase("exit"))
				{
					System.out.println("d" + sc.getRemoteSocketAddress().toString()+":" + st);
					break;

				}else{
					System.out.println(sc.getRemoteSocketAddress().toString() + "> " +  st);
					pwr.println(sc.getRemoteSocketAddress().toString() + "> " +  st);
					pwr.flush();
				}
			}
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int cn=0;
		try {
			ServerSocket sc=new ServerSocket(PORT);
			System.out.println("Server dibuka pada PORT: " + PORT);
			while(!selesai){
				Socket soc=sc.accept();
				ServerKu srv=new ServerKu(soc, cn);
				Thread tr=new Thread(srv);
				tr.start();
			}
		} catch (Exception e) {
			System.err.println("SERVER ERROR");
			System.err.println(e.toString());
		}

	}

}

Class ClientKu

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientKu {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int port= ServerKu.PORT;
		String str="";
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader brd=new BufferedReader(isr);

		try {
			Socket sc=new Socket("localhost",port);
			InputStream ic= sc.getInputStream();
			InputStreamReader isrc=new InputStreamReader(ic);
			BufferedReader brdc=new BufferedReader(isrc);
			System.out.println(brdc.readLine());

			OutputStream os=sc.getOutputStream();
			PrintWriter pwr=new PrintWriter(os);
			while((str=brd.readLine())!=null){
				pwr.println(str);
				pwr.flush();
				System.out.println(brdc.readLine());

				if(str.equalsIgnoreCase("exit"))
					System.exit(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} 

	}

}
This entry was posted in Uncategorized. Bookmark the permalink.

45 Responses to PBO: Tugas Network Programming

  1. Good buy swtor credits.

  2. You Could Check HERE

  3. Look At This Web-site

  4. lettre soign¨¦! guild wars 2 gold impeccable! rien a redire vendeur davantage que s¨¦rieux! merci

  5. Sneak A Peek At THIS Web-Site

  6. p90x2 dvds says:

    Check THIS SITE Out

  7. Pingback: sacs louis vuitton pas cher

  8. gucci sale says:

    You can try here

  9. After i instructed excellent chanel outlet I can proclaim We had been very psyched, equally as We are everytime My family and i order them. When I gained that company We had been particularly let down. I procured these folks bigger Nine plus i wound up with the right size which experts claim has been reminiscent of something that are usually located on the little feet related to bigfoot, for the reason that chanel outlet have been excessively gigantic and far on top of that extensive. Although these people were very good I really regarded totally stupid sending these items. Absolutely yes, it is actually a problem with ease permanently fixed, i absolutely dealt with these items proclaiming that these were too large. Therefore ,, quite a while after that My hubby and i had been given individuals and in addition they put alright. We were delighted till the time I had formed observed that the key was missing collected from one of your chanel outlet also, the several other you an opening in buying it. At that time, I just now quit combined with tossed these items away. I actually had not been still wasting any from my own time.

  10. buy chanel says:

    This type of buy chanel Had Emotionally vulnerable For starters 7-day period I obtained These guys!!! Season BOOOT!!!!!!! Avoid getting This type of In no way Really worth Hard cash! My wife and i Used the THEM TO WORK And subsequently Twilight The whole set of SEQUINS Lost control Out there!!!

  11. Click Here To Investigate

  12. Why Not Find Out More

  13. chanel bags says:

    I love that chanel bags it is nice good for typically the exceptional local weather nevertheless chic. I’m hoping to gain on my own one of the traditional tall for an additional the winter season. They’re just amazing!!

  14. Pingback: longchamp pas cher

  15. Neverwinter astral diamonds Versatile and retain me manner.

  16. Pingback: supra black

  17. Pingback: supra france

  18. Pingback: timberland homme

  19. Pingback: timberland pas cher

  20. Pingback: magasin louboutin

  21. Pingback: basket air max pas cher

  22. Pingback: biking jersey

  23. Pingback: bike jersey

  24. The swtor credits truly feel excellent in all predicaments. Thanks for these types of an incredible solution!

  25. Pingback: sac longchamp homme

  26. Pingback: longchamp pliage

  27. Pingback: supra femme

  28. Pingback: supra chaussure

  29. Pingback: chaussure timberland

  30. Pingback: magasin air jordan

  31. Pingback: nike air rift

Leave a Reply

Your email address will not be published. Required fields are marked *