Java實現一個控制臺的多線程聊天程序
剛學習完Java基本內容,寫個程序
就是想把多線程,IO操作,socket都用上,不過看起來好像簡單些了。不過這里面肯定有些問題是我沒想到的。
大家看看,提點意見,主要是面向對象方面
import java.net.*;
import java.io.*;
import java.util.*;

public class SocketDemo {
public static void main(String[] args) throws Exception {
if(args.length <= 0 || args.length > 2 ) {
System.out.println("I just think you don't know how to use this program.");
System.out.println("Please use '-help' to get more help");
System.exit(-1);
}
if(args[0].equals("help") || args[0].equals("-help")) {
System.out.println(IODemo.getHelp());
System.exit(-1);
}
Thread tServer = new TServer("server",Integer.valueOf(args[0]));
Client client = new Client(Integer.valueOf(args[1]));

//get words from terminal
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
//Start the Server
tServer.start();
String s = "";
while(!(s = sin.readLine()).equals("")) {
client.send(s);
IODemo.writeLog(s);
if(s.equals("bye")) {
System.exit(-1);
}
}
}
}

class TServer extends Thread {
int port = 6666;
TServer(String s,int port) {
super(s);
this.port = port;
}

public void run() {
String strMessage = "";

try {
ServerSocket ss = new ServerSocket(port);
Socket s;
DataInputStream dis;
while(true) {
s = ss.accept();
dis = new DataInputStream(s.getInputStream());
if(!(strMessage = dis.readUTF()).equals("")) {
System.out.println("Client: " + strMessage);
IODemo.writeLog("Client: " + strMessage);
strMessage = "";
}
dis.close();
s.close();
}
} catch(Exception e) {
//e.printStackTrace();
System.out.println("服務器異常退出!");
System.exit(-1);
}
}
}

class Client {
int port = 8888;
Client(int port) {
this.port = port;
}
public void send(String strMessage) throws Exception {
Socket s = new Socket("127.0.0.1",port);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(strMessage);
dos.flush();
dos.close();
s.close();
}
}

class IODemo {

public static String getHelp() {
String str = "";
String strTmp = null;
try {
BufferedReader br = new BufferedReader(new FileReader("d:\\help.dat"));
while((strTmp = br.readLine())!=null) {
str += strTmp + "\n\r" ;
}
br.close();
} catch(IOException e) {
//e.printStackTrace();
System.out.println("幫助文件獲得失??!");
System.exit(-1);
}
return str;
}
public static void writeLog(String s) {
try {
FileWriter fw = new FileWriter("d:\\log.log",true);
PrintWriter log = new PrintWriter(fw);
log.println(s+"------"+ new Date()+ "----");
log.flush();
log.close();
} catch(IOException e) {
//e.printStackTrace();
System.out.println("日志文件寫入錯誤");
System.exit(-1);
}
}
}
按照下圖運行,當然了幫助文件要先寫好。在D盤help.dat文件中寫:
There are two parameter in this program.
Java TCPServer serverPort clientPort.
For example:
Run one as this:java TCPServer 6666 8888
And another one as this:java TCPServer 8888 6666
Then the two intances can communicate.
And you can by pass 'bye' on the terminal to exit this program.
Enjoy it!

就是想把多線程,IO操作,socket都用上,不過看起來好像簡單些了。不過這里面肯定有些問題是我沒想到的。
大家看看,提點意見,主要是面向對象方面





































































































































按照下圖運行,當然了幫助文件要先寫好。在D盤help.dat文件中寫:
There are two parameter in this program.
Java TCPServer serverPort clientPort.
For example:
Run one as this:java TCPServer 6666 8888
And another one as this:java TCPServer 8888 6666
Then the two intances can communicate.
And you can by pass 'bye' on the terminal to exit this program.
Enjoy it!

posted on 2010-09-21 14:55 the_fire 閱讀(1280) 評論(1) 編輯 收藏 所屬分類: Java