java基礎第十六天

package com.qq;


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Iterator;

import java.util.Set;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTable;

import javax.swing.JTextArea;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableModel;


public class MyWindows {


JFrame jf;


// 聯繫人表格


JTable table;

// 聊天記錄


JTextArea history;


// 內容


JTextArea content;


// 按鈕發送


JButton jbn;


public MyWindows() {


iniWindow();

listenList();

}


public void iniWindow() {

// TODO Auto-generated method stub


jf = new JFrame();

jf.setLayout(null);

jf.setBounds(100, 100, 800, 600);


// 聯繫人表格

table = new JTable();

table.setBounds(550, 0, 100, 300);


// 聊天記錄

history = new JTextArea();

history.append("aaa");

history.setBounds(0, 0, 500, 300);


// 內容

content = new JTextArea();

content.setBounds(0, 320, 400, 200);

content.setVisible(true);


// 按鈕發送

jbn = new JButton("發送");

jbn.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

sendContent(content.getText());

ClientSocket.clientSend(content.getText());

content.setText("");

}

});


jbn.setVisible(true);

jbn.setBounds(550, 300, 100, 100);


jf.add(history);

jf.add(table);

jf.add(jbn);

jf.add(content);


jf.addWindowListener(new WindowAdapter() {


@Override

public void windowClosing(WindowEvent e) {

// TODO Auto-generated method stub

ClientSocket.closeClinet();

System.exit(-1);

}

});

jf.setVisible(true);


}


public void sendContent(String content) {

this.history.append("\r\n" + content);


}


public static void main(String[] arg) {


new MyWindows();

}


public void listenList() {

try {

System.out.println(123);

ServerSocket ss = new ServerSocket(6000);

while (true) {

Socket accept = ss.accept();

InputStream inputStream = accept.getInputStream();


System.out.println(321);

ObjectInputStream ois = new ObjectInputStream(inputStream);

Set<People> st = (Set<People>) ois.readObject();


Object[] array = st.toArray();


TableModel dataModel = new AbstractTableModel() {

public int getColumnCount() {

return 1;

}


public int getRowCount() {

return array.length;

}


public Object getValueAt(int row, int col) {

return ((People) array[row]).getName();

}

};

table.setModel(dataModel);


}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();


}

}

}










package com.qq;


import java.io.IOException;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;


public class ClientSocket {

private static Socket ss;

static {

try {

ss = new Socket("localhost", 5566);

ss.setKeepAlive(true);

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


public static  void clientSend(String str) {

// TODO Auto-generated methodstub

try {

// System.out.println(555555);

OutputStream outputStream = ss.getOutputStream();

outputStream.write(str.getBytes());

// outputStream.close();

// ss.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


public static void closeClinet() {

try {

ss.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

package com.qq;


import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.util.HashSet;

import java.util.Set;


public class ListPeople {


private static Set<People> peple = new HashSet<People>();

public static void add(People pp) {

peple.add(pp);

flushList();

}



/**

* @return the peple

*/

public static Set<People> getPeple() {

return peple;

}


public static void  flushList() {

for (People people : peple) {

System.out.println(987);

commClient(people.getIp(), people.getPort());

System.out.println(999);

}

}

public static void commClient(String ip, int port) {

try {

Socket ss = new Socket("localhost", port);

OutputStream outputStream = ss.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(outputStream);

oos.writeObject(peple);

oos.flush();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

package com.qq;


import java.io.Serializable;


public class People implements Serializable{


private String ip;

private String name;

private final int port = 6000;




/**

* @return the port

*/

public int getPort() {

return port;

}






public People() {

}




public People(String ip, String name) {

super();

this.ip = ip;

this.name = name;

}






public String getIp() {

return ip;

}

public void setIp(String ip) {

this.ip = ip;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

package com.qq;


import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.Socket;

import java.net.UnknownHostException;


public class ServerSocket {


public static void main(String[] args) {

// TODO Auto-generated method stub


try {

java.net.ServerSocket ss = new java.net.ServerSocket(5566);


while (true) {


Socket sk = ss.accept();

new Thread(){

public void run(){ 

try {

System.out.println(11111111);

People pp = new People(ss.getInetAddress().getLocalHost().getHostAddress(),ss.getInetAddress().getLocalHost().getHostName());

ListPeople.add(pp);

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}.start();

InputStream inputStream = sk.getInputStream();


byte[] by = new byte[100];

int ln = 0;

while ((ln = inputStream.read(by)) != -1) {

System.out.println(new String(by));


}


}


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章