现在的位置: 首页 > 综合 > 正文

ChatServer.java

2013年09月02日 ⁄ 综合 ⁄ 共 732字 ⁄ 字号 评论关闭

import java.io.*;
import java.net.*;

public class ChatServer {

 public static void main(String[] args) {
   boolean started =false;
   ServerSocket ss =null;
   Socket s=null;
   try {
   ss = new ServerSocket(8888);
   }catch (IOException e) {
   e.printStackTrace();
  }
   
   try{
   
   started =true;
   while(started) {
    boolean bConnected =false;
     s = ss.accept();
System.out.println("a client connected!");
    bConnected = true;
    DataInputStream dis = new DataInputStream(s.getInputStream());
    
    while(bConnected){
    
    String str = dis.readUTF();
    System.out.println(str);
    }
    dis.close();
    
   }
  } catch (IOException e) {
   try {
    s.close();
    System.out.println("客户端已经离开");
   } catch (IOException e1) {
    e1.printStackTrace();
   }
  // e.printStackTrace();
  }
 }

}

【上篇】
【下篇】

抱歉!评论已关闭.