好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java聊天室的实现代码

本文实例为大家分享了 java 实现 聊天室 的具体代码,供大家参考,具体内容如下

聊天室界面:

源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

public class clientframe extends frame {

 

  private textfield textfieldcontent = new textfield();

  private textarea textareacontent = new textarea();

  private socket socket = null ;

  private outputstream out = null ;

  private dataoutputstream dos = null ;

  private inputstream in = null ;

  private datainputstream dis = null ;

  private boolean flag = false ;

 

  /**

   * 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午9:19:51 功能:启动客户端程序

   *

   * @param args

   */

  public static void main(string[] args) {

   new clientframe().init();

  }

 

  /**

   * 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午9:20:43 功能:对窗口进行初始化

   */

  private void init() {

   this .setsize( 300 , 300 );

   setlocation( 250 , 150 );

   setvisible( true );

   settitle( "wechatroom" );

 

   // 添加控件

   this .add(textareacontent);

   this .add(textfieldcontent, borderlayout.south);

   textareacontent.setfocusable( false );

   pack();

 

   // 关闭事件

   addwindowlistener( new windowadapter() {

    public void windowclosing(windowevent e) {

     system.out.println( "用户试图关闭窗口" );

     disconnect();

     system.exit( 0 );

    }

 

   });

   // textfieldcontent添加回车事件

   textfieldcontent.addactionlistener( new actionlistener() {

    public void actionperformed(actionevent e) {

     onclickenter();

    }

   });

 

   // 建立连接

   connect();

   new thread( new recivemessage()).start();

  }

 

  private class recivemessage implements runnable {

   @override

   public void run() {

    flag = true ;

    try {

     while (flag) {

      string message = dis.readutf();

      textareacontent.append(message + "\n" );

     }

    } catch (eofexception e) {

     flag = false ;

     system.out.println( "客户端已关闭" );

     // e.printstacktrace();

    } catch (socketexception e) {

     flag = false ;

     system.out.println( "客户端已关闭" );

     // e.printstacktrace();

    } catch (ioexception e) {

     flag = false ;

     system.out.println( "接受消息失败" );

     e.printstacktrace();

    }

   }

 

  }

 

  /**

   * 功能:当点击回车时出发的事件 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午9:49:30

   */

  private void onclickenter() {

   string message = textfieldcontent.gettext().trim();

   if (message != null && !message.equals( "" )) {

    string time = new simpledateformat( "h:m:s" ).format( new date());

    textareacontent.append(time + "\n" + message + "\n" );

    textfieldcontent.settext( "" );

    sendmessagetoserver(message);

   }

  }

 

  /**

   * 功能:给服务器发送消息 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:13:48

   *

   * @param message

   */

  private void sendmessagetoserver(string message) {

   try {

    dos.writeutf(message);

    dos.flush();

   } catch (ioexception e) {

    system.out.println( "发送消息失败" );

    e.printstacktrace();

   }

  }

 

  /**

   * 功能:申请socket链接 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:00:38

   */

  private void connect() {

   try {

    socket = new socket( "localhost" , 8888 );

    out = socket.getoutputstream();

    dos = new dataoutputstream(out);

    in = socket.getinputstream();

    dis = new datainputstream(in);

   } catch (unknownhostexception e) {

    system.out.println( "申请链接失败" );

    e.printstacktrace();

   } catch (ioexception e) {

    system.out.println( "申请链接失败" );

    e.printstacktrace();

   }

  }

 

  /**

   * 功能:关闭流和链接 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:01:32

   */

