Java网络编程例-Socket

 1 import java.io.DataInputStream;
2 import java.io.DataOutputStream;
3 import java.net.ServerSocket;
4 import java.net.Socket;
5 import java.util.Date;
6
7
8 public class Server {
9 ServerSocket serverSocket=null;
10 public Server(){
11 try{
12 //注意,请下一次运行前关闭所有程序,否则端口号被JVM占用,将出错
13 serverSocket=new ServerSocket(4115);
14 }catch (Exception e) {
15 e.printStackTrace();
16 }
17 }
18 public void ready(){
19 Socket you=null;
20 while(true){
21 try{
22 you=serverSocket.accept(); //等待
23 if(you!=null)
24 new ServerThread(you).start(); //为每一个客户建立一个处理线程
25 }catch (Exception e) {
26 // TODO: handle exception
27 e.printStackTrace();
28 continue;
29 }
30
31 }
32 }
33
34 public static void main(String[] args) {
35 Server server=new Server();
36 //在另一个线程里启动Client
37 Thread thread=new Thread(
38 new Runnable(){
39 public void run() {
40 // TODO Auto-generated method stub
41 Client client=new Client();
42 //try{Thread.sleep(1000);}catch (Exception e) {
43 // TODO: handle exception
44 //}
45 }
46 });
47 thread.start();
48 server.ready();
49
50 }
51 class ServerThread extends Thread{
52 Socket socket;
53 DataOutputStream out=null;
54 DataInputStream in=null;
55 String s=null;
56 public ServerThread(Socket t) {
57 // TODO Auto-generated constructor stub
58 socket=t;
59 try{
60 in=new DataInputStream(socket.getInputStream());
61 out=new DataOutputStream(socket.getOutputStream());
62 }catch (Exception e) {
63 // TODO: handle exception
64 }
65 }
66 @Override
67 public void run() {
68 while(true){
69 try{
70 String s=in.readUTF();
71 System.out.println("服务器端收到:"+s);
72 String send="服务器发送当前服务器时间:"+new Date();
73 out.writeUTF(send);
74 }catch (Exception e) {
75 // TODO: handle exception
76 System.out.println("客户离开");
77 break;
78 }
79 }
80 }
81 }
82 }

服务器端程序与客户端程序,直接运行Server.java

 1 import java.awt.Container;
2 import java.awt.FlowLayout;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.net.InetAddress;
8 import java.net.InetSocketAddress;
9 import java.net.Socket;
10
11 import javax.swing.Box;
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JScrollPane;
15 import javax.swing.JTextArea;
16 import javax.swing.JTextField;
17
18
19 public class Client extends JFrame
20 implements Runnable,ActionListener{
21 Socket socket=null;
22 JButton connBtn,sendBtn;
23 JTextField input;
24 JTextArea showArea;
25 DataInputStream in=null;
26 DataOutputStream out=null;
27 Thread thread=null;
28 boolean threadB=true;
29 public Client(){
30 socket=new Socket();
31 connBtn=new JButton("连接服务器");
32
33 sendBtn=new JButton("发送");
34 sendBtn.setEnabled(false); //未连接之前不可用
35 showArea=new JTextArea(8,18);
36 input=new JTextField(12);
37 Container container=getContentPane();
38 container.setLayout(new FlowLayout());
39 container.add(connBtn);
40 container.add(input);
41 container.add(sendBtn);
42 container.add(new JScrollPane(showArea));
43 sendBtn.addActionListener(this);
44 connBtn.addActionListener(this);
45
46 thread=new Thread(this); //创建线程
47 setBounds(100, 100, 360, 310);
48 setVisible(true);
49 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50 }
51 public void run() {
52 // TODO Auto-generated method stub
53 while(threadB){ //探测服务器端是否发来数据
54 try{
55 String s=in.readUTF();//取到数据之前堵塞
56 showArea.append("接收到:"+s+"\n");
57 showArea.setCaretPosition(showArea.getText().length());
58 }catch (Exception e) {
59 // TODO: handle exception
60 showArea.append("连接已断开");
61 sendBtn.setEnabled(false);
62 break; //终止线程
63 }
64 }
65 }
66 public void actionPerformed(ActionEvent e) {
67 // TODO Auto-generated method stub
68 if(e.getSource()==connBtn){ //连接服务器的按钮
69 try{
70 if(!socket.isConnected()){
71 InetAddress address=InetAddress.getByName("127.0.0.1");
72 InetSocketAddress socketAddress=new InetSocketAddress(address,4115); //端口号
73 socket.connect(socketAddress); //连接服务器
74 in=new DataInputStream(socket.getInputStream());
75 out=new DataOutputStream(socket.getOutputStream());
76 sendBtn.setEnabled(true); //发送按钮可用
77 thread.start(); //线程启动,用于接收服务器端发来的消息并显示
78
79 }
80 }catch (Exception ex) {
81 // TODO: handle exception
82 ex.printStackTrace();
83 socket=new Socket();
84 }
85
86 }else if(e.getSource()==sendBtn){
87 try{
88 String s=input.getText();
89 out.writeUTF(s);
90 }catch (Exception ex) {
91 ex.printStackTrace();
92 }
93 }
94 }
95 public static void main(String[] args) {
96 Client client=new Client();
97 }
98 }



posted @ 2011-12-20 20:49  tazi  阅读(315)  评论(0编辑  收藏  举报