package im;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ClientMain extends JFrame implements Runnable {
/**
* @param args
*/
private Socket socket;
private int userID;
private String name;
private DataInputStream din;
private DataOutputStream dout;
String[][] list;
private JLabel lblTips=new JLabel("点击其他用户的昵称与之聊天");
private JList jList;
private Thread th;
public ClientMain(final Socket socket,int userID,String name) throws IOException, ClassNotFoundException {
this.socket = socket;
this.userID=userID;
this.name=name;
th=new Thread(this);
th.start();
din=new DataInputStream(socket.getInputStream());
dout=new DataOutputStream(socket.getOutputStream());
jList=new JList();
jList.setPreferredSize(new Dimension(200,400));
new Thread(this).start();
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
if (e.getValueIsAdjusting()==false) {
int index=((JList)e.getSource()).getSelectedIndex();
try {
dout.writeUTF(list[index][0]);
int selfPort=ClientMain.this.userID;
int oppositePort=Integer.parseInt(list[index][0]);
System.out.println(selfPort+" "+oppositePort);
new Thread(new ClientChat(selfPort+3000,oppositePort+3000,ClientMain.this.name,list[index][1])).start() ;//前者为自己的UDP端口,后者为对方的UDP端口,+3333防止端口冲突,同时标示相应用户
} catch (Exception e1) {
// TODO Auto-generated catch block
}
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
try {
DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
dout.writeUTF("#off#");
dout.close();
System.exit(EXIT_ON_CLOSE);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
setTitle(name);
add(lblTips, BorderLayout.NORTH);
add(new JScrollPane(jList),BorderLayout.CENTER);
pack();
setResizable(false);
setVisible(true);
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
byte[] buff=new byte[9999];
din.read(buff);
ByteArrayInputStream bais = new ByteArrayInputStream(buff);
ObjectInputStream ois = new ObjectInputStream(bais);
Object test=ois.readObject();
if (test.getClass()==Class.forName("java.lang.String")) {
String oppositeString=(String)test;
int oppositePort=Integer.parseInt(oppositeString);
String oppositeName="";
for (int i = 0; i < list.length; i++) {
if (list[i][0].equals(oppositeString)) {
oppositeName=list[i][1];
}
}
new Thread(new ClientChat(userID+3000,oppositePort+3000,name,oppositeName)).start();//前者为自己的UDP端口,后者为对方的UDP端口,+3333防止端口冲突,同时标示相应用户
}
else {
list = (String[][])test; //读取类
String[] names=new String[list.length];
for (int i = 0; i < list.length; i++) {
names[i]=list[i][1];
}
if (names!=null) {
jList.setListData(names);
}
}
}
}
catch (java.net.SocketException e) {
// TODO: handle exception
System.out.println("socket关闭,主窗口关闭");
}
catch (Exception e) {
// TODO: handle exception
}
}
}
评论0