  private void disconnect() {

   flag = false ;

   if (dos != null ) {

    try {

     dos.close();

    } catch (ioexception e) {

     system.out.println( "dos关闭失败" );

     e.printstacktrace();

    }

   }

   if (out != null ) {

    try {

     out.close();

    } catch (ioexception e) {

     system.out.println( "dos关闭失败" );

     e.printstacktrace();

    }

   }

   if (socket != null ) {

    try {

     socket.close();

    } catch (ioexception e) {

     system.out.println( "socket关闭失败" );

     e.printstacktrace();

    }

    ;

   }

  }

 

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

package com.chat;

 

import java.io.datainputstream;

import java.io.dataoutputstream;

import java.io.eofexception;

import java.io.ioexception;

import java.io.inputstream;

import java.io.outputstream;

import java.net.bindexception;

import java.net.serversocket;

import java.net.socket;

import java.net.socketexception;

import java.util.arraylist;

import java.util.list;

 

public class chatserver {

 

  private list<client> clients = new arraylist<>();

 

  /**

   * 功能:启动chatsever 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:26:41

   *

   * @param args

   */

  public static void main(string[] args) {

   new chatserver().init();

  }

 

  /**

   * 功能:对chatserver初始化 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午10:27:09

   */

  private void init() {

   system.out.println( "服务器已开启" );

   // bindexception

 

   serversocket ss = null ;

   socket socket = null ;

   try {

    ss = new serversocket( 8888 );

   } catch (bindexception e) {

    system.out.println( "端口已被占用" );

    e.printstacktrace();

   } catch (ioexception e1) {

    e1.printstacktrace();

   }

   try {

    client client = null ;

    while ( true ) {

     socket = ss.accept();

     system.out.println( "客户驾到" );

     client = new client(socket);

     clients.add(client);

     new thread(client).start();

    }

   } catch (ioexception e) {

    e.printstacktrace();

   }

  }

 

  private class client implements runnable {

   private socket socket = null ;

   inputstream in = null ;

   datainputstream din = null ;

   outputstream out = null ;

   dataoutputstream dos = null ;

   boolean flag = true ;

 

   public client(socket socket) {

    this .socket = socket;

    try {

     in = socket.getinputstream();

     din = new datainputstream(in);

    } catch (ioexception e) {

     system.out.println( "接受消息失败" );

     e.printstacktrace();

    }

 

   }

 

   public void run() {

 

    string message;

    try {

     while (flag) {

      message = din.readutf();

      // system.out.println("客户说:" + message);

      forwordtoallclients(message);

     }

    } catch (socketexception e) {

     flag = false ;

     system.out.println( "客户下线" );

     clients.remove( this );

     // e.printstacktrace();

    } catch (eofexception e) {

     flag = false ;

     system.out.println( "客户下线" );

     clients.remove( this );

     // e.printstacktrace();

    } catch (ioexception e) {

     flag = false ;

     system.out.println( "接受消息失败" );

     clients.remove( this );

     e.printstacktrace();

    }

 

    if (din != null ) {

     try {

      din.close();

     } catch (ioexception e) {

      system.out.println( "din关闭失败" );

      e.printstacktrace();

     }

    }

    if (in != null ) {

     try {

      in.close();

     } catch (ioexception e) {

      system.out.println( "din关闭失败" );

      e.printstacktrace();

     }

    }

    if (socket != null ) {

     try {

      socket.close();

     } catch (ioexception e) {

      system.out.println( "din关闭失败" );

      e.printstacktrace();

     }

    }

 

   }

 

   /**

    * 功能:转发给所有客户端 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午11:11:59

    *

    * @param message

    * @throws ioexception

    */

   private void forwordtoallclients(string message) throws ioexception {

    for (client c : clients) {

     if (c != this ) {

      out = c.socket.getoutputstream();

      dos = new dataoutputstream(out);

      forwordtoclient(message);

     }

    }

   }

 

   /**

    * 功能:发送给一个客户端 学校:山东师范大学 程序员:外力_victor 日期:2016年5月8日 上午11:16:12

    *

    * @throws ioexception

    */

   private void forwordtoclient(string message) throws ioexception {

    dos.writeutf(message);

    dos.flush();

    system.out.println( "转发成功!" );

   }

 

  }

}

源码下载: java聊天室代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_24082497/article/details/51347426

查看更多关于java聊天室的实现代码的详细内容...

  阅读:50次