建立一個(gè)監(jiān)聽器,在Session里放一個(gè)List保存每個(gè)用戶的Session,這樣能看到服務(wù)器上有哪些用戶登陸了,以及他們的登錄信息。每個(gè)用戶的操作記錄,建議你用log4j記錄下就可以了,比方說,寫到一個(gè)文本里。
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的開平網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
服務(wù)端:
package com.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java點(diǎn)虐 .ServerSocket;
import java點(diǎn)虐 .Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(5038);
while(true){
System.out.println("正在監(jiān)聽...");
if((socket = server.accept()) != null){
System.out.println("接收到一個(gè)請求"+ socket.getRemoteSocketAddress());
new Thread(new OperThread(socket)).start();
}
}
} catch (IOException e) {
System.out.println("waiting");
}
if(!socket.isConnected()){
socket.close();
server.close();
}
}
}
class OperThread implements Runnable{
Socket socket = null;
BufferedReader br = null;
public OperThread(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@Override
public void run() {
try {
String s = null;
while( (s = br.readLine()) != null){
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
客戶端:
package com.socket;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java點(diǎn)虐 .Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
PrintWriter pw = null;
Scanner scanner = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",5038);
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String s = null;
while((s = scanner.nextLine()) != null){
pw.println(s);
pw.flush();
}
} catch (Exception e) {
System.out.println("\nconnect error");
}finally{
if(pw != null){
pw.close();
}
try {
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
先開服務(wù)端,再開客戶端。
俺以前寫的...
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jp2_bt2){//判斷按下了相應(yīng)按鈕。
if(!jp1_jtf.getText().trim().equals("")){//判斷用戶名和密碼是否為空
User u = new User();//創(chuàng)建登陸用戶向信息
u.setUid(jp1_jtf.getText().trim());
u.setUpd(new String(jp1_jpf.getPassword()).trim());
if(ClientBO.login(u)){//登陸并判斷是否成功
System.out.println("成功");
UserInfo uinfo = ClientBO.getUserInfo(u.getUid());//從服務(wù)器得到用戶信息
new Thread(new QQClientMain(uinfo)).start();//打開新窗口
this.dispose();//關(guān)閉登陸窗口
}else{
JOptionPane.showMessageDialog(this, "用戶名或密碼錯(cuò)誤");
}
}
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestRandom extends JFrame {
JTextField field;
JButton randomButton;
RandomNumber t = new RandomNumber();
public TestRandom() {
field = new JTextField(15);
randomButton = new JButton("產(chǎn)生隨機(jī)數(shù)");
randomButton.addActionListener(new RandomListener());
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(field);
this.getContentPane().add(randomButton);
this.setSize(300,100);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
class RandomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String actionCommand = randomButton.getText();
if(actionCommand.equals("產(chǎn)生隨機(jī)數(shù)")) {
randomButton.setText("停止");
t.stop = false;
new Thread(t).start();
} else if(actionCommand.equals("停止")) {
randomButton.setText("產(chǎn)生隨機(jī)數(shù)");
t.stop = true;
}
}
}
class RandomNumber implements Runnable {
Random random;
StringBuffer num;
boolean stop = false;
public RandomNumber() {
random = new Random();
num = new StringBuffer("");
}
public void run() {
while(!stop) {
num.setLength(0);
for(int i=0;i6;i++) {
num.append(random.nextInt(9));
}
field.setText(num.toString());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new TestRandom();
}
}