大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
建立一个监听器,在Session里放一个List保存每个用户的Session,这样能看到服务器上有哪些用户登陆了,以及他们的登录信息。每个用户的操作记录,建议你用log4j记录下就可以了,比方说,写到一个文本里。
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的开平网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
服务端:
package com.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java点虐 .ServerSocket;
import java点虐 .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("正在监听...");
if((socket = server.accept()) != null){
System.out.println("接收到一个请求"+ 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点虐 .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();
}
}
}
}
先开服务端,再开客户端。
俺以前写的...
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jp2_bt2){//判断按下了相应按钮。
if(!jp1_jtf.getText().trim().equals("")){//判断用户名和密码是否为空
User u = new User();//创建登陆用户向信息
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());//从服务器得到用户信息
new Thread(new QQClientMain(uinfo)).start();//打开新窗口
this.dispose();//关闭登陆窗口
}else{
JOptionPane.showMessageDialog(this, "用户名或密码错误");
}
}
}
}
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("产生随机数");
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("产生随机数")) {
randomButton.setText("停止");
t.stop = false;
new Thread(t).start();
} else if(actionCommand.equals("停止")) {
randomButton.setText("产生随机数");
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();
}
}