import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JTextArea;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* 服务器.java
*
* Created on 2012-4-5, 12:13:43
*/
/**
*
* @author admin
*/
public class 服务器 extends javax.swing.JFrame {
ServerSocket ss;
Socket socket;
/** Creates new form 服务器 */
public 服务器() {
initComponents();
jTextArea1.append("服务器:\n");
try {
ss = new ServerSocket(5001);
try {
while (true) {
Socket socket = ss.accept();
ServerThread st = new ServerThread(socket, jTextArea1);
Thread th = new Thread(st);
th.start();
}
} catch (Exception exc) {
socket.close();
}
} catch (Exception e) {
try {
ss.close();
} catch (Exception ex) {
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(50);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new 服务器().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
class ServerThread implements Runnable {
Socket socket;
JTextArea jTextArea1;
PrintWriter pw;
BufferedReader br;
ServerThread(Socket s, JTextArea jTextArea1) {
this.jTextArea1 = jTextArea1;
try {
socket = s;
pw = new PrintWriter(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (Exception e) {
e.toString();
}
}
public void run() {
jTextArea1.append("客户端连接来自" + socket.getInetAddress() + "\n");
pw.println("服务器连接成功");
pw.flush();
while (true) {
try {
String s = br.readLine();
if (s.equals(socket.getInetAddress() + "," + socket.getPort() + ":close")) {
break;
}
jTextArea1.append(s + "\n");
pw.println("发送成功");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
try{
socket.close();
}catch(Exception ex){
}
}
}
}
}
引用来自“牛牛牛牛”的答案
纯中文比qing englist强,否则class FWQ 这玩意看的你都蛋疼你都不知道这名字怎么来的。ddh(订单号)