文档库 最新最全的文档下载
当前位置:文档库 › 基于ANDROID的socket聊天室服务器

基于ANDROID的socket聊天室服务器

基于ANDROID的socket聊天室服务器
基于ANDROID的socket聊天室服务器

package com.Server;

import java.io.*;

import https://www.wendangku.net/doc/0613358.html,.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.*;

import javax.swing.JOptionPane;

public class Server {

ServerSocket ss = null;

private String getnameString=null;

boolean started = false;

List clients = new ArrayList();

public static void main(String[] args) {

String inputport = JOptionPane.showInputDialog("请输入服务器所用的端口:");

int port = Integer.parseInt(inputport);

new Server().start(port);

}

public void start(int port) {

try {

ss = new ServerSocket(port);

System.out.println("服务器启动");

started = true;

} catch (BindException e) {

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

System.exit(0);

}

catch (IOException e) {

e.printStackTrace();

}

try {

while (started) {

Socket s = ss.accept();

Client c = new Client (s);

System.out.println("a client is connected");

new Thread(c).start();

clients.add(c);

}

} catch (IOException e) {

e.printStackTrace();

}

finally {

try {

ss.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

public List getClient(){

return clients;

}

class Client implements Runnable{

private Socket s = null;

private DataInputStream dis = null;

private DataOutputStream dos = null;

private boolean bConnected = false;

private String sendmsg=null;

Client (Socket s) {

this.s = s;

try {

dis = new DataInputStream (s.getInputStream());

dos = new DataOutputStream (s.getOutputStream());

bConnected = true;

} catch(IOException e) {

e.printStackTrace();

}

}

public void send (String str) {

try {

dos.writeUTF(str+"");

dos.flush();

} catch(IOException e) {

clients.remove(this);

}

}

public void run() {

try {

while (bConnected) {

String str=dis.readUTF();

DateFormat df = new SimpleDateFormat("MM-dd hh:mm");

String date = " ["+df.format(new Date())+"]";

if(str.startsWith("online:")){

getnameString = str.substring(7);

for (int i=0; i

Client c = clients.get(i);

c.send(getnameString+" on line."+date);

}

System.out.println(getnameString+" on line."+date);

}else if(str.startsWith("offline:")){

getnameString = str.substring(8);

clients.remove(this);

for (int i=0; i

Client c = clients.get(i);

c.send(getnameString+" off line."+date);

}

System.out.println(getnameString+" off line."+date);

}

else{

int charend = str.indexOf("end;");

String chatString = str.substring(charend+4);

String chatName = str.substring(5, charend);

sendmsg=chatName+date+"\n"+chatString;

for (int i=0; i

Client c = clients.get(i);

c.send(sendmsg);

}

System.out.println(sendmsg);

}

}

} catch (SocketException e) {

System.out.println("a client is closed!");

clients.remove(this);

} catch (EOFException e) {

System.out.println("a client is closed!");

clients.remove(this);

}

catch (IOException e) {

e.printStackTrace();

}

finally {

try {

if (dis != null) dis.close();

if (dos != null) dos.close();

if (s != null) s.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

相关文档