Creating a simple Chat Client/Server Solution – Part2

Step 2: Many Time Server, One Client (Client is as before)

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

public class ChatServer implements Runnable
{  private Socket       socket = null;
private ServerSocket server = null;
private Thread       thread = null;
private DataInputStream  streamIn  =  null;

public ChatServer(int port)
{  try
{  System.out.println(“Binding to port ” + port + “, please wait  …”);
server = new ServerSocket(port);
System.out.println(“Server started: ” + server);
start();
}
catch(IOException ioe)
{  System.out.println(ioe);
}
}
public void run()
{  while (thread != null)
{   try
{  System.out.println(“Waiting for a client …”);
socket = server.accept();
System.out.println(“Client accepted: ” + socket);
open();
boolean done = false;
while (!done)
{  try
{  String line = streamIn.readUTF();
System.out.println(line);
done = line.equals(“.bye”);
}
catch(IOException ioe)
{  done = true;  }
}
close();
}
catch(IOException ie)
{  System.out.println(“Acceptance Error: ” + ie);  }
}
}
public void start()
{  if (thread == null)
{  thread = new Thread(this);
thread.start();
}
}
public void stop()
{  if (thread != null)
{  thread.stop();
thread = null;
}
}
public void open() throws IOException
{  streamIn = new DataInputStream(new
BufferedInputStream(socket.getInputStream()));
}
public void close() throws IOException
{  if (socket != null)    socket.close();
if (streamIn != null)  streamIn.close();
}
public static void main(String args[])
{  ChatServer server = null;
if (args.length != 1)
System.out.println(“Usage: java ChatServer port”);
else
server = new ChatServer(Integer.parseInt(args[0]));
}
}

Nguồn: Sưu tầm

Creating a simple Chat Client/Server Solution – Part1

Here is an example of how to extend a very simple client-server demo program into a fully functioning (but simple) Chat Client/Server package. There are five stages involved:

Step 1: A simple server that will accept a single client connection and display everything the client says on the screen. If the client user types “.bye”, the client and the server will both quit.
Step 2: A server as before, but this time it will remain ‘open’ for additional connection once a client has quit. The server can handle at most one connection at a time.
Step 3: A server as before, but this time it can handle multiple clients simultaneously. The output from all connected clients will appear on the server’s screen.
Step 4: A server as before, but this time it sends all text received from any of the connected clients to all clients. This means that the server has to receive and send, and the client has to send as well as receive
Step 5: Wrapping the client from step 4 into a very simple GUI interface but not changing the functionality of either server or client. The client is implemented as an Applet, but a Frame would have worked just as well (for a stand-alone program).

Step 1: Simple, one-time Server

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

public class ChatServer
{  private Socket          socket   = null;
private ServerSocket    server   = null;
private DataInputStream streamIn =  null;

public ChatServer(int port)
{  try
{  System.out.println(“Binding to port ” + port + “, please wait  …”);
server = new ServerSocket(port);
System.out.println(“Server started: ” + server);
System.out.println(“Waiting for a client …”);
socket = server.accept();
System.out.println(“Client accepted: ” + socket);
open();
boolean done = false;
while (!done)
{  try
{  String line = streamIn.readUTF();
System.out.println(line);
done = line.equals(“.bye”);
}
catch(IOException ioe)
{  done = true;
}
}
close();
}
catch(IOException ioe)
{  System.out.println(ioe);
}
}
public void open() throws IOException
{  streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
public void close() throws IOException
{  if (socket != null)    socket.close();
if (streamIn != null)  streamIn.close();
}
public static void main(String args[])
{  ChatServer server = null;
if (args.length != 1)
System.out.println(“Usage: java ChatServer port”);
else
server = new ChatServer(Integer.parseInt(args[0]));
}
}

The Simple Client corresponding to the previous server (and to step 2 and step 3 servers as well):

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

public class ChatClient
{  private Socket socket              = null;
private DataInputStream  console   = null;
private DataOutputStream streamOut = null;

public ChatClient(String serverName, int serverPort)
{  System.out.println(“Establishing connection. Please wait …”);
try
{  socket = new Socket(serverName, serverPort);
System.out.println(“Connected: ” + socket);
start();
}
catch(UnknownHostException uhe)
{  System.out.println(“Host unknown: ” + uhe.getMessage());
}
catch(IOException ioe)
{  System.out.println(“Unexpected exception: ” + ioe.getMessage());
}
String line = “”;
while (!line.equals(“.bye”))
{  try
{  line = console.readLine();
streamOut.writeUTF(line);
streamOut.flush();
}
catch(IOException ioe)
{  System.out.println(“Sending error: ” + ioe.getMessage());
}
}
}
public void start() throws IOException
{  console   = new DataInputStream(System.in);
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void stop()
{  try
{  if (console   != null)  console.close();
if (streamOut != null)  streamOut.close();
if (socket    != null)  socket.close();
}
catch(IOException ioe)
{  System.out.println(“Error closing …”);
}
}
public static void main(String args[])
{  ChatClient client = null;
if (args.length != 2)
System.out.println(“Usage: java ChatClient host port”);
else
client = new ChatClient(args[0], Integer.parseInt(args[1]));
}
}

Nguồn: Sưu tầm

GiftWrap

GiftWrap

GiftWrap là phần mềm mình tạm dịch là phần mềm dành cho các artist chia sẽ theme, developer chia sẽ soft hoặc người dùng cài đặt phần mềm và không có sourcecode.
Nó sẽ giúp bạn tạo .deb packages thông qua môi trường trực quan hơn là dùng dòng lệnh trong Linux.
Hướng dẫn cài đặt
bạn có thể vào System → Administration → Software Sources. Chọn tab Third-Party Software, bấm Add và chèn thêm đoạn code sau vào:
Với 9.04 “Jaunty Jackalope”: deb http://ppa.launchpad.net/giftwrap/ppa/ubuntu jaunty main #GiftWrap
8.10 “Intrepid Ibex”: deb http://ppa.launchpad.net/giftwrap/ppa/ubuntu intrepid main #GiftWrap
9.10 “Karmic Koala”: deb http://ppa.launchpad.net/giftwrap/ppa/ubuntu karmic main #GiftWrap
Close lại và chọn tiếp tab Authentication.
Download key:

http://giftwrap.tuxfamily.org/downloads/giftwrap-ppa-key

Sử dụng Import Key File… button đẻ import key.
Sau đó close và Reload và bắt đầu cài đặt được rồi.(có thể dùng Add or remove program hoặc bất kì trình cài đặt phần mềm khác.)
Một cách khác là dùng lệnh cài:

echo deb http://ppa.launchpad.net/giftwrap/ppa/ubuntu `lsb_release -cs` main | sudo tee /etc/apt/sources.list.d/ppa.list
sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com 207CAD03
sudo apt-get update && sudo apt-get install giftwrap

Nguồn: http://giftwrap.tuxfamily.org/

Lập trình trên Ubuntu – Phần 1

Lập trình trên Ubuntu – Phần 1

Lập trình ( programming) là một công việc không mấy hấp dẫn. Bạn phải nặng óc suy nghĩa tìm ra phương pháp, giải thuật để đi giải quyết những bài toán thực tế phức tạp; đôi khi còn đau đầu thêm. Tuy vậy nó là một phần của dân IT; lập trình không chỉ để có thể rèn luyện tư duy logic mà còn để hiểu rõ thêm tại sao người ta có thể làm nên những điều kì diệu trên chiếc máy người ta gọi là PC.

Lập trình trên Linux cũng vậy; một cái lợi là bạn có thể hiểu rõ hơn các lệnh và cơ chế chủa Linux từ đó giúp bạn có cái nhìn tổng quan hơn về phần mềm và chương trình máy tính. Bài viết này sẽ một phần nào giới thiệu cho các bạn biết về lập trình trên Ubuntu ( một distribution thông dụng của Linux); do đó nó vẫn có thể áp dụng cho các distro khác

  • Trước tiên bạn cần có IDE ( môi trường lập trình, soạn thảo code) đã: trên Ubuntu ( hay Linux ) bạn có thể chọn một trong số các phần mềm sau, tất cả đều miễn phí.
    *Emacs     *Editra     *Gedit       *kate
    *Kwrite    *Vim         *SciTE
  • Interface Designers
    *Glade
  • Bạn cũng có thể cài thêm
    *Anjuta  *Code::Blocks  *Eclipse  *Eric Python IDE
    *Geany   *GNAT Programming Studio  *IDLE  *KDevelop  *Lazarus
    *MonoDevelop *NetBeans
  • Ngôn ngữ hổ trợ
    * Ada  *Assembly  *C  *C++
    *Fortran  *Haskell  *Java
    *Lisp
    *Perl  *Python  *Ruby
    *Tcl
  • Tạm thời bài đầu tiên bạn cần chú ý các công cụ đó thui, bài sau sẽ nói rõ hơn từng công cụ, và từng ngôn ngữ trên đó.