java实现简单聊天软件

时间:2022-11-07 16:26:12

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

介绍

这是我自己学习socket时写的,用到了多线程,socket的知识,还有java的io技术,并且做了一个java的ui,废话不多说,先上效果图

java实现简单聊天软件

sendthread

这个类是用来实现数据的发送的,当按键按下时,会调用sendmessage方法

?
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
public class sendthread {
 
 chatwindow chatwindow;
 private string remoteip = "";
 private int port = 0;
 private string message = "";
 
 public sendthread(int port,chatwindow window) {
  chatwindow = window;
 }
 
 public void notrun() {
  inetsocketaddress isa = new inetsocketaddress(remoteip, port);
  try {
   socket socket = new socket();
   socket.connect(isa);
   outputstreamwriter writer = new outputstreamwriter(socket.getoutputstream());
   writer.write(message);
   writer.flush();
   writer.close();
   system.out.println("将数据写入到流中");
  } catch (ioexception e) {
   e.printstacktrace();
  }finally{
   message = "";
  }
 }
 
 public void senmessage(string host,int port,string message){
  remoteip = host;
  this.port = port;
  this.message = message;
  notrun();
 }
 
}

receivethread

这个类实现的功能是维护一个阻塞线程,来对端口进行监听,接受收到的信息,每当收到信息时,都会显示。

?
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
public class receivethread extends thread {
 
 private serversocket server;
 private chatwindow window;
 
 public receivethread(chatwindow window) {
  this.window = window;
  try {
   server = new serversocket(0);
   window.setlocalport(server.getlocalport());
   start();
  } catch (ioexception e) {
   // todo auto-generated catch block
   window.printerror("连接出错");
  }
 }
 
 @override
 public void run() {
  // todo auto-generated method stub
  while(true){
   socket socket;
   try {
    socket = server.accept();
    inputstreamreader reader = new inputstreamreader(socket.getinputstream());
    int c;
    stringbuilder sb = new stringbuilder();
    while((c = reader.read()) != -1){
     sb.append((char)c);
    }
    window.setreceive(sb.tostring());
   } catch (ioexception e) {
    // todo auto-generated catch block
    system.out.println("线程将接受到的数据写入对话框出错");
   }
  }
 }
}

chatwindow

这个类是ui的实现,通过绑定各个按钮的操作来实现别的功能

?
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
public class chatwindow extends jframe {
 
 private jbutton sendbutton;
 private jbutton cancelbutton;
 private jbutton receivebutton;
 private jtextarea receivetext;
 private jtextfield sendtext;
 
 private jlabel localport;
 
 private jtextfield remoteaddress;
 private jtextfield remoteport;
 private sendthread sendthread;
 private receivethread receivethread;
 
 private void guiini(){
  container c = this.getcontentpane();
  c.setlayout(new borderlayout());
  setsize(400, 500);
 
  //第一个panel
  jpanel panel1 = new jpanel(new gridlayout(4, 2));
//  jpanel panel1 = new jpanel(new (4, 2));
  panel1.setsize(400, 75);
  panel1.add(new jlabel("当前机器的ip地址是:"));
  try {
   panel1.add(new jlabel(inetaddress.getlocalhost().gethostaddress()));
  } catch (unknownhostexception e) {
   // todo auto-generated catch block
   panel1.add(new jlabel("当前机器的ip地址是:" + "unknown"));
  }
  panel1.add(new jlabel("当前对话的端口是:"));
  panel1.add(localport = new jlabel(""+0));
  panel1.add(new jlabel("remote host ip address:"));
  remoteaddress = new jtextfield();
  remoteaddress.setcolumns(0);
  panel1.add(remoteaddress);
  panel1.add(new jlabel("remote host port:"));
  remoteport = new jtextfield();
  remoteport.setcolumns(0);
  panel1.add(remoteport);
  c.add(panel1,borderlayout.north);
 
 
  //第二个panel
  jpanel panel2 = new jpanel(new flowlayout(flowlayout.leading));
  panel2.setsize(400,200);
  panel2.add(new jlabel("收到的内容"));
  receivetext = new jtextarea(15,30);
  receivetext.seteditable(false);
  receivetext.setautoscrolls(true);
  jscrollpane jsp = new jscrollpane(receivetext);
  panel2.add(jsp);
  //第三个panel
//  jpanel panel3 = new jpanel(new gridlayout(2, 1));
  panel2.add(new jlabel("请输入发送的内容"));
  sendtext = new jtextfield(30);
  sendtext.setautoscrolls(true);
  panel2.add(sendtext);
  c.add(panel2,borderlayout.center);
  //c.add(panel3);
  //第四个panel
  jpanel panel4 = new jpanel(new gridlayout(1, 0));
  panel4.setsize(400,20);
  receivebutton = new jbutton("开始接受数据");
  sendbutton = new jbutton("发送");
  cancelbutton = new jbutton("取消");
  panel4.add(receivebutton);
  panel4.add(cancelbutton);
  panel4.add(sendbutton);
  c.add(panel4,borderlayout.south);
  //四个面板内容设置完毕
 
  setvisible(true);
  setresizable(false);
  setdefaultcloseoperation(exit_on_close);
 }
 
 private void actionini() {
 
  //键盘动作
  this.addkeylistener(new keylistener() {
 
   @override
   public void keytyped(keyevent e) {
 
   }
 
   @override
   public void keyreleased(keyevent e) {
    // todo auto-generated method stub
    if(e.getkeycode() == keyevent.vk_enter){
     //发送文本
     sendthread.senmessage(remoteaddress.gettext(), integer.parseint(remoteport.gettext()), sendtext.gettext());
     receivetext.settext(receivetext.gettext() + "\n" + "发送:" + sendtext.gettext());
    }
   }
 
   @override
   public void keypressed(keyevent e) {
    // todo auto-generated method stub
 
   }
  });
  //取消按钮的动作
  cancelbutton.addmouselistener(new mouselistener() {
 
   @override
   public void mousereleased(mouseevent e) {
    // todo auto-generated method stub
    sendtext.settext("");
   }
 
   @override
   public void mousepressed(mouseevent e) {
    // todo auto-generated method stub
 
   }
 
   @override
   public void mouseexited(mouseevent e) {
    // todo auto-generated method stub
 
   }
 
   @override
   public void mouseentered(mouseevent e) {
    // todo auto-generated method stub
 
   }
 
   @override
   public void mouseclicked(mouseevent e) {
    // todo auto-generated method stub
 
   }
  });
  sendbutton.addmouselistener(new mouselistener() {
 
   @override
   public void mousereleased(mouseevent e) {
    //发送文本
    sendthread.senmessage(remoteaddress.gettext(), integer.parseint(remoteport.gettext()), sendtext.gettext());
    receivetext.settext(receivetext.gettext() + "发送:" + sendtext.gettext()+ "\n");
   }
 
   @override
   public void mousepressed(mouseevent e) {
    // todo auto-generated method stub
 
   }
 
   @override
   public void mouseexited(mouseevent e) {
    // todo auto-generated method stub
 
   }
 
   @override
   public void mouseentered(mouseevent e) {
    // todo auto-generated method stub
 
   }
 
   @override
   public void mouseclicked(mouseevent e) {
    // todo auto-generated method stub
 
   }
  });
 }
 
 private void threadini() {
  // todo auto-generated method stub
  sendthread = new sendthread(0, this);
  receivethread = new receivethread(this);
 }
 //构造函数
 public chatwindow() {
  guiini();
  actionini();
  threadini();
 }
 
 public void printerror(string err){
  system.out.println("error occur:" + err);
 }
 
 //回调函数,用于接受从线程中返回的数据
 public void setreceive(string receive){
  receivetext.settext(receivetext.gettext() + "收到:" + receive+ "\n" );
 }
 
 //当接受数据的线程开始工作以后,就调用该回调函数,设置当前聊天窗口使用的端口是哪个
 public void setlocalport(int localporttext){
  localport.settext(""+localporttext);
 }
 
 //放远,这个是整个程序的起点
 public static void main(string[] args) {
  // todo auto-generated method stub
  new chatwindow();
 }
 
}

如何使用

将工程导入到eclipse中以后,运行两次,因为这个程序不属于单例模式,所以可以出现两个窗口,在远程端口,远程地址那个输入框中分别输入另一个窗口中显示的ip和端口号,接下来在输入框中输入要发送的文字就可以了

源码下载:java实现简单聊天软件

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

原文链接:https://blog.csdn.net/im_zhq/article/details/51393012