public class m implements Runnable {
Socket sock;
int id;
ArrayList list;
int i=0;
public void run() {
Iterator it=list.iterator();
DataOutputStream dout;
try {
dout = new DataOutputStream(sock.getOutputStream() );
System.out.println(sock.getInetAddress()+"...."+id);
dout.writeUTF( "sock"+id+"connect!");
while(true){
DataInputStream in=new DataInputStream(sock.getInputStream() );
String str=in.readUTF();
System.out.println(str+'\n');
while(it.hasNext())
{
Socket s=(Socket)(it.next()) ;
DataOutputStream out=new DataOutputStream(s.getOutputStream() );//我想用这一部分做转发功
out.writeUTF( str); //能,但出了问题!搞了好就
} //久!就来这求救!
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public m(Socket s,int i,ArrayList list)
{
sock=s;
id=i;
this.list=list;
}
}
//////////server///////
public class mm {
static int n=0;
public static void main(String[] args) {
System.out.println("start");
ArrayList list=new ArrayList();
try {
ServerSocket s=new ServerSocket(3333);
while(true){
n=n+1;
Socket client=s.accept() ;
list.add(client);
m m1=new m(client,n,list);
Thread th=new Thread(m1);
th.start();
System.out.println("connect!!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
////////client/////
public class cc extends JFrame implements ActionListener {
JTextField f1;
JTextArea a1;
JScrollPane s1;
ServerSocket server;
Socket sock;
public cc()
{
f1=new JTextField(30);
a1=new JTextArea(35,35);
s1=new JScrollPane(a1);
getContentPane().add(f1,"North");
getContentPane().add(s1,"Center");
setSize(300,300);
setVisible(true);
f1.addActionListener(this);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
try {
sock=new Socket(InetAddress.getLocalHost(),3333);
//a1.append( "server connect ok");
DataOutputStream dout=new DataOutputStream(sock.getOutputStream() );
dout.writeUTF("client\n");
while(true){
DataInputStream in=new DataInputStream(sock.getInputStream() );
a1.append(in.readUTF()+"\n") ;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new cc();
}
public void actionPerformed(ActionEvent arg0) {
DataOutputStream dout;
try {
dout = new DataOutputStream(sock.getOutputStream() );
String s=f1.getText() ;
dout.writeUTF(s);
a1.append(s+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}