import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
public class TCPServer {
private JFrame frmSocketclient;
static PrintWriter os;
static BufferedReader is;
private static JTextArea textArea;
public static ArrayList<String> H=new ArrayList<String>();
public static ArrayList<String> T=new ArrayList<String>();
public static ArrayList<String> L=new ArrayList<String>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
readfile();
try {
new Thread(){
public void run(){
try{
ServerSocket server=null;
try{
server=new ServerSocket(4567);
}catch(Exception e) {
System.out.println("can not listen to:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os=new PrintWriter(socket.getOutputStream());
String returnstr="";
Random r=new Random();
while(true){
line=is.readLine();
textArea.append("receive from client: "+line+"\n");
if(line.equals("Humidity")){
returnstr=H.get(r.nextInt(H.size()));
}else if(line.equals("Temperature")){
returnstr=T.get(r.nextInt(T.size()));
}else if(line.equals("Light")){
returnstr=L.get(r.nextInt(L.size()));
}
returnstr=line+"="+returnstr;
os.println(returnstr);
os.flush();
textArea.append("server reply:"+returnstr+"\n");
}
}catch(Exception e){
System.out.println("Error:"+e);
//出错,打印出错信息
}
}
}.start();
TCPServer window = new TCPServer();
window.frmSocketclient.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TCPServer() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmSocketclient = new JFrame();
frmSocketclient.setTitle("SocketServer");
frmSocketclient.setBounds(100, 100, 450, 300);
frmSocketclient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
frmSocketclient.getContentPane().add(textArea, BorderLayout.CENTER);
}
public static void readfile(){
File file = new File("src/data/proprammingProject1 humidity.dat");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
if(tempString.length()>0){
H.add(tempString);
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
File file1 = new File("src/data/proprammingProject1 temperature.dat");
BufferedReader reader1 = null;
try {
reader1 = new BufferedReader(new FileReader(file1));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader1.readLine()) != null) {
// 显示行号
if(tempString.length()>0){
T.add(tempString);
}
line++;
}
reader1.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader1 != null) {
try {
reader1.close();
} catch (IOException e1) {
}
}
}
File file11 = new File("src/data/proprammingProject1 light.dat");
BufferedReader reader11 = null;
try {
reader11 = new BufferedReader(new FileReader(file11));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader11.readLine()) != null) {
// 显示行号
if(tempString.length()>0){
L.add(tempString);
}
line++;
}
reader11.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader11 != null) {
try {
reader11.close();
} catch (IOException e1) {
}
}
}
}
}