经过前面内容的学习,我们了解了Java技术中实现网络通信的基本知识。下面将通过一个具体视力的实现过程,讲解客户端和服务器端通信的流程。

服务器端的实现文件是 Server.java,代码如下:

 import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*; class Conn extends Thread{
private JTextArea txt;
private Socket st;
private String msg = null;
private BufferedReader br = null;
private PrintStream ps;
public Conn(Socket st,JTextArea txt){
this.st = st;
this.txt = txt;
start();
}
public void run(){
try{
br = new BufferedReader(new InputStreamReader(st.getInputStream()));
ps = new PrintStream(new DataOutputStream(st.getOutputStream()));
}catch(Exception e){
System.err.println("input failed");
}
while(true){
try{
msg = br.readLine();
txt.append("从客户端收到信息:"+msg+'\n');
txt.append("信息接受时间是:"+new Date()+"\n");
Server.send(msg);
}catch(Exception e){
System.err.println("connection closed");
break;
}
}
}
public void send(String msg){
ps.println(msg);
}
}
public class Server extends JFrame{
private JTextArea txt;
private ServerSocket ss;
private static java.util.List<Conn> conns = new ArrayList<Conn>();
public Server(){
txt = new JTextArea();
this.setTitle("服务器");
this.setLayout(new BorderLayout());
this.add(new JScrollPane(txt),BorderLayout.CENTER);
this.setSize(500,300);
this.setVisible(true);
run();
}
public void run(){
try{
ss = new ServerSocket(8000);
}catch(Exception e){
System.err.println("open socket failed");
}
txt.append("服务器已经启动!"+"\n");
while(true){
try{
Socket st=ss.accept();
conns.add(new Conn(st,txt));
}
catch(IOException ex){
System.err.println(ex);
}
}
}
public static void send(String msg){
for(Conn c:conns)
c.send(msg);
}
public static void main(String args[]){
Server myserver=new Server();
}
}

客户端的实现文件是Client.java,代码如下:

 import java.io.*;
import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.net.*;
import java.util.*; public class Client extends JFrame implements ActionListener{
final JTextArea txta;
JTextField txtf;
JPanel pl;
JButton bt;
BufferedReader br;
DataOutputStream out;
PrintStream ps;
Container f = this.getContentPane();
public Client() {
f.setLayout(new BorderLayout());
txta = new JTextArea();
f.add(txta,BorderLayout.CENTER);
txtf = new JTextField(20);
bt = new JButton ("发送");
pl = new JPanel();
pl.setLayout(new FlowLayout());
pl.add(txtf);
pl.add(bt);
bt.addActionListener(this);
f.add(pl,BorderLayout.SOUTH);//The south layout constraint (bottom of container).
setTitle("信息发送端");
setSize(500,300);
setVisible(true);
run();
Thread t = new Thread(new Runnable() { public void run() {
while(true) { try{
txta.append("收到消息:"+br.readLine()+"\n");
}catch(Exception ex) {}
} }
});
t.start();
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == bt) {
String msg = txtf.getText();
try{
ps.println(msg);
txta.append("已经发送消息:"+msg+"\n");
}catch(Exception ex){
txta.append(ex.toString()+"\n");
}
}
}
private void run() {
try
{
Socket sc= new Socket("127.0.0.1",8000);
out = new DataOutputStream(sc.getOutputStream());
ps = new PrintStream(out);
br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
}
catch(IOException ex)
{
txta.append(ex.toString()+"\n");
} } public static void main(String[] args) {
Client myclient = new Client();
} }

同样是先执行服务器端,在执行客户端,效果如下:

《安卓网络编程》之第二篇 java环境下网络通信的综合应用-LMLPHP

04-26 21:47