实例介绍
【实例截图】
【核心代码】
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.*; import java.net.*; import javax.swing.*; public class Socket10 extends JFrame implements Runnable, ActionListener{ public Panel leftPanel; public Panel rightPanel; public Panel lastPanel; public Button button1= new Button( "创建服务器" ),button2,button3,button4; public TextArea text1,text2,text3,text4; public Label label1,label2,label3,label4; private TextField textfield1,textfield2,textfield3,textfield4; Thread inithread; String IPserver= null ; String nameserver= null ; String strin= null ; String Address = "44444" ; String name = "44444" ; String s= null ; String Stringss= null ; String Strname= null ; int scan= 2 ; int lock= 0 ; //Stringarray soctr; //Stringarray soctr1; ServerSocket server= null ; Socket you= null ; DataInputStream in= null ,in1= null ; DataOutputStream out= null ,out1= null ; private Socket mysocket; ////////////////////////////////////得到主机名字////////////////////////////////////////////////////// public static String getname(String s) { try { InetAddress myip=InetAddress.getLocalHost(); s=myip.getHostName(); } catch (IOException e2) { } return s; } ////////////////////////////////////得到主机IP////////////////////////////////////////////////////////// public static String getip(String s) { try { InetAddress myip=InetAddress.getLocalHost(); s=myip.getHostAddress(); } catch (IOException e2) { } return s; } ///////////////////////////////////////创建主界面///////////////////////////////////////// private void launchFrame() { label1= new Label( "IP地址" ); //"IP地址" label2= new Label( "本机名称" ); textfield1= new TextField(getip(Address), 19 ); textfield2= new TextField(getname(name), 19 ); textfield3= new TextField( 14 ); textfield4= new TextField( 14 ); text1= new TextArea( 20 , 68 ); text2= new TextArea( 3 , 68 ); text3= new TextArea( 14 , 25 ); text4= new TextArea( 9 , 25 ); button1= new Button( "创建服务器" ); button2= new Button( "搜索服务器" ); button3= new Button( "发送" ); Container c=getContentPane(); c.setLayout( new BorderLayout()); Panel firstPanel= new Panel(); Panel leftPanel= new Panel(); Panel rightPanel= new Panel(); Panel subleftPanel1= new Panel(); Panel subleftPanel2= new Panel(); Panel lastPanel= new Panel(); Panel subrightPanel1= new Panel(); Panel subrightPanel2= new Panel(); leftPanel.setLayout( new BorderLayout()); rightPanel.setLayout( new BorderLayout()); firstPanel.add(label1); firstPanel.add(textfield1); firstPanel.add(label2); firstPanel.add(textfield2); subleftPanel1.add(text1); subleftPanel2.add(text2); subrightPanel1.add(text3); subrightPanel2.add(text4); leftPanel.add(subleftPanel1,BorderLayout.NORTH); leftPanel.add(subleftPanel2,BorderLayout.SOUTH); rightPanel.add(subrightPanel1,BorderLayout.NORTH); rightPanel.add(subrightPanel2,BorderLayout.SOUTH); firstPanel.add(button1); firstPanel.add(button2); lastPanel.add(button3); c.add(firstPanel,BorderLayout.NORTH); c.add(leftPanel,BorderLayout.CENTER); c.add(rightPanel,BorderLayout.EAST); c.add(lastPanel,BorderLayout.SOUTH); this .pack(); setSize( 700 , 500 ); setTitle( "聊天HD" ); setVisible( true ); Window fream; setResizable( false ); } ////////////////////////////////////////////////////////////////////////////////////////////////////// private void addWindowListener(WindowAdapter windowAdapter) throws IOException { // TODO Auto-generated method stub if (scan== 0 ) { out.writeUTF( "endover" ); you.close(); System.exit( 0 ); } else if (scan== 1 ) { out1.writeUTF( "endover" ); mysocket.close(); System.exit( 0 ); } } /////////////////////////////////////////////开启服务///////////////////////////////// private void startServer() { try { button1.setLabel( "停止服务器" ); ServerSocket server= new ServerSocket( 7028 ); you=server.accept(); } catch (Exception e) { text3.setText( "" ); text3.append( "服务器启动错误,请重新设置后启动!\n可能是由于:\n" ); text3.append( "1.端口被占用。\n" ); text3.append( "2.服务器已经启动。\n" ); e.printStackTrace(); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////// private void StartClient() { try { String IP=textfield1.getText(); Socket mysocket; mysocket= new Socket(IP, 7028 ); in1= new DataInputStream(mysocket.getInputStream()); out1= new DataOutputStream(mysocket.getOutputStream()); text4.append( "连接到服务器!\n" ); nameserver=textfield2.getText(); out1.writeUTF( "begin" nameserver); button2.setLabel( "退出服务器" ); //mysocket.close(); } catch (Exception e) { text4.append( "无法连接网络!\n" ); } } ///////////////////////////////////////////////////////////////////////////////////////////////////// public void Message() { button1.addActionListener( this ); button2.addActionListener( this ); button3.addActionListener( this ); } /////////////////////////////////////////////////////////////////////////////////////////////////// public void actionPerformed(ActionEvent e) { ////////////////////////////////////////////////////////////////////////////////////////////// if (e.getSource()==button1) { if (lock== 0 ) { IPserver=getip(IPserver); text3.setText( "" ); text3.append( "服务器IP地址:" IPserver "\n" ); text3.append( "服务器名称:" textfield2.getText() "\n" ); textfield1.setText(getip(nameserver)); text3.append( "端口号:7028" "\n" ); text3.append( "服务器已启动,正在监听......" "\n" ); // button1.setName("停止服务器"); this .startServer(); scan= 0 ; lock= 1 ; } else { try { out= new DataOutputStream(you.getOutputStream()); out.writeUTF( "endover" ); text3.append( "服务器已关闭!" "\n" ); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); text3.append( "服务器关闭出现问题,请稍后重试!" "\n" ); } } } //////////////////////////////////////////////////////////////////////////////////////////// else if (e.getSource()==button3) { if (scan== 0 ) { //String IPserver=null; String nameserver= null ; try { //in=new DataInputStream(you.getInputStream()); out= new DataOutputStream(you.getOutputStream()); //IPserver=getip(IPserver); nameserver=textfield2.getText(); if (text2.getText().length()> 0 ) { text1.append( "---" nameserver "---说: \n" text2.getText() "\n" ); out.writeUTF(text2.getText()); text2.setText( "" ); } else { text2.setText( "\n\n 无输入内容!" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } text2.setText( "" ); } //text1.append("---" nameserver "---说:11 \n" text2.getText() "\n"); } catch (IOException e1) { // TODO Auto-generated catch block //text1.append("---" nameserver "---说: \n" text2.getText() "\n"); e1.printStackTrace(); } } else if (scan== 1 ) { try { //in1=new DataInputStream(mysocket.getInputStream()); //out1=new DataOutputStream(mysocket.getOutputStream()); nameserver=textfield2.getText(); if (text2.getText().length()> 0 ) { text1.append( "---" nameserver "---说: \n" text2.getText() "\n" ); out1.writeUTF(text2.getText()); text2.setText( "" ); } else { text2.setText( "\n\n 无输入内容!" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } text2.setText( "" ); } //text1.append("---" nameserver "---说: 22 \n" text2.getText() "\n"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { text2.setText( "" ); text2.append( "\n\n 网络未连接!!!" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } text2.setText( "" ); } } ////////////////////////////////////////////////////////////////////////////////////////////////////// else if (e.getSource()==button2) { if (lock== 0 ) { text4.setText( "" ); text4.append( "开始搜索服务器,请稍后......\n" ); scan= 1 ; // button2.setName("退出服务器"); this .StartClient(); lock= 1 ; } else try { out= new DataOutputStream(mysocket.getOutputStream()); out.writeUTF( "endover" ); text4.append( "已退出登录!\n" ); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); text4.append( "退出连接失败,请稍后重试!\n" ); } //text4.append("已连接服务器,请勿重复连接!\n"); } } /////////////////////////////////////////////////////////////////////////////////////////////// public void MessageSaR() throws InterruptedException, IOException { if (scan== 0 ) { try { //接收消息 //InputStreamReader input=new InputStreamReader(you.getInputStream()); //BufferedReader read=new BufferedReader(input); in= new DataInputStream(you.getInputStream()); out= new DataOutputStream(you.getOutputStream()); String recvier=in.readUTF(); //String Stringss=null; //String Strname=null; //Stringss=recvier.substring(0,5); if (recvier.length()> 4 ) { Stringss=recvier.substring( 0 , 5 ); if (Stringss.equals( "begin" )&&recvier.length()> 4 ) { Strname=recvier.substring( 5 ); text3.append( "客户机-" Strname "-已登录\n" ); out.writeUTF( "conna" textfield2.getText()); //Thread.sleep(200); //out.writeUTF("conip" textfield1.getText()); //text1.append("---" Strname "---说: \n" recvier "\n"); //text1.append("conip" textfield1.getText() "\n"); } if (Stringss.equals( "endov" )&&recvier.length()> 4 ) { text3.append( "客户机-" Strname "-已退出\n" ); you.close(); } } if (recvier.length()< 5 ) text1.append( "---" Strname "---说: \n" recvier "\n" ); if (!Stringss.equals( "begin" )){ text1.append( "---" Strname "---说: \n" recvier "\n" ); } //else text1.append("---" Strname "---说: \n" recvier "\n"); } catch (Exception e) { e.printStackTrace(); } //Thread.sleep(100); } else if (scan== 1 ) { try { //接收消息 //text1.append("客户端:\n"); //DataInputStream in=null; //DataOutputStream out=null; //mysocket=new Socket("localhost",7028); //in1=new DataInputStream(mysocket.getInputStream()); //out=new DataOutputStream(mysocket.getOutputStream()); //text1.append("客户端2:\n"); String recvier=in1.readUTF(); //text1.append("客户端3:\n"); //if(Stringss.equals("conna")) //{ // text4.append("服务器名称:" Strname "\n"); //text1.append("客户端4:" recvier "\n"); //} if (recvier.length()> 4 ) { Stringss=recvier.substring( 0 , 5 ); if (Stringss.equals( "conna" )&&recvier.length()> 4 ) { Strname=recvier.substring( 5 ); text4.append( "服务器名称:" Strname "\n" ); //text1.append("客户端4:" recvier "\n"); } if (Stringss.equals( "endov" )&&recvier.length()> 4 ) { text4.append( "服务器-" Strname "-已关闭\n" ); //out1.writeUTF("endover"); mysocket.close(); } } //Stringss=recvier.substring(0,5); //text1.append("客户端1:" recvier "\n");u if (recvier.length()< 5 ) text1.append( "---" Strname "---说: \n" recvier "\n" ); if (!Stringss.equals( "conna" )){ text1.append( "---" Strname "---说: \n" recvier "\n" ); } //else //else if(Stringss.equals("conip")) //{ // Stringss=recvier.substring(5); // text4.append("服务器IP:" Stringss); // text1.append("客户端5:" recvier "\n"); //} //text1.append("---" Strname "---说: \n" recvier "\n"); } catch (Exception e) { e.printStackTrace(); } //Thread.sleep(100); //mysocket.close(); } } /////////////////////////////////////////////////////////////////////////////////////// public static void main(String args[]) throws InterruptedException, IOException { Socket10 aa= new Socket10(); aa.launchFrame(); aa.Message(); while ( true ) { aa.MessageSaR(); } } @Override public void run() { // TODO Auto-generated method stub } } |
标签: 聊天
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论