我的第一篇博客!过去的一周,应老师要求,用java编写了一个聊天室的程序,今天上午刚刚验收结束,自己对这个程序还算满意。本科阶段没有养成做完项目写报告写博客的好习惯,现在要读研了,秉承着助人为乐和帮助自己梳理思绪的初衷,决定以后多写些博客记录一下自己的学习和生活。
废话不多说了,直接上干货。程序实现了登陆、退出、单聊、群聊、预定义文字表情、在线用户查询、历史记录查询等功能,有命令行和图形界面两个版本,在文章的下面会附上代码。编写这个程序我用到了Git(版本控制,老师要求使用,大家编程时也可以不用)、Socket、IO、字符串处理、多线程、线程安全、容器、异常处理、AWT/Swing等知识。不过由于这是我用java编写的第一个程序(HelloWorld就不算了),我知道程序虽然能够运行,但其中异常处理和GUI等一些方面我做得还不是很好,也希望路过的大神们能不吝赐教。
效果图
服务器程序
ChatServer.java
package chatroom.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author lqshanshuo
*/
public class ChatServer {
public static void main(String[] args) {
int port =12345;//TCP端口号
ServerSocket serverSocket=null;//声明服务器ServerSocket类
try{
serverSocket = new ServerSocket(port);//创建服务器
Map<Socket,String> socketMap=new ConcurrentHashMap();//创建同步的Map存放连接状态的客户端及其用户名
while (true) {
Socket socket = serverSocket.accept();//阻塞等待客户端连接
try{
new Mult(socket,socketMap);//为刚连接的客户端创建单独线程
}catch(IOException e){
socket.close();//线程启动故障
}
}
}catch(IOException e){
System.out.println("Server starting failed");//服务器创建故障
}
}
}
Mult.java
package chatroom.server;
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;
/**
*
* @author lqshanshuo
*/
class Mult extends Thread {
private Socket socket;
private Map<Socket,String> socketMap;
private DataOutputStream dos;
private DataInputStream dis;
String strName[]=null;
public Mult(Socket s,Map<Socket,String> sM) throws IOException{
socket = s;
socketMap=sM;
//用于向客户端发送数据的输出流
dos = new DataOutputStream(socket.getOutputStream());
//用于接收客户端发来的数据的输入流
dis = new DataInputStream(socket.getInputStream());
start();
}
public void run(){
try{
//登录检测
dos.writeUTF("Please login:");
while(true){
String str = dis.readUTF();
strName = str.split(" +");
if(str.startsWith("/login")&&(strName.length>1))
{
//检测用户名是否重复
int nameUsed = 0;
for(Socket s:socketMap.keySet()){
if(strName[1].equals(socketMap.get(s))){
nameUsed = 1;
}
}
if(nameUsed==1){
dos.writeUTF("This name has been used.Please choose another.");
}else{
//链接成功,将用户信息存入Map,并通知所有用户有人上线了
System.out.println(strName[1]+" has connected successfully");
socketMap.put(socket, strName[1]);
dos.writeUTF("You have logined.");
for(Socket s:socketMap.keySet()){
if(!s.equals(socket)){
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strName[1]+" has logined.");
}
}
break;
}
}
dos.writeUTF("Invalid command.");
}
while(true){
//开始聊天
int noticeNum=0;
String str = dis.readUTF();
if("/quit".equals(str))//检测是否退出
{
System.out.println(strName[1]+" has finished");
break;
}else if("/who".equals(str)){//为用户提供查看当前所有在线用户服务
dos = new DataOutputStream(socket.getOutputStream());
for(Socket s:socketMap.keySet()){
dos.writeUTF(socketMap.get(s));
}
dos.writeUTF("Total online user:"+String.valueOf(socketMap.size()));
}else{
//消息处理(判断1群聊还是私聊2是否发送表情)
for(Socket s:socketMap.keySet()){
if((str.contains(socketMap.get(s)))&&(str.indexOf(socketMap.get(s))>0)&&('@'==str.charAt(str.indexOf(socketMap.get(s))-1))){
if(!str.startsWith("//")){
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strName[1]+" says to you: "+str.replace("@"+socketMap.get(s), ""));
}else{
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strName[1]+" says to you: "+emotion(str,socketMap.get(s)));
}
if(noticeNum<1){
if(!str.startsWith("//")){
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("you says to "+socketMap.get(s)+": "+str.replace("@"+socketMap.get(s), ""));
}else{
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("you says to "+socketMap.get(s)+": "+emotion(str,socketMap.get(s)));
}
}
noticeNum++;
}
}
if(noticeNum<1){
for(Socket s:socketMap.keySet()){
if(!s.equals(socket)){
if((str.startsWith("//"))&&(str.contains("@all"))){
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strName[1]+" says to all: "+emotion(str,"all"));
}else{
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strName[1]+" says to all: "+str);
}
}else{
if((str.startsWith("//"))&&(str.contains("@all"))){
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strName[1]+" says to all: "+emotion(str,"all"));
}else{
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("You says to all: "+str);
}
}
}
}
noticeNum=0;
}
}
socketMap.remove(this.socket);
socket.close();
}catch(IOException e){
//System.out.println("lala"+e.getMessage());
}
}
public String emotion(String cc,String str){
//预定义表情
char c=cc.charAt(2);
if("all".equals(str)){
if('G'==c)
return("Hello everybody~");
else if('A'==c)
return("I hate all of you!");
else if('B'==c)
return("It's time for me to leave,bye-bye.");
else
return("There is no perset emotion for your words.");
}else{
if('G'==c)
return("Nice to meet you,"+str+".");
else if('A'==c)
return(str+",you make me angry!");
else if('B'==c)
return(str+"I look forward to talk with you next time,goodbye.");
else
return("There is no perset emotion for your words.");
}
}
}
客户端程序(命令行版)
ChatClient.java
package chatroom.client;
import java.io.*;
import java.io.IOException;
import static java.lang.Thread.sleep;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author lqshanshuo
*/
public class ChatClient {
public static void main(String[] args){
String hostName = "localhost";
int portNumber = 12345;
Socket socket=null;//声明Socket类
try {
try{
socket = new Socket(hostName,portNumber);//新建socket连接
}catch(Exception e){
System.out.println("Connection failed!");
}
//获取用户键盘输入字符流
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
//获取输出流,用于客户端向服务器端发送数据
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//本地创建文本文件存储历史记录
String historyFileName;
do{
historyFileName=""+(int)(Math.random()*100000);
}while(new File("./history/"+historyFileName+".txt").exists());
File f = new File("./history/"+historyFileName+".txt");
PrintWriter history = new PrintWriter(new FileWriter(f,true));
try{
new ReceiveThread(socket,history);//创建单独线程接收服务器信息
}catch(IOException e){
socket.close();
f.delete();
System.out.println("Receive Thread start failed!This socket is closed.");
}
while(true){
//获取用户键盘输入
String str = userInput.readLine();
try{sleep(1);}catch(Exception e){System.out.println(e.getMessage());}
//历史记录功能
if(str.startsWith("/history")){
String[] strSplit=str.split(" +");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String lineTxt = null;
int i=0,sign=0;
System.out.println("-------------history-------------");
if(strSplit.length==1){//无参数时默认显示50条历史记录
while((lineTxt = br.readLine()) != null){
i++;
if(i<=50){
System.out.println(i+"."+lineTxt);
sign=1;
}
}
}
if(strSplit.length==2){//可选参数,从第几条开始显示
while((lineTxt = br.readLine()) != null){
i++;
if(i>=Integer.valueOf(strSplit[1])){
System.out.println(i+"."+lineTxt);
sign=1;
}
}
}
if(strSplit.length==3){//可选参数,一共显示多少条
while((lineTxt = br.readLine()) != null){
i++;
if(i>=(Integer.valueOf(strSplit[1]))+(Integer.valueOf(strSplit[2])))
break;
if(i>=Integer.valueOf(strSplit[1])){
System.out.println(i+"."+lineTxt);
sign=1;
}
}
}
if(sign==0)
System.out.println("No history meet the requirements");//无满足要求历史记录
System.out.println("------------------------------------");
}else{
dos.writeUTF(str);
if("/quit".equals(str)){//退出
System.out.println("quit!!!!!!!!!!!!!!!");
f.delete();
break;
}
}
}
socket.close();
}catch(IOException e){
System.out.println(e.getMessage());
}
}
}
ReceiveThread.java
package chatroom.client;
import java.io.*;
import java.net.*;
/**
*
* @author lqshanshuo
*/
class ReceiveThread extends Thread{
private Socket socket;
private PrintWriter history;
private DataInputStream dis;
public ReceiveThread(Socket s,PrintWriter h) throws IOException{
socket = s;
history = h;
dis = new DataInputStream(socket.getInputStream());
start();
}
public void run(){
try{
while(true){
String strTemp=dis.readUTF();//读取服务器发送来的内容
System.out.println(strTemp);//显示在屏幕上
history.println(strTemp);//存在历史记录里
history.flush();
}
}catch(IOException e){
//System.out.println("ReceiveThread is over.");
}
}
}
客户端程序(图形界面版)
ChatroomJFrame.java
package chatroom.client;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
/**
*
* @author lqshanshuo
*/
public class ChatroomJFrame extends javax.swing.JFrame {
File f=null;
DataOutputStream dos=null;
DataInputStream dis=null;
Socket socket=null;
public void connect(){
String hostName = "localhost";
int portNumber = 12345;
try {
try{
socket = new Socket(hostName,portNumber);
}catch(Exception e){
System.out.println("Connection failed!");
}
//获取输出流,用于客户端向服务器端发送数据
dos = new DataOutputStream(socket.getOutputStream());
//获取输入流,用于接收服务器端发送来的数据
dis = new DataInputStream(socket.getInputStream());
String historyFileName;
do{
historyFileName=""+(int)(Math.random()*100000);
}while(new File("./history/"+historyFileName+".txt").exists());
f = new File("./history/"+historyFileName+".txt");
PrintWriter history = new PrintWriter(new FileWriter(f,true));
try{
new ReceiveThread1(this,socket,history);
}catch(IOException e){
socket.close();
f.delete();
System.out.println("Receive Thread start failed!This socket is closed.");
}
}catch(IOException e){
System.out.println(e.getMessage());
}
}
public int send(String str){
//try{sleep(1);}catch(Exception e){System.out.println(e.getMessage());}
if(str.startsWith("/history")){
try{
String[] strSplit=str.split(" +");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String lineTxt = null;
int i=0,sign=0;
jTextArea1.append("-------------history-------------"+"\n");
if(strSplit.length==1){
while((lineTxt = br.readLine()) != null){
i++;
if(i<=50){
jTextArea1.append(i+"."+lineTxt+"\n");
sign=1;
}
}
}
if(strSplit.length==2){
while((lineTxt = br.readLine()) != null){
i++;
if(i>=Integer.valueOf(strSplit[1])){
jTextArea1.append(i+"."+lineTxt+"\n");
sign=1;
}
}
}
if(strSplit.length==3){
while((lineTxt = br.readLine()) != null){
i++;
if(i>=(Integer.valueOf(strSplit[1]))+(Integer.valueOf(strSplit[2])))
break;
if(i>=Integer.valueOf(strSplit[1])){
jTextArea1.append(i+"."+lineTxt+"\n");
sign=1;
}
}
}
if(sign==0)
jTextArea1.append("No history meet the requirements"+"\n");
jTextArea1.append("------------------------------------"+"\n");
}catch(IOException e){System.out.println(e.getMessage());}
}else{
try{
dos.writeUTF(str);}catch(IOException e){System.out.println(e.getMessage());}
if("/quit".equals(str)){
jTextArea1.append("quit!!!!!!!!!!!!!!!"+"\n");
f.delete();
return 0;
}
}
return 1;
}
public void receive(String str){
jTextArea1.append(str+"\n");
jTextArea1.setCaretPosition(jTextArea1.getText().length());
}
public ChatroomJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ChatRoom");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jButton1.setText(" send ");
jButton1.setToolTipText("");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("Greet");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("Angry");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("Goodbye");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton4);
jRadioButton4.setSelected(true);
jRadioButton4.setText("Null");
jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
jButton2.setText("help");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jRadioButton4))
.addGap(0, 8, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(0, 84, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
.addComponent(jTextField1))
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String str = jTextField1.getText();
jTextArea1.append(str+"\n");
jTextField1.setText("");
if(send(str)==0)
try{
socket.close();
}catch(IOException e){System.out.println(e.getMessage());}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String str = jTextField1.getText();
jTextArea1.append(str+"\n");
jTextField1.setText("");
if(send(str)==0)
try{
socket.close();
}catch(IOException e){System.out.println(e.getMessage());}
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
f.delete();
send("/quit");
}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("//Angry! @");
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("//Greet! @");
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("//Goodbye! @");
}
private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JDialog dialog = new JDialog();
dialog.setTitle("help");
dialog.setSize(450,170);
Container contentPane = dialog.getContentPane();
contentPane.add(new JLabel(
"<html>/login xxx: login and use 'xxx' as username.<br/>"
+ "/who        : show all online users.<br/>"
+ "/history   : show history.<br/>"
+ "/quit        : quit this chatroom.<br/><br/>"
+ "click on the right RadioButton for send emotins or send '//XXX@xxx'.<html>"
,JLabel.CENTER),BorderLayout.CENTER);
dialog.setVisible(true);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChatroomJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatroomJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatroomJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatroomJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
final ChatroomJFrame chatroomJFrame=new ChatroomJFrame();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
chatroomJFrame.setVisible(true);
}
});
chatroomJFrame.jPanel1.setVisible(true);
chatroomJFrame.connect();
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
ReveiveThread1.java
package chatroom.client;
import java.io.*;
import java.net.*;
/**
*
* @author lqshanshuo
*/
class ReceiveThread1 extends Thread{
private ChatroomJFrame chatroomJFrame;
private Socket socket;
private PrintWriter history;
private DataInputStream dis;
public ReceiveThread1(ChatroomJFrame c,Socket s,PrintWriter h) throws IOException{
chatroomJFrame = c;
socket = s;
history = h;
dis = new DataInputStream(socket.getInputStream());
start();
}
public void run(){
try{
while(true){
String strTemp=dis.readUTF();
chatroomJFrame.receive(strTemp);
history.println(strTemp);
history.flush();
}
}catch(IOException e){
//System.out.println("ReceiveThread is over.");
}
}
}