import java.net.*;import java.awt.*;import java.io.*;import java.awt.event.*;import javax.swing.*;public class Server extends JFrame implements ActionListener{ TextField tf=new TextField(23); TextArea ta=new TextArea(); Button bt=new Button("out"); ServerSocket ss; Socket sk; void run(){ try{ add(tf); add(ta); add(bt); setLayout(new FlowLayout()); bt.addActionListener(this); setSize(300,300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); ss=new ServerSocket(8700);
while(true){
Socket sk=ss.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream())); String ch=br.readLine(); ta.append(ch);
System.out.println(ch); } } catch(IOException e){ e.printStackTrace(); }}public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="out") { String st=tf.getText(); try{ OutputStream os=sk.getOutputStream(); os.write(st.getBytes());
os.flush(); } catch(IOException o){} } } public static void main(String [] ii){ new Server().run(); } }
class Client extends JFrame implements ActionListener{
TextArea tf2=new TextArea();
TextField ta2=new TextField(23);
Button bt2=new Button("out");
Socket se;
void wap(){
try{
add(tf2);
add(ta2);
add(bt2);
bt2.addActionListener(this);
setLayout(new FlowLayout());
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
se=new Socket(InetAddress.getByName("127.0.0.1"),8700);
while(true){
BufferedReader la=new BufferedReader(new InputStreamReader(se.getInputStream()));
String sn=la.readLine();
// tf2.append("\n");
tf2.append(sn);
}
}
catch(IOException t){
t.printStackTrace();
}
}
public void actionPerformed( ActionEvent e){
if(e.getActionCommand()=="out"){
String st=ta2.getText();
try{
OutputStream ap=se.getOutputStream();
ap.write(st.getBytes());
ap.flush();
}
catch(IOException i){
i.printStackTrace();
}
}
}
public static void main(String args[]){
new Client().wap();
}
} 为啥点“out”后没反应呢
while(true){
Socket sk=ss.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream())); String ch=br.readLine(); ta.append(ch);
System.out.println(ch); } } catch(IOException e){ e.printStackTrace(); }}public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="out") { String st=tf.getText(); try{ OutputStream os=sk.getOutputStream(); os.write(st.getBytes());
os.flush(); } catch(IOException o){} } } public static void main(String [] ii){ new Server().run(); } }
class Client extends JFrame implements ActionListener{
TextArea tf2=new TextArea();
TextField ta2=new TextField(23);
Button bt2=new Button("out");
Socket se;
void wap(){
try{
add(tf2);
add(ta2);
add(bt2);
bt2.addActionListener(this);
setLayout(new FlowLayout());
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
se=new Socket(InetAddress.getByName("127.0.0.1"),8700);
while(true){
BufferedReader la=new BufferedReader(new InputStreamReader(se.getInputStream()));
String sn=la.readLine();
// tf2.append("\n");
tf2.append(sn);
}
}
catch(IOException t){
t.printStackTrace();
}
}
public void actionPerformed( ActionEvent e){
if(e.getActionCommand()=="out"){
String st=ta2.getText();
try{
OutputStream ap=se.getOutputStream();
ap.write(st.getBytes());
ap.flush();
}
catch(IOException i){
i.printStackTrace();
}
}
}
public static void main(String args[]){
new Client().wap();
}
} 为啥点“out”后没反应呢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货