the_fire的技术博客

勤奋让我享受更多生命的乐趣
数据加载中……
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!




posted on 2010-09-21 14:55 the_fire 阅读(1275) 评论(1)  编辑  收藏 所属分类: Java

评论

# re: Java实现一个控制台的多线程聊天程序 2014-11-29 14:32 wear他

我觉得这个只能实现两个人聊天啊
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: