以前用linux c实现过简单的聊天程序,但毕竟用linux的人不多,只能自己ping自己。最近用java实现了,其中主要用到了java的网络编程,GUI编程,IO编程。整个程序总共有三个线程,线程1为界面程序+客户端,线程2为服务器,线程3为接收发来消息的方法。程序启动后,线程1启动,随之线程2启动,当建立连接后,线程3启动。当然这只是最初的版本,有待改进。以后最重要的改进还是对于多人聊天的并发处理。此程序用法很简单,内网和外网都适用,(端口号我设定的是1080,一些用路由器的朋友要把自己的内网ip映射到外网ip,具体做法参考下方有图示), 不多废话啦,看程序吧。。。

 

先看两张截图:

 

路由器分配的为内网IP(192.168.1.XXX),如果想被外网IP访问,必须要把你的内网IP映射到外网IP。(如果想访问别人,则无需设置)首先登陆你的路由器设置页面(浏览器中输入192.168.1.1),然后选择转发规则进行设置:端口号设置为1080,IP设置为本机的内网IP。

一下为程序源代码,注释有点少,有疑问或者更好建议的朋友请给我留言。


Code:

1. import
2.   
3. import
4. import
5. import
6. import
7.   
8. import
9. import
10. import
11. import
12. import
13. import
14.   
15.   
16. public class
17. public static void
18. new
19. new
20.     }  
21. }  
22.   
23. class Recv implements
24. new
25. public void
26. try
27. new ServerSocket(1080);  
28.             Socket s = ss.accept();  
29.             inf.connectInformation(s);    
30.               
31. catch
32.             e.printStackTrace();  
33.         }  
34.   
35.     }  
36. }  
37.   
38. class InterFace implements
39. //  网络连接的套接字
40. null;  
41. null;  
42. null;  
43.       
44. //  聊天界面的顶层容器
45. new JFrame("Seek You");  
46. //  聊天界面的顶部
47. new
48. new JLabel("Input His/Her IP: ");  
49. final JTextField tf = new JTextField(20);  
50. new JButton("Connect");      
51. //  聊天界面的中部
52. new
53. new
54.       
55. //  聊天界面的底部
56. new
57. new JTextField(40);  
58. new JButton("Send");    
59.   
60.   
61.     InterFace()  
62.     {  
63.           
64. //      聊天界面的顶层容器
65. 600,400);  
66. 340,160);  
67. true);  
68.           
69. //      聊天界面的顶部
70. new
71. public void
72. if( e.getKeyChar() == KeyEvent.VK_ENTER){  
73.                     bt.doClick();  
74.                       
75.                 }  
76.              }     
77.         });  
78.         p.add(l);  
79.         p.add(tf);  
80.         p.add(bt);  
81.           
82.         jf.getContentPane().add(p,BorderLayout.NORTH);  
83. //      添加indowListener
84. new
85. public void
86. try
87. if(is != null)  
88.                         is.close();  
89. if(os != null)  
90.                         os.close();  
91. if(sock != null)  
92.                         sock.close();  
93. catch
94.                     e1.printStackTrace();  
95.                 }  
96. 0);   
97.             }  
98.         });  
99. //      聊天界面的中部
100.         ta.setBackground(Color.gray);  
101.         ta.setForeground(Color.white);  
102. //  js.add(ta);
103.         jf.getContentPane().add(ta,BorderLayout.CENTER);  
104.   
105. //      聊天界面的底部
106.           
107. new
108. public void
109. if( e.getKeyChar() == KeyEvent.VK_ENTER){  
110.                     btt.doClick();  
111.                       
112.                 }  
113.              }     
114.         });  
115.         p2.add(tff);  
116.         p2.add(btt);  
117.         jf.getContentPane().add(p2,BorderLayout.SOUTH);  
118.       
119.           
120.           
121.           
122. //      实现Send的侦听
123. new
124. public void
125. if(sock == null){  
126. "line.separator");  
127. "");  
128. "您还没有建立连接");  
129.                     ta.append(line);  
130.                 }  
131. else{  
132.                     String s = tff.getText();  
133. "");  
134. try
135. "line.separator");  
136.                         os.write(s.getBytes());  
137. "Me: "+s);  
138.                         ta.append(line);  
139. catch
140.                         e1.printStackTrace();  
141.                     }  
142.                 }  
143.             }  
144.         });  
145.           
146. //      实现connect的监听
147. new
148. public void
149. if(sock ==null){  
150. try
151. new Socket(tf.getText(),1080);  
152.                             is = sock.getInputStream();  
153.                             os = sock.getOutputStream();  
154. "line.separator");  
155. "您已经和"+sock.getInetAddress().getHostName()+"建立连接");  
156.                             ta.append(line);  
157. //启动接收信息的进程    
158.                         }  
159. catch
160. "line.separator");  
161. "连接失败,请重新输入IP进行连接");  
162.                         ta.append(line);  
163.                         e1.printStackTrace();  
164.                     }     
165.                       
166. else{  
167. "line.separator");  
168. "您已经和"+sock.getInetAddress().getHostName()+"建立连接,");  
169. "重新启动可以建立其他连接");  
170.                     ta.append(line);  
171.                 }  
172.             }  
173.               
174.         });  
175.     }  
176.       
177. final void
178. new Thread(this).start();  
179.     }  
180. void
181. if(sock==null){  
182.             sock = s;  
183. "line.separator");  
184. "已经连接到你的电脑");  
185.               
186.             ta.append(line);      
187. "");  
188.             ta.append(line);      
189. try
190.                 is = s.getInputStream();  
191.                 os = s.getOutputStream();  
192. //启动接收信息的进程
193. catch
194.                 e.printStackTrace();  
195.             }  
196.         }  
197. else{  
198. "line.separator");  
199. "正在连接到你的电脑...");  
200. "但您已经和"+sock.getInetAddress().getHostName()+"建立连接");  
201.             ta.append(line);  
202. "");       
203.             ta.append(line);      
204.         }  
205.       
206.     }  
207. /*
208.     void wordclear(){
209.         String   input = ta.getText();
210.         String[]   regStr   = input.split( "line.separator") ; 
211.  
212.         int   i   =  0;
213.         for(String s : regStr){i++;}
214.         if(i==14){
215.             ta.setText("");
216.         }
217.     }
218.     */
219.   
220. public void
221. //  new Thread(new WordClear(ta)).start();//开启文本域字数控制线程
222. while(true){  
223. if(is!=null){  
224. "line.separator");  
225. byte[] buf = new byte[100];  
226. try
227. int
228. if(len != 0){  
229. "You: "+new String(buf, 0, len));  
230.                         ta.append(line);  
231.                     }     
232. catch
233. // TODO Auto-generated catch block
234.                     e.printStackTrace();  
235.                 }  
236.             }  
237.         }  
238.     }  
239.       
240. }  
241.   
242. /*
243. class WordClear implements Runnable{
244.     TextArea ta;
245.     WordClear(TextArea ta){
246.         this.ta = ta;
247.     }
248.  
249.     String ss;
250.     public void run(){
251.             while(true){
252.                 System.out.println("!!!!!!!!!");
253.                 String   input = ta.getText();
254.                 String[]   regStr   = input.split( "/n") ; 
255.                 
256.                 int   i   =  0;
257.                 for(String s : regStr){
258.                     i++;
259.                     if(i>16){
260.                         ss = s+"/n";
261.                     }
262.                 }
263.                 if(i==17){
264.                     try {
265.                         ta.setText(ss);
266.                     } catch (Exception e) {
267.                         e.printStackTrace();
268.                     }
269.                 
270.                     
271.                 }
272.             }
273.     }
274. }
275. */