简述:
建立一个简单的客户端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());
}
}
运行结果如下图:
注:运行时应服务器先给客户端发信息,以此来打通相互通信。