/*=============服務端================*/java
/**
* 服務器程序 在9999端口監聽
* 能夠經過控制檯輸入來回應客戶端
* @author xiaoluo
* @qq 3087438119
*/服務器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;app
public class MyServer1 extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextArea jta = null;
JTextField jtf = null;
JButton jb = null;
JPanel jpl= null;
JScrollPane jsp = null;
//把信息發給客戶端的對象
PrintWriter pw =null;
public static void main(String [] args){
MyServer1 ms = new MyServer1();
}
public MyServer1(){
jta = new JTextArea();
jtf = new JTextField(20);
jb= new JButton("發送");
jb.addActionListener(this);
jpl = new JPanel();
jsp = new JScrollPane(jta);
jpl.add(jtf);
//jpl.add(jta);
jpl.add(jb);
this.add(jsp,"Center");
this.add(jpl,"South");
this.setTitle("和佳客服");
this.setSize(400,300);
this.setVisible(true);
//服務器監聽
try {
ServerSocket ss= new ServerSocket(9988);
//等待客戶端鏈接
Socket s = ss.accept();
//讀取客戶端發來的信息
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader brd = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(),true);
while(true){
//讀取客戶端信息
String info = brd.readLine();
//把客戶端信息寫到信息欄
jta.append("客戶端:"+info+"\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}jsp
@Override
public void actionPerformed(ActionEvent e) {
//若是用戶按下發送信息按鈕
if(e.getSource()==jb){
//把服務器在框裏寫內容發送給客戶端
String info = jtf.getText();
jta.append("服務端:"+info+"\r\n");
pw.println(info);//發送
jtf.setText("");//清空輸入框
}
}ide
}this
/*===============客戶端====================*/.net
/**
* 客戶端
* @author xiaoluo
* @qq 3087438119
*/orm
import java.io.*;
import java.net.*;
import javax.swing.*;對象
public class MyClient1 extends JFrame implements ActionListener{
JTextArea jta = null;
JTextField jtf = null;
JButton jb = null;
JPanel jpl= null;
JScrollPane jsp = null;
//把信息發給客戶端的對象
PrintWriter pw =null;
public static void main(String [] args){
MyClient1 mc = new MyClient1();
}
public MyClient1(){
jta = new JTextArea();
jtf = new JTextField(20);
jb= new JButton("發送");
jb.addActionListener(this);
jpl = new JPanel();
jsp = new JScrollPane(jta);
jpl.add(jtf);
//jpl.add(jta);
jpl.add(jb);
this.add(jsp,"Center");
this.add(jpl,"South");
this.setTitle("客戶端");
this.setSize(400,300);
this.setVisible(true);
try {
Socket s = new Socket("127.0.0.1",9988);
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(),true);
while(true){
//不停地讀取從服務器端發來的信息
String info = br.readLine();
jta.append("服務端:"+info+"\r\n");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}get
@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //若是用戶按下發送信息按鈕 if(e.getSource()==jb){ //把服務器在框裏寫內容發送給客戶端 String info = jtf.getText(); jta.append("客戶端:"+info+"\r\n"); pw.println(info);//發送 jtf.setText("");//清空輸入框 } }}