📄 chatserver.java
字号:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.*;
public class ChatServer {
private ServerSocket ss;
public static Hashtable lclient = new Hashtable();
public static Writer flout;//kajal
String line;
public ChatServer( int port ) throws IOException {
try {
ss = new ServerSocket( port );
flout = new FileWriter("Output.txt");//kajal
System.out.println(" Server Statred at " + new Date() + "\n");
flout.write("Chat Server Started at " + new Date() + "\n \n");
flout.flush();
//*****************enter in jdbc********
line = "Chat Server Started at " + new Date();
//jdbtest.connmth(line);
connlog(line);
//*****************enter in jdbc********
}
catch (Exception e) {
//System.out.print( e.printStackTrace());
e.printStackTrace();
}
while (true) {
Socket s = ss.accept();
ChatHandler c = new ChatHandler (s);
c.start ();
}
}
public static void main(String[] args) throws Exception {
//int port = Integer.parseInt( args[0] );
int port = 2585;
new ChatServer( port );
}
Enumeration getOutputStreams() {
return lclient.elements();
}
public static void connlog (String line1)throws SQLException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String url = "jdbc:oracle:thin:@prophet.njit.edu:1521:course";
Connection conn = DriverManager.getConnection (url, "kbp27", "0iq50oAzY");
Statement stmt = conn.createStatement ();
//String command = "CREATE TABLE CHATSERVERLOG (logrecords varchar2(70))";
//stmt.executeUpdate(command);
String command = "INSERT INTO CHATSERVERLOG VALUES (" +"'"+ line1 +"')";
stmt.executeUpdate(command);
stmt.close();
conn.close();
}
}
class ChatHandler extends Thread {
protected Socket s;
protected ObjectInputStream i;
protected ObjectOutputStream o;
protected DataObject dO;
Boolean arun = false;
ChatServer cs;
//******************jdbc conn**************
String line;
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
String datetime = dateFormat.format(date);
//******************jdbc conn**************
//public static Hashtable lclient = new Hashtable();
boolean first = true;
public ChatHandler (Socket s){
this.s = s;
try{
i = new ObjectInputStream (s.getInputStream ());
o = new ObjectOutputStream (s.getOutputStream ());
//ChatServer.flout.write(s11);
this.arun = arun;
}catch(IOException e){
System.out.println("Couldn't create IO streams.");
}
}
protected static Vector handlers = new Vector ();
public void run () {
try {
handlers.addElement (this);
while (true) {
dO=(DataObject)i.readObject();
if(dO.getMessage().startsWith("@")){
ChatServer.lclient.put(this, dO.getName());
//ChatServer.flout.append(dO.getName()+" "+"Has Entered"+"\n");//kajal
try {
ChatServer.flout.append(dO.getName()+ " has entered at "+ datetime +" "+"\n");
ChatServer.flout.flush();
//*****************enter in jdbc********
line = dO.getName()+ " has entered at "+ datetime ;
//jdbtest.connmth(line);
ChatServer.connlog(line);
//*****************enter in jdbc********
}
catch (Exception e) {
System.out.println("Error: " + e.toString());
}
sendToAll(this);
}
broadcast (dO);
if(dO.getMessage().startsWith("#")){
handlers.removeElement (this);
System.out.println("in main dis");
ChatServer.lclient.remove(this);
sendToAll_rm(this);
dO.setMessage("HAS LEFT");
this.arun = true;
try {
ChatServer.flout.append(dO.getName()+" "+"Has Left At "+datetime+"\n");
//*****************enter in jdbc********
line = dO.getName()+" "+"Has Left At "+datetime ;
//jdbtest.connmth(line);
ChatServer.connlog(line);
//*****************enter in jdbc********
ChatServer.flout.flush();
}
catch (Exception e) {
System.out.println("Error: " + e.toString());
}
broadcast (dO);
try {
//ChatServer.flout.close();
s.close ();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
} catch (IOException ex) {
System.out.println("Disconnection from "+s.getInetAddress());
} catch(ClassNotFoundException e){
} finally {
if(!(this.arun)){
handlers.removeElement (this);
System.out.println("in finally dis");
ChatServer.lclient.remove(this);
sendToAll_rm(this);
dO.setMessage("HAS LEFT");
try {
ChatServer.flout.append(dO.getName()+" "+"Has Left At "+datetime+"\n");
//*****************enter in jdbc********
line = dO.getName()+" "+"Has Left At "+datetime ;
//jdbtest.connmth(line);
ChatServer.connlog(line);
//*****************enter in jdbc********
ChatServer.flout.flush();
}
catch (Exception e) {
System.out.println("Error: " + e.toString());
}
broadcast (dO);
try {
//ChatServer.flout.close();
s.close ();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
void sendToAll(ChatHandler s) {
String s1 = "@";
synchronized( ChatServer.lclient ) {
for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {
s1 = s1 + (String)e.nextElement()+ "\n";
//out.println( msg );
}
dO.setMessage(s1);
s1 = "";
try {
s.o.writeObject (dO);
s.o.flush ();
} catch (IOException ex) {
s=null;
}
}
}
Enumeration getOutputStreams() {
return ChatServer.lclient.elements();
}
void sendToAll_rm(ChatHandler s) {
String s1 = "@";
synchronized( ChatServer.lclient ) {
for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {
s1 = s1 + (String)e.nextElement()+ "\n";
//out.println( msg );
}
dO.setMessage(s1);
s1 = "";
broadcast (dO);
}
}
protected static void broadcast (DataObject obj) {
DataObject TempObject = new DataObject();
TempObject.setName(obj.getName());
TempObject.setMessage(obj.getMessage());
synchronized (handlers) {
Enumeration e = handlers.elements ();
while (e.hasMoreElements ()) {
ChatHandler c = (ChatHandler) e.nextElement ();
try {
synchronized (c.o) {
c.o.writeObject (TempObject);
}
c.o.flush ();
} catch (IOException ex) {
c=null;
}
}
}
}
}
class jdbcLog{
public void connmth (String line)throws SQLException {
System.out.println("..in..");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("..in1..");
String url = "jdbc:oracle:thin:@prophet.njit.edu:1521:course";
System.out.println("..in2..");
Connection conn = DriverManager.getConnection (url, "kbp27", "0iq50oAzY");
System.out.println("..in3..");
Statement stmt = conn.createStatement ();
System.out.println("..in4..");
//String command = "CREATE TABLE CHATSERVERLOG (logrecords varchar2(70))";
//stmt.executeUpdate(command);
String command = "INSERT INTO CHATSERVERLOG VALUES (" +"'"+ line +"')";
System.out.println("..in5..");
stmt.executeUpdate(command);
System.out.println("..in6..");
stmt.close();
conn.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -