Archive

Archive for December, 2009

MiniChat Client-Server Basic

December 29, 2009 13 comments

Đây là một bài tập về lập trình mạng bằng Java mà nhóm mình đã làm, vì không có nhiều thời gian và công sức đầu tư  để phát triển các chức năng bổ sung nhưng đây cũng là một phần mềm chat đơn giản là có thể gửi tin qua lại giữa các client trong một mạng Lan.

Một số hình ảnh demo:

Phần mềm này viết bằng Java và được export định dạng *.jar nên bạn chỉ cần có máy ảo JVM là có thể chạy được rồi.

Download here:

Client: http://www.mediafire.com/?qxqo4yyyxyg

Server: http://www.mediafire.com/?unwwytmzgzz

Source code:

http://www.mediafire.com/?ymow1mdztqh

Categories: Software Tags: , , , ,

Creating a simple Chat Client/Server Solution – Part5

December 29, 2009 Leave a comment

Stage 5: Chat client moved to very simple GUI interface

import java.net.*;
import java.io.*;
import java.applet.*;
import java.awt.*;
public class ChatClient extends Applet
{  private Socket socket              = null;
private DataInputStream  console   = null;
private DataOutputStream streamOut = null;
private ChatClientThread client    = null;
private TextArea  display = new TextArea();
private TextField input   = new TextField();
private Button    send    = new Button(“Send”), connect = new Button(“Connect”),
quit    = new Button(“Bye”);
private String    serverName = “localhost”;
private int       serverPort = 4444;

public void init()
{  Panel keys = new Panel(); keys.setLayout(new GridLayout(1,2));
keys.add(quit); keys.add(connect);
Panel south = new Panel(); south.setLayout(new BorderLayout());
south.add(“West”, keys); south.add(“Center”, input);  south.add(“East”, send);
Label title = new Label(“Simple Chat Client Applet”, Label.CENTER);
title.setFont(new Font(“Helvetica”, Font.BOLD, 14));
setLayout(new BorderLayout());
add(“North”, title); add(“Center”, display);  add(“South”,  south);
quit.disable(); send.disable(); getParameters(); }
public boolean action(Event e, Object o)
{  if (e.target == quit)
{  input.setText(“.bye”);
send();  quit.disable(); send.disable(); connect.enable(); }
else if (e.target == connect)
{  connect(serverName, serverPort); }
else if (e.target == send)
{  send(); input.requestFocus(); }
return true; }
public void connect(String serverName, int serverPort)
{  println(“Establishing connection. Please wait …”);
try
{  socket = new Socket(serverName, serverPort);
println(“Connected: ” + socket);
open(); send.enable(); connect.disable(); quit.enable(); }
catch(UnknownHostException uhe)
{  println(“Host unknown: ” + uhe.getMessage()); }
catch(IOException ioe)
{  println(“Unexpected exception: ” + ioe.getMessage()); } }
private void send()
{  try
{  streamOut.writeUTF(input.getText()); streamOut.flush(); input.setText(“”); }
catch(IOException ioe)
{  println(“Sending error: ” + ioe.getMessage()); close(); } }
public void handle(String msg)
{  if (msg.equals(“.bye”))
{  println(“Good bye. Press RETURN to exit …”);  close(); }
else println(msg); }
public void open()
{  try
{  streamOut = new DataOutputStream(socket.getOutputStream());
client = new ChatClientThread(this, socket); }
catch(IOException ioe)
{  println(“Error opening output stream: ” + ioe); } }
public void close()
{  try
{  if (streamOut != null)  streamOut.close();
if (socket    != null)  socket.close(); }
catch(IOException ioe)
{  println(“Error closing …”); }
client.close();  client.stop(); }
private void println(String msg)
{  display.appendText(msg + “\n”); }
public void getParameters()
{  serverName = getParameter(“host”);
serverPort = Integer.parseInt(getParameter(“port”)); }
}

Nguồn: sưu tầm

Categories: Java Tags: , , , ,

Creating a simple Chat Client/Server Solution – Part4

December 29, 2009 1 comment

Step 4: A Simple but functional, text based chat server/client

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

public class ChatServer implements Runnable
{  private ChatServerThread clients[] = new ChatServerThread[50];
private ServerSocket server = null;
private Thread       thread = null;
private int clientCount = 0;

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(“Can not bind to port ” + port + “: ” + ioe.getMessage()); }
}
public void run()
{  while (thread != null)
{  try
{  System.out.println(“Waiting for a client …”);
addThread(server.accept()); }
catch(IOException ioe)
{  System.out.println(“Server accept error: ” + ioe); stop(); }
}
}
public void start()  { /* as before */ }
public void stop()   { /* as before */ }
private int findClient(int ID)
{  for (int i = 0; i < clientCount; i++)
if (clients[i].getID() == ID)
return i;
return -1;
}
public synchronized void handle(int ID, String input)
{  if (input.equals(“.bye”))
{  clients[findClient(ID)].send(“.bye”);
remove(ID); }
else
for (int i = 0; i < clientCount; i++)
clients[i].send(ID + “: ” + input);
}
public synchronized void remove(int ID)
{  int pos = findClient(ID);
if (pos >= 0)
{  ChatServerThread toTerminate = clients[pos];
System.out.println(“Removing client thread ” + ID + ” at ” + pos);
if (pos < clientCount-1)
for (int i = pos+1; i < clientCount; i++)
clients[i-1] = clients[i];
clientCount–;
try
{  toTerminate.close(); }
catch(IOException ioe)
{  System.out.println(“Error closing thread: ” + ioe); }
toTerminate.stop(); }
}
private void addThread(Socket socket)
{  if (clientCount < clients.length)
{  System.out.println(“Client accepted: ” + socket);
clients[clientCount] = new ChatServerThread(this, socket);
try
{  clients[clientCount].open();
clients[clientCount].start();
clientCount++; }
catch(IOException ioe)
{  System.out.println(“Error opening thread: ” + ioe); } }
else
System.out.println(“Client refused: maximum ” + clients.length + ” reached.”);
}
public static void main(String args[]) { /* as before */ }
}
import java.net.*;
import java.io.*;

public class ChatServerThread extends Thread
{  private ChatServer       server    = null;
private Socket           socket    = null;
private int              ID        = -1;
private DataInputStream  streamIn  =  null;
private DataOutputStream streamOut = null;

public ChatServerThread(ChatServer _server, Socket _socket)
{  super();
server = _server;
socket = _socket;
ID     = socket.getPort();
}
public void send(String msg)
{   try
{  streamOut.writeUTF(msg);
streamOut.flush();
}
catch(IOException ioe)
{  System.out.println(ID + ” ERROR sending: ” + ioe.getMessage());
server.remove(ID);
stop();
}
}
public int getID()
{  return ID;
}
public void run()
{  System.out.println(“Server Thread ” + ID + ” running.”);
while (true)
{  try
{  server.handle(ID, streamIn.readUTF());
}
catch(IOException ioe)
{  System.out.println(ID + ” ERROR reading: ” + ioe.getMessage());
server.remove(ID);
stop();
}
}
}
public void open() throws IOException
{  streamIn = new DataInputStream(new
BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new
BufferedOutputStream(socket.getOutputStream()));
}
public void close() throws IOException
{  if (socket != null)    socket.close();
if (streamIn != null)  streamIn.close();
if (streamOut != null) streamOut.close();
}
}

Read more…

Categories: Java Tags: , , , ,
Follow

Get every new post delivered to your Inbox.