Java网络编程之服务器与客户端通信

简述:

建立一个简单的客户端client和服务器server。

知识点:

1.Java socket通信

2.Java IO

3.Java GUI开发


sever服务器端源代码:

    import java.io.*;  
    import java.awt.*;  
    import java.awt.event.ActionEvent;  
    import java.awt.event.ActionListener;  
    import java.util.*;  
    import javax.swing.*;  
    import javax.swing.Timer;  
      
      
    import java.net.*;  
    import java.text.DateFormat;  
    public class myServer extends JFrame implements ActionListener{  
      
      
    JScrollPane jsp=null;  
    JTextField jtf=null;  
    JTextArea jta=null;  
    JButton jb=null;  
    JPanel jp=null,jp_north=null;  
    JLabel jl=null;  
    ServerSocket ss=null;  
    Socket s=null;  
    BufferedReader br=null;  
    PrintWriter pw=null;  
    Date dt=null;  
    DateFormat medium=null;  
    Timer t=null;  
    public static void main(String[] args) {  
    // TODO Auto-generated method stub  
            new myServer();  
    }  
      
      
    public myServer()  
    {  
    jp_north=new JPanel();  
      
    t=new Timer(1000,this);  
    t.start();  
    jl=new JLabel("当前时间:"+Calendar.getInstance().getTime().toLocaleString());  
    jp_north.add(jl);  
      
    jta=new JTextArea();  
    jsp=new JScrollPane(jta);  
    jsp.getVerticalScrollBar();  
      
    jp=new JPanel();  
    jtf=new JTextField(15);  
    jb=new JButton("send");  
    jb.addActionListener(this);  
      
    jp.add(jtf);  
    jp.add(jb);  
      
    this.add(jp_north,"North");  
    this.add(jsp,"Center");  
    this.add(jp, "South");  
    this.setTitle("server");  
    this.setVisible(true);  
    this.setBounds(100, 100, 300, 300);  
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);  
      
    try {  
    s=new Socket();  
    ss=new ServerSocket(5005);  
    s=ss.accept();  
      
    br=new BufferedReader(new InputStreamReader(s.getInputStream()));  
    pw=new PrintWriter(s.getOutputStream());  
    while(true)  
    {  
    String s=br.readLine();  
    dt=new Date();  
    medium=DateFormat.getTimeInstance(DateFormat.MEDIUM);  
    jta.append(medium.format(dt)+" client: "+s+"\r\n");  
    }  
    } catch (Exception e) {  
    // TODO: handle exception  
    }  
    }  
      
      
    @Override  
    public void actionPerformed(ActionEvent e) {  
    // TODO Auto-generated method stub  
    if(e.getSource()==jb)  
    {  
    pw.println(jtf.getText());  
    dt=new Date();  
    medium=DateFormat.getTimeInstance(DateFormat.MEDIUM);  
    jta.append(medium.format(dt)+" server:"+jtf.getText()+":"+"\r\n");  
    jtf.setText("");  
    pw.flush();  
    }  
    this.jl.setText("当前时间:"+Calendar.getInstance().getTime().toLocaleString());  
    }  
    }  
client客户端源代码

    import java.io.*;  
    import java.net.Socket;  
    import java.text.DateFormat;  
    import java.awt.*;  
    import java.awt.event.ActionEvent;  
    import java.awt.event.ActionListener;  
    import java.util.*;  
    import javax.swing.*;  
    import javax.swing.Timer;  
    public class myClient extends JFrame implements ActionListener{  
      
      
    JScrollPane jsp=null;  
    JTextField jtf=null;  
    JTextArea jta=null;  
    JButton jb=null;  
    JPanel jp=null,jp_north=null;  
    JLabel jl=null;  
    BufferedReader br=null;  
    PrintWriter pw=null;  
    Socket s=null;  
    Date dt=null;  
    DateFormat medium=null;  
    Timer t=null;  
    public static void main(String[] args) {  
    // TODO Auto-generated method stub  
            new myClient();  
    }  
      
      
    public myClient()  
    {  
    jp_north=new JPanel();  
    t=new Timer(1000,this);  
    t.start();  
    jl=new JLabel("当前时间"+Calendar.getInstance().getTime().toLocaleString());  
    jp_north.add(jl);  
      
    jta=new JTextArea();  
    jsp=new JScrollPane(jta);  
    jsp.getVerticalScrollBar();  
      
    jp=new JPanel();  
    jtf=new JTextField(15);  
    jb=new JButton("send");  
    jb.addActionListener(this);  
      
    jp.add(jtf);  
    jp.add(jb);  
      
    this.add(jp_north,"North");  
    this.add(jsp,"Center");  
    this.add(jp, "South");  
    this.setTitle("client");  
    this.setVisible(true);  
    this.setBounds(500, 150, 300, 300);  
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);  
      
    try {  
    s=new Socket("127.0.0.1", 5005);  
    br=new BufferedReader(new InputStreamReader(s.getInputStream()));  
    pw=new PrintWriter(s.getOutputStream());  
      
    while(true)  
    {  
    String s=br.readLine();  
    dt=new Date();  
    medium=DateFormat.getTimeInstance(DateFormat.MEDIUM);  
    jta.append(medium.format(dt)+" server: "+s+"\r\n");  
    }  
    } catch (Exception e) {  
    // TODO: handle exception  
    }  
    }  
      
      
    @Override  
    public void actionPerformed(ActionEvent arg0) {  
    // TODO Auto-generated method stub  
    if(arg0.getSource()==jb)  
    {  
    pw.println(jtf.getText());  
    dt=new Date();  
    medium=DateFormat.getTimeInstance(DateFormat.MEDIUM);  
    jta.append(medium.format(dt)+" client"+jtf.getText()+":"+"\r\n");  
    jtf.setText("");  
    pw.flush();  
    }  
    this.jl.setText("当前时间"+Calendar.getInstance().getTime().toLocaleString());  
    }  
    }  

运行结果如下图:

  

注:运行时应服务器先给客户端发信息,以此来打通相互通信。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值