protected Date date=new Date(); protected GregorianCalendar calendar=new GregorianCalendar();
protected RandomAccessFile logFile;
public MyConnection(Socket clientSocket) { netClient=clientSocket;
try { fromClient=new BufferedReader(new InputStreamReader(netClient.getInputStream())); toClient=new PrintStream(netClient.getOutputStream()); logFile=new RandomAccessFile("log.txt","rw"); } catch(IOException e) { try { netClient.close(); } catch(IOException err) { System.err.println("Unable to set up stream "+err); return; } } this.start();
} public void writeFile(String entry) { try { logFile.seek(logFile.length()); logFile.writeBytes(entry); } catch(IOException e) { System.out.println("can not write on to the log file"+e); } }
public boolean userLogin() { try { while(true) { toClient.println("Login:"); login=fromClient.readLine(); if (login==null) { toClient.println("error,user name can not be null,login again or enter exit quit talking"); continue; } else if(login.equals("exit")) { System.out.println("default user quiting..."); return false; } else { while(true) { toClient.println("Password:"); password=fromClient.readLine(); if(password==null) { System.out.println("password error"); continue; } else { System.out.println(login+" logged in..."); writeFile(login+" logged in..."+"\n");
calendar.setTime(date); String strTime="on date "+calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DATE)+" "+"at time "+calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+"... "; System.out.println(strTime); writeFile(strTime+"\n");
System.out.println(login+"'s password is "+password); writeFile(login+"'s password is "+password+"\n");
break; } } } break; } } catch(IOException e) {
} return true; }
public void run() { try { boolean userlogin=userLogin();
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;public class Server extends Thread
{
ServerSocket serverSocket;public Server()
{
try
{
serverSocket=new ServerSocket(1001);
System.out.println(serverSocket.toString());
}
catch(IOException e)
{
fail("could not start server",e);
}
System.out.println("server srarted...");
this.start();
}public static void fail(String str,Exception err)
{
System.out.println(str+" . "+err);
System.exit(1);
}public void run()
{
try
{
while(true)
{
Socket clientSocket=serverSocket.accept();
MyConnection con=new MyConnection(clientSocket);
}
}
catch(IOException e)
{
fail("not listening",e);
}
}public static void main(String args[])
{
new Server();
}}class MyConnection extends Thread
{
protected Socket netClient=null;
protected BufferedReader fromClient=null;
protected PrintStream toClient=null;
protected String login=null,password=null,fromMessage=null,toMessage=null;
protected Date date=new Date();
protected GregorianCalendar calendar=new GregorianCalendar();
protected RandomAccessFile logFile;
public MyConnection(Socket clientSocket)
{
netClient=clientSocket;
try
{
fromClient=new BufferedReader(new InputStreamReader(netClient.getInputStream()));
toClient=new PrintStream(netClient.getOutputStream());
logFile=new RandomAccessFile("log.txt","rw");
}
catch(IOException e)
{
try
{
netClient.close();
}
catch(IOException err)
{
System.err.println("Unable to set up stream "+err);
return;
}
}
this.start();
} public void writeFile(String entry)
{
try
{
logFile.seek(logFile.length());
logFile.writeBytes(entry);
}
catch(IOException e)
{
System.out.println("can not write on to the log file"+e);
}
}
public boolean userLogin()
{
try
{
while(true)
{
toClient.println("Login:");
login=fromClient.readLine();
if (login==null)
{
toClient.println("error,user name can not be null,login again or enter exit quit talking");
continue;
}
else if(login.equals("exit"))
{
System.out.println("default user quiting...");
return false;
}
else
{
while(true)
{
toClient.println("Password:");
password=fromClient.readLine();
if(password==null)
{
System.out.println("password error");
continue;
}
else
{
System.out.println(login+" logged in...");
writeFile(login+" logged in..."+"\n");
calendar.setTime(date);
String strTime="on date "+calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DATE)+" "+"at time "+calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+"... ";
System.out.println(strTime);
writeFile(strTime+"\n");
System.out.println(login+"'s password is "+password);
writeFile(login+"'s password is "+password+"\n");
break;
}
}
}
break;
}
}
catch(IOException e)
{
}
return true;
}
public void run()
{ try
{
boolean userlogin=userLogin();
BufferedReader stdin=new BufferedReader(new InputStreamReader((System.in)));
toMessage=": hi,"+login;
for(;userlogin;)
{
toClient.println("from zjyserver "+toMessage);
writeFile("from zjyserver "+toMessage+"\n");
String fromMessage=fromClient.readLine();
if(fromMessage.equals("exit"))
{
System.out.println(login+" exiting...");
writeFile(login+" exiting..."+"\n");
break;
}
else
{
System.out.println("from "+login+" "+fromMessage);
writeFile("from "+login+" "+fromMessage+"\n");
toMessage=stdin.readLine();
calendar.setTime(date);
String strTime="at("+calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DATE)+" "+calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+"): ";
toMessage=strTime+toMessage;
}
}
}
catch(IOException e)
{
}
finally
{
try
{
netClient.close();
}
catch(IOException err)
{
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;public class Client
{
public static void main(String args[])throws IOException
{
Socket clientSocket;
BufferedReader in=null;
PrintStream out=null;
BufferedReader stdin=null;
Date date;
GregorianCalendar calendar;
try
{
clientSocket=new Socket("192.168.0.3",1001);
out=new PrintStream(clientSocket.getOutputStream());
in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
stdin=new BufferedReader(new InputStreamReader((System.in)));
}
catch(UnknownHostException e)
{
System.err.println("unidentified hostname");
System.exit(1);
}
catch(IOException err)
{
System.out.println("couldn't I/O");
System.exit(1);
}
while(true)
{
String strFromServer=in.readLine();
System.out.println(strFromServer);
String strToServer=stdin.readLine();
if(strToServer.equals("exit"))
{
out.println(strToServer);
break;
}
else if(strFromServer.equals("Login:") || strFromServer.equals("Password:"))
{
out.println(strToServer);
}
else
{
date=new Date();
calendar=new GregorianCalendar();
calendar.setTime(date);
String strTime="at("
+calendar.get(Calendar.YEAR)+"-"
+calendar.get(Calendar.MONTH)+"-"
+calendar.get(Calendar.DATE)+" "
+calendar.get(Calendar.HOUR)+":"
+calendar.get(Calendar.MINUTE)+":"
+calendar.get(Calendar.SECOND)+"): ";
strToServer=strTime+strToServer;
out.println(strToServer);
}
}
in.close();
out.close();
stdin.close();
}
}
1。Java IO
2。Socket编程
3。图形
4。了解一些CSCW中的并发控制算法(比如乐观锁、悲观锁)