写错了 你可别笑我
我是做J2EE的 J2ME不怎么明白
兔子不吃窝边草
服务器程序
package com;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class AppServer implements Runnable{
ServerSocket server;
Socket fromClient;
Thread serverThread;
public AppServer(){
System.out.println("FunChat聊天服务器启动.......");
try{
server = new ServerSocket(1001);
serverThread = new Thread(this);
serverThread.start();
}catch(Exception e){
System.out.println("不能启动线程:"+e.toString());
}
}//AppServer结束
public static void main(String[] args){
new AppServer();
}
public void run() {
try{
while(true){
fromClient =server.accept();
Connect con = new Connect(fromClient);
}
}catch(Exception e){
System.out.println("不能监听客服请求:"+e.toString());
}
}
}
class Connect {
ObjectOutputStream streamToClient;
int ctr = 0;
BufferedReader streamFromClient;
static Vector vector;
static Vector vctrList;
String message="";
static String str = new String("用户列表");
static
{
vector = new Vector(1,1);
vctrList = new Vector(1,1);
vctrList.addElement((String)str);
}
int verify(String mesg)
{
try{
RandomAccessFile RAS = new RandomAccessFile("UsrPwd.txt","r");
int i= 0;
String str="";
while((RAS.getFilePointer())!=(RAS.length())){
str = RAS.readLine();
if(str.equals(mesg)){
ctr = 1;
break;
}
}
RAS.close();
}catch(Exception e){
System.out.println("发生异常:"+e.toString());
}
return ctr;
}
int checkFile(String mesg){
int chk = 1;
try{
RandomAccessFile RS = new RandomAccessFile("UsrPwd.txt","r");
int i =0;
String str="";
String colon = new String(":");
int index = ((String)mesg).lastIndexOf(colon);
String userName = (String)mesg.substring(0, index);
while((RS.getFilePointer())!=(int)(RS.length()))
{
str = RS.readLine();
int index1 = ((String)str).lastIndexOf(colon);
String usrName=(String)str.substring(0,index1);
if(usrName.equals(userName)){
chk = 0;
break;
}
}
RS.close();
}
catch(Exception ee){
System.out.println("发生异常:"+ee.toString());
}
return chk;
}
public Connect(Socket inFromClient){
String msg="";
String mesg = "";
try{
streamFromClient = new BufferedReader(
new InputStreamReader(inFromClient.getInputStream())
);
streamToClient = new ObjectOutputStream(inFromClient.getOutputStream());
msg = streamFromClient.readLine();
if((msg.equals("来自定时器")))
{
streamToClient.writeObject(vector);
streamToClient.writeObject(vctrList);
}else if(msg.equals("登陆信息")){
msg = streamFromClient.readLine();
int ver = verify(msg);
if(ver==1){
String colon = new String(":");
int index =((String)msg).lastIndexOf(colon);
String userName =(String)msg.substring(0,index);
if(!(vctrList.indexOf((String)userName)>0)){
streamToClient.writeObject("欢迎");
vctrList.addElement((String)userName);
}
}else{
streamToClient.writeObject("拒绝登陆");
}
}
else if(msg.equals("注册信息")){
msg = streamFromClient.readLine();
int ret = checkFile(msg);
if(ret==0){
streamToClient.writeObject("用户已经存在");
}
if(ret==1){
FileOutputStream out = new FileOutputStream("UsrPwd.txt",true);
PrintStream p = new PrintStream(out);
p.println();
p.println(msg);
p.close();
streamToClient.writeObject("已注册");
}
}
else if(msg.equals("用户退出")){
String remUser = streamFromClient.readLine();
boolean b = vctrList.removeElement((String)remUser);
}
else{
message = message + msg;
vector.addElement((String)message);
streamToClient.writeObject(vector);
}
}catch(Exception e3){
System.out.println("无发获取客户端的的流对象:"+e3.toString());
}
finally{
try{
inFromClient.close();
}catch(IOException e0){
System.out.println("发生异常:"+e0.toString());
}
}
}
}
登陆程序
package com;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class Login extends JFrame implements ActionListener
{
JLabel lblUserName;
JLabel lblUserPwd;
JTextField txtUsrName;
JPasswordField txtUsrPwd;
JButton btnLogin;
JButton btnCancel;
JButton btnRegister;
String UsrName;
char[] UsrPwd;
String strPwd;
Socket toServer;
ObjectInputStream streamFromServer;
PrintStream streamToServer;
public Login(){
this.setTitle("登陆");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
gbCons.gridx = 0;
gbCons.gridy = 0;
lblUserName = new JLabel("用户名");
panel.add(lblUserName,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 0;
txtUsrName = new JTextField(20);
panel.add(txtUsrName,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 1;
lblUserPwd = new JLabel("密码");
panel.add(lblUserPwd,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 1;
txtUsrPwd = new JPasswordField(20);
panel.add(txtUsrPwd,gbCons);
JPanel btnPanel = new JPanel();
btnLogin = new JButton("登陆");
btnPanel.add(btnLogin);
btnLogin.addActionListener(this);
btnRegister = new JButton("注册");
btnPanel.add(btnRegister);
btnRegister.addActionListener(this);
btnCancel = new JButton("取消");
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
gbCons.gridx = 1;
gbCons.gridy = 3;
gbCons.anchor = GridBagConstraints.EAST;
panel.add(btnPanel,gbCons);
this.getContentPane().add(panel);
this.setVisible(true);
this.setSize(450,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void showdlg(){
JOptionPane.showMessageDialog(this,
"用户名密码不正确","错误信息",JOptionPane.ERROR_MESSAGE);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if(button.equals(btnCancel)){
this.dispose();
}
else if(button.equals(btnRegister))
{
new Register();//调用注册程序
this.dispose();
}else{
try{
toServer = new Socket("机器名",1001);
streamFromServer = new ObjectInputStream(toServer.getInputStream());
streamToServer = new PrintStream(toServer.getOutputStream());
streamToServer.print("登陆信息");
UsrName = txtUsrName.getText();
UsrPwd = txtUsrPwd.getPassword();
strPwd = new String(UsrPwd);
streamToServer.println(UsrName+":"+UsrPwd);
String fromServer = (String)streamFromServer.readObject();
if(fromServer.equals("欢迎")){
new clientInt(UsrName);
this.dispose();
}
else{
this.showdlg();
}
}
catch(Exception el)
{
System.out.println("发生异常:"+el.toString());
}
}
}
public static void main(String args[]){
new Login();
}
}
注册程序
package com;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class Register extends JFrame implements ActionListener{
JLabel lblHeading;
JLabel lblUserName;
JLabel lblUserPwd;
JLabel lblCnfUserPwd;
JLabel lblFrstName;
JLabel lblLstName;
JLabel lblAge;
JLabel lblEmpId;
JLabel lblSex;
String usrName;
char[] usrPwd;
char[] cnfPwd;
String frstName;
String lstName;
String age;
String empid;
Socket toServer;
ObjectInputStream streamFromServer;
PrintStream streamToServer;
JComboBox lstSex;
JTextField txtUserName;
JPasswordField txtUsrPwd;
JPasswordField txtCnfUsrPwd;
JTextField txtFrstName;
JTextField txtLstName;
JTextField txtAge;
JTextField txtEmpId;
Font f; Color r;
JButton btnSubmit;
JButton btnCancel;
public Register(){
this.setTitle("注册界面");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
gbCons.gridx = 0;
gbCons.gridy = 0;
lblHeading = new JLabel("注册信息");
Font f = new Font("Monospaced",Font.BOLD,24);
lblHeading.setFont(f);
Color c = new Color(0,200,0);
lblHeading.setForeground(new Color(131,25,38));
lblHeading.setVerticalAlignment(SwingConstants.TOP);
gbCons.anchor = GridBagConstraints.WEST;
panel.add(lblHeading,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 1;
lblUserName = new JLabel("用户名称");
gbCons.anchor = GridBagConstraints.WEST;
panel.add(lblUserName,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 1;
txtUserName = new JTextField(15);
panel.add(txtUserName,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 2;
lblUserPwd = new JLabel("密码");
panel.add(lblUserPwd,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 2;
txtUsrPwd = new JPasswordField(15);
panel.add(txtUsrPwd,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 3;
lblCnfUserPwd = new JLabel("确认密码");
panel.add(lblCnfUserPwd,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 3;
txtCnfUsrPwd = new JPasswordField(15);
panel.add(txtCnfUsrPwd,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 4;
lblEmpId = new JLabel("雇员编号");
panel.add(lblEmpId,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 4;
txtEmpId = new JTextField(15);
panel.add(txtEmpId,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 5;
lblFrstName = new JLabel("名:");
panel.add(lblFrstName,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 5;
txtFrstName = new JTextField(15);
panel.add(txtFrstName,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 6;
lblLstName = new JLabel("性:");
panel.add(lblLstName,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 6;
txtLstName = new JTextField(15);
panel.add(txtLstName,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 7;
lblAge = new JLabel("年龄:");
panel.add(lblAge,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 7;
txtAge = new JTextField(3);
panel.add(txtAge,gbCons);
gbCons.gridx = 0;
gbCons.gridy = 8;
lblSex = new JLabel("性别:");
panel.add(lblSex,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 8;
String[] sex={"男","女"};
JComboBox lstSex = new JComboBox(sex);
lstSex.setSelectedIndex(0);
panel.add(lstSex,gbCons);
JPanel btnPanel = new JPanel();
btnSubmit = new JButton("提交");
btnPanel.add(btnSubmit);
btnSubmit.addActionListener(this);
btnCancel = new JButton("取消");
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
gbCons.gridx = 0;
gbCons.gridy = 9;
gbCons.anchor = GridBagConstraints.EAST;
panel.add(btnPanel,gbCons);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(450,400);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if(button.equals(btnCancel)){
this.dispose();
}else{
int ver = verify();
if(ver==1){
try{
toServer = new Socket("机器名",1001);
streamFromServer =new ObjectInputStream
(toServer.getInputStream());
streamToServer = new PrintStream
(toServer.getOutputStream());
streamToServer.println("注册信息");
usrName = txtUserName.getText();
usrPwd = txtUsrPwd.getPassword();
String pwd = new String(usrPwd);
streamToServer.println(usrName+":"+pwd);
String fromServer = (String) streamFromServer.readObject();
if(fromServer.equals("已注册")){
new Login();
this.dispose();
}
else if(fromServer.equals("用户已成在")){
showUsrExists();
}
}
catch(Exception el){
System.out.println("发生异常:"+el.toString());
}
}
}
}
int verify() {//检验用户信息的正确性
int ctr = 0;
int intAge = 0;
try{
usrName = txtUserName.getText();
usrPwd = txtUsrPwd.getPassword();
cnfPwd = txtCnfUsrPwd.getPassword();
frstName = txtFrstName.getText();
lstName = txtLstName.getText();
age = txtAge.getText();
empid = txtEmpId.getText();
String strUsrPwd = new String(usrPwd);
String strCnfPwd = new String(cnfPwd);
try{
intAge = (int)Integer.parseInt(age.trim());
}catch(Exception ee){
showErrordlgInt();
}
if((usrName.length()>0)&&(strUsrPwd.length()>0)&&(strCnfPwd.length()>0)
&&(frstName.length()>0)&&(lstName.length()>0)&&(intAge>21)&&(intAge<58)
&&(empid.length()>0)&&(strUsrPwd.equals(strCnfPwd))){
ctr = 1;
return ctr;
}else{
showErrordlg();
}
}catch(Exception r){
System.out.println("发生异常:"+r.toString());
}
return ctr;
}
int flg=0;
void showErrordlg() {
JOptionPane.showMessageDialog(this,
"非法输入",
"错误",
JOptionPane.ERROR_MESSAGE);
}
private void showErrordlgInt() {
JOptionPane.showMessageDialog(this,
"年龄不正确",
"错误",
JOptionPane.ERROR_MESSAGE);
}
void showUsrExists() {
JOptionPane.showMessageDialog(this,
"用户已存在",
"错误",
JOptionPane.ERROR_MESSAGE);
}
public static void main(String args[]){
Register fs = new Register();
}
}
聊天室
package com;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class clientInt extends JFrame implements ActionListener {
Timer t = new Timer(5000,new TimerAction());
String usr_name;
public String remUser;
int messageCount = 0;
String name;
PrintStream streamToServer;
ObjectInputStream streamFromServer;
Socket toServer;
JTextArea txtMessages;
JTextArea txtListUsers;
JTextField txtMsg;
JButton msgSendBtn;
JButton userLoginBtn;
JButton userRegisterBtn;
JButton userLogoutBtn;
JLabel lblChatRoom;
JLabel lblUserList;
JScrollPane jspSendMsgPane;
JScrollPane jspTxtMsgPane;
JScrollPane jspUserListPane;
JTextField textWriteMsg;
String message;
int nSend;
class TimerAction implements ActionListener{
Socket toServer;
ObjectInputStream streamFromServer;
PrintStream streamToServer;
public void actionPerformed(ActionEvent e2) {
try{
toServer = new Socket("机器名",1001);
streamFromServer = new ObjectInputStream(toServer.getInputStream());
streamToServer = new PrintStream(toServer.getOutputStream());
message = txtMsg.getText();
streamToServer.println("来自定时器");
Vector vector = (Vector) streamFromServer.readObject();
Vector vector1 =(Vector) streamFromServer.readObject();
txtListUsers.setText("");
for(int j=1;j<vector1.capacity();j++){
txtListUsers.append((String)vector1.elementAt(j));
txtListUsers.append("\n");
}
int i = messageCount;
for(;i<vector.capacity();i++){
txtMessages.append((String)vector1.elementAt(i));
txtMessages.append("\n");
}
messageCount = i;
}catch(Exception e){
System.out.println("发生异常:"+e.toString());
}
}
}
public clientInt(String nm){
remUser = nm;
usr_name = nm;
this.setTitle("FunChat:"+usr_name);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
gbCons.gridx = 0;
gbCons.gridy = 0;
lblChatRoom = new JLabel("聊天室",SwingConstants.LEFT);
panel.add(lblChatRoom,gbCons);
gbCons.gridx = 1;
gbCons.gridy = 0;
lblUserList = new JLabel("在线用户",SwingConstants.LEFT);
panel.add(lblUserList,gbCons);
gbCons.gridx =0;
gbCons.gridy = 1;
gbCons.gridwidth = 1;
gbCons.gridheight =1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
txtMessages = new JTextArea(25,35);
txtMessages.setEditable(false);
jspTxtMsgPane = new
JScrollPane(txtMessages,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(jspTxtMsgPane,gbCons);
gbCons.gridx =1;
gbCons.gridy = 1;
gbCons.gridwidth = 1;
gbCons.gridheight =1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
txtListUsers = new JTextArea(25,10);
txtListUsers.setEditable(false);
jspUserListPane = new
JScrollPane(txtListUsers,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(jspUserListPane,gbCons);
gbCons.gridx =0;
gbCons.gridy = 2;
gbCons.gridwidth = 1;
gbCons.gridheight =1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
txtMsg = new JTextField(35);
jspSendMsgPane = new
JScrollPane(txtMsg,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(jspSendMsgPane,gbCons);
gbCons.gridx =1;
gbCons.gridy = 2;
gbCons.gridwidth = 1;
gbCons.gridheight =1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
gbCons.anchor = GridBagConstraints.WEST;
msgSendBtn = new JButton("发送");
panel.add(msgSendBtn,gbCons);
msgSendBtn.addActionListener(this);
JPanel btnPanel = new JPanel();
userLogoutBtn = new JButton("退出");
userLogoutBtn.addActionListener(this);
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent el)
{
try{
Socket toServer;
PrintStream streamToServer;
ObjectInputStream streamFromServer;
toServer = new Socket("机器名",1001);
streamToServer = new PrintStream(toServer.getOutputStream());
streamToServer.println("用户提出");
streamToServer.println("remUser");
}catch(Exception ee){
System.out.println("发生异常:"+ee.toString());
}
}
}
);
btnPanel.add(userLogoutBtn);
gbCons.gridx =0;
gbCons.gridy = 3;
gbCons.gridwidth = 1;
gbCons.gridheight =1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
gbCons.anchor = GridBagConstraints.EAST;
gbCons.fill = GridBagConstraints.HORIZONTAL;
panel.add(btnPanel,gbCons);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(546,567);
t.start();
}
public void actionPerformed(ActionEvent me) {
JButton button = (JButton) me.getSource();
if(button.equals(userLogoutBtn)){
try{
toServer = new Socket("机器名",1001);
streamToServer = new PrintStream(toServer.getOutputStream());
streamToServer.println("用户TT出");
streamToServer.println(remUser);
}catch(Exception oo){
System.out.println("发生异常:"+oo.toString());
}
this.dispose();
}else{
int num1=0,num2= 0, res = 0;
String name = "";
try{
toServer = new Socket("机器名",1001);
streamFromServer = new ObjectInputStream(toServer.getInputStream());
streamToServer = new PrintStream(toServer.getOutputStream());
message = txtMsg.getText();
String msg = message;
streamToServer.println(usr_name+":"+msg);
txtMsg.getText();
Vector vector = (Vector) streamFromServer.readObject();
int i = messageCount;
for(;i<vector.capacity();i++){
txtMessages.append((String)vector.elementAt(i));
}
messageCount = i;
}catch(Exception on){
System.out.println("发生异常:"+on.toString());
}
}
}
public static void main(String[] args) {
String nm = new String();
clientInt CI = new clientInt(nm);
}
}