还在吗
不好意思
我刚才网络不好 一直上不去
代码:
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.net.MalformedURLException;
public class chatApplet extends Applet implements Runnable,ActionListener{
Label userInfo;
Label messageInfo;
Label listInfo;
Button sendButton;
TextField userText;
TextArea messageText;
List userList;
URL chatURL;
URLConnection connect;
String username;
Thread pollThread=null;
volatile private boolean loggedin=false;
String servletPath;
public synchronized void inti(){
int newPriority;
if(pollThread!=null)
return;
super.init();
resize(500,300);
userInfo=new Label("Enter User Name (max 10 chars):");
messageInfo=new Label("message text:");
userText=new TextField(40);
sendButton=new Button("send");
messageText=new TextArea(10,40);
messageText.setEditable(false);
userText.addActionListener(this);
sendButton.addActionListener(this);
Panel mainp=new Panel();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx =0;
gbc.weighty =0;
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight =1;
gbc.gridwidth=10;
gbc.anchor =GridBagConstraints.CENTER;
gbc.fill =GridBagConstraints.NONE ;
mainp.setLayout(gbl);
gbl.setConstraints(userInfo, gbc);
mainp.add(userInfo);
gbc.gridy=1;
gbc.gridwidth=9;
gbc.fill =GridBagConstraints.HORIZONTAL ;
gbl.setConstraints(userText, gbc);
mainp.add(userText);
gbc.gridx=9;
gbc.gridwidth=1;
gbc.fill =GridBagConstraints.NONE ;
gbl.setConstraints(sendButton, gbc);
mainp.add(sendButton);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=10;
gbl.setConstraints(messageInfo, gbc);
mainp.add(messageInfo);
gbc.gridy=3;
gbc.weighty=100;
gbc.gridheight=10;
gbc.fill =GridBagConstraints.BOTH ;
gbl.setConstraints(messageText, gbc);
mainp.add(messageText);
Panel userp=new Panel();
userp.setLayout(new BorderLayout());
listInfo=new Label("users logged in:");
userp.add(listInfo, "North");
userList=new List(10,false);
userList.add("nont logged in");
userp.add(userList, "Center");
setLayout(new BorderLayout());
add(mainp,"Center");
add(userp,"East");
servletPath=getParameter("serverPath");
try{
chatURL=new URL(servletPath);
}catch (MalformedURLException e){
}
int currPriority=Thread.currentThread().getPriority();
if(currPriority==Thread.MIN_PRIORITY)
newPriority=Thread.MIN_PRIORITY;
else
newPriority=currPriority-1;
pollThread=new Thread(this,"ChatPoll");
pollThread.setDaemon(true);
pollThread.setPriority(newPriority);
pollThread.start();
p("chat:getting the information from chatServlet.java");
}
public synchronized void start(){
if(!isLoggedin()&&username!=null){
login();
}
if(pollThread==null||!pollThread.isAlive()){
p("chat :no poll thread!");
pollThread=new Thread(this,"Chatpoll");
pollThread.setDaemon(true);
pollThread.start();
p("chat: starting poll");
}
}
public synchronized void stop(){
if(pollThread.isAlive()){
try {
pollThread.wait();
p("chat: suspending poll");
}catch(InterruptedException e){
}
}else{
p("chat: pollThread die");
}
logout();
}
public synchronized void destroy(){
if(pollThread!=null&&pollThread.isAlive()){
pollThread=null;
p("chat:stoppint poll");
}
logout();
}
public void run(){
p("chat:starting the poll run");
while(!Thread.interrupted()){
if(isLoggedin()){
pollList();
poll();
p("chat:poll");
}else{
pollList();
p("chat:not loggedin for poll");
}
try{
Thread.sleep(1000*3);
}catch(InterruptedException e){
}
}
p("chat:exting run()");
}
private void login(){
String queryString="";
if(username==null)
return;
try{
queryString="chatServlet?mode=login&user="+URLEncoder.encode(username, "UTF-8");
}catch(UnsupportedEncodingException e){
}
p("Attempting login as "+username);
try{
connect=(new URL(chatURL,queryString)).openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();
p("made connection to"+connect);
BufferedReader in=new BufferedReader(
new InputStreamReader(connect.getInputStream()));
String response=in.readLine();
if(response.startsWith("+")){
setLoggedin(true);
showStatus("Logged into chat as user:"+username);
userInfo.setText("type message:");
p("logged in as "+username);
repaint();
}else {
showStatus("error logging in"+response);
p("could not log in as "+username);
System.err.println("error logging in"+response);
}
}catch (MalformedURLException e1){
System.err.println("MalformedURLException logging in!");
e1.printStackTrace(System.err);
showStatus("error logging in");
}catch(IOException e2){
System.err.println("IOException logging in!");
e2.printStackTrace(System.err);
showStatus("error logging in");
}
}
private void logout(){
String queryString="";
if(username==null||!isLoggedin())
return;
try{
queryString="chatServlet?mode=login&user="+URLEncoder.encode(username, "UTF-8");
}catch(UnsupportedEncodingException e){
}
p("Attempting login as "+username);
try{
connect=(new URL(chatURL,queryString)).openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();
BufferedReader in=new BufferedReader(
new InputStreamReader(connect.getInputStream()));
String response=in.readLine();
if(response.startsWith("+")){
setLoggedin(false);
showStatus("user:"+username+"logged out of chat");
}else {
showStatus("error logging out "+response);
System.err.println("error logging out"+response);
}
}catch (MalformedURLException e1){
System.err.println("MalformedURLException logging out!");
e1.printStackTrace(System.err);
showStatus("error logging in");
}catch(IOException e2){
System.err.println("IOException logging out!");
e2.printStackTrace(System.err);
showStatus("error logging in");
}
}
private void send(){
String queryString="";
String message=userText.getText();
if(message.equals(""))
return;
userText.setText("");
showStatus("sending message");
try{
queryString="chatServlet?mode=send&user="+URLEncoder.encode(username, "UTF-8");
queryString=queryString+"&message="+URLEncoder.encode(username, "UTF-8");
}catch(UnsupportedEncodingException e){
}
p("Attempting login as "+username);
try{
connect=(new URL(chatURL,queryString)).openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();
BufferedReader in=new BufferedReader(
new InputStreamReader(connect.getInputStream()));
String response=in.readLine();
if(response.startsWith("+")){
showStatus("message is send");
}else {
showStatus("error sending message "+response);
System.err.println("error logging out"+response);
}
}catch (MalformedURLException e1){
System.err.println("MalformedURLException logging out!");
e1.printStackTrace(System.err);
showStatus("error logging in");
}catch(IOException e2){
System.err.println("IOException logging out!");
e2.printStackTrace(System.err);
showStatus("error logging in");
}
}
private void poll(){
String queryString="";
if(username==null)
return;
try{
queryString="chatServlet?mode=poll&user="+URLEncoder.encode(username, "UTF-8");
}catch(UnsupportedEncodingException e){
}
try{
URL url=new URL(chatURL,queryString);
BufferedReader in=new BufferedReader(
new InputStreamReader(url.openStream()));
String nextLine=in.readLine();
if(!nextLine.startsWith("+")){
setLoggedin(true);
showStatus("error getting message from server");
System.err.println("error getting message from server");
}
nextLine=in.readLine();
while(nextLine!=null&&!nextLine.equals(".")){
System.err.println(nextLine);
messageText.append(nextLine+"\r\n");
repaint();
nextLine=in.readLine();
}
}catch(IOException e){
System.err.println("IOException !");
e.printStackTrace(System.err);
showStatus("error ");
}
}
private void pollList(){
String queryString="chatServlet?mode=list";
Vector<Object> users=new Vector<Object>();
try{
URL listURL=new URL(chatURL,queryString);
URLConnection listConn=listURL.openConnection();
listConn.setDefaultUseCaches(false);
listConn.setUseCaches(false);
listConn.connect();
BufferedReader in=new BufferedReader(
new InputStreamReader(listConn.getInputStream()));
String nextLine=in.readLine();
if(!nextLine.startsWith("+")){
showStatus("error getting userlist from server");
p("error getting userlist from server");
return;
}
nextLine=in.readLine();
while(nextLine!=null&&!nextLine.equals(".")){
p("read user:"+nextLine);
users.addElement(nextLine);
nextLine=in.readLine();
}
if(!users.isEmpty()){
userList.removeAll();
int size=users.size();
for (int i=0;i<size;i++){
userList.add((String)users.elementAt(i));
}
}else{
userList.removeAll();
userList.add("none logged in");
}
repaint();
}catch(IOException e){
System.err.println("IOException !");
e.printStackTrace(System.err);
showStatus("error logging in");
}
}
public boolean isLoggedin(){
return loggedin;
}
protected void setLoggedin(boolean newval){
loggedin=newval;
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==sendButton||evt.getSource()==userText){
if(!isLoggedin())
send();
else{
username=userText.getText();
if(username.length()>10){
showStatus("10 or fewer characters,please");
}else{
userText.setText("");
login();
}
}
}
}
private void p(String debug){
System.err.println("chat:"+debug);
}
}