使用Socket套接字進行編程,完成的是基於TCP可靠服務實現服務器與客戶端的雙通信。
- package com.han;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.*;
- import java.net.*;
- import javax.swing.JDialog;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- /**
- * 本程序實現了一個TCP程序的服務器編程部分。
- * 使用Socket套接字進行編程,完成的是基於TCP可靠服務實現與客戶端的雙通信。
- * 客戶端的編程見本包中的類Client
- * @author HAN
- *
- */
- @SuppressWarnings("serial")
- public class Server extends JDialog{
- private BufferedReader reader;
- private PrintWriter writer;
- private ServerSocket server;
- private Socket socket;
- private JTextArea ta=new JTextArea();
- private JScrollPane sp=new JScrollPane(ta);
- private JTextField tf=new JTextField();
- Container cc;
- public Server(String title) {
- setTitle(title);
- addWindowListener(new WindowAdapter() {
- public void windowClosing (WindowEvent we) {
- dispose(); //撤銷dialog一切相關資源
- System.exit(0); //正常退出程序
- }
- });
- cc=getContentPane();
- setLayout(null);
- ta.setLineWrap(true);
- ta.setEditable(false);
- sp.setBounds(0,0,300,342);
- tf.setBounds(0,342,300,25);
- cc.add(sp);
- cc.add(tf);
- tf.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- try {
- writer=new PrintWriter(socket.getOutputStream(),true);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- writer.println(tf.getText());
- ta.append("User1:"+tf.getText()+'\n');
- tf.setText("");
- }
- });
- }
- void getserver(){
- try{
- server=new ServerSocket(8998);
- ta.append("服務器套接字已經創建成功\n");
- while(true){
- ta.append("等待客戶機的連接\n");
- socket=server.accept();
- ta.append("客戶機已連接\n");
- reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- getClientMessage();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- private void getClientMessage(){
- try {
- while(true){
- String news=reader.readLine();
- if(news!=null){
- ta.append("User2:"+news+"\n");
- }else{
- ta.append("User2(客戶端) 已斷開鏈接\n");
- break;
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try{
- if(reader!=null){
- reader.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(socket!=null){
- try {
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- Server user1=new Server("User1");
- user1.setBounds(0,0,300,400);
- user1.setResizable(false);
- user1.setVisible(true);
- user1.getserver();
- }
- }
- package com.han;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import javax.swing.*;
- /**
- * 本程序實現了一個TCP程序的客戶端編程部分。
- * 使用Socket套接字進行編程,完成的是基於TCP可靠服務實現與服務器的雙通信。
- * 服務器的編程見本包中的類Server
- * 可以在不同的平台與不同的機器上運行,只是要把代碼中寫的IP地址修改為運行服務器程序Server的機器的IP地址。
- * @author HAN
- *
- */
- @SuppressWarnings("serial")
- public class Client extends JDialog{
- private BufferedReader reader;
- private PrintWriter writer;
- private Socket socket;
- private JTextArea ta=new JTextArea();
- private JScrollPane sp=new JScrollPane(ta);
- private JTextField tf=new JTextField();
- Container cc;
- public Client(String title) {
- setTitle(title);
- addWindowListener(new WindowAdapter() {
- public void windowClosing (WindowEvent we) {
- dispose(); //撤銷dialog一切相關資源
- System.exit(0); //正常退出程序
- }
- });
- cc=getContentPane();
- setLayout(null);
- ta.setLineWrap(true);
- ta.setEditable(false);
- sp.setBounds(0,0,300,342);
- tf.setBounds(0,342,300,25);
- cc.add(sp);
- cc.add(tf);
- tf.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- writer.println(tf.getText());
- ta.append("User2:"+tf.getText()+'\n');
- tf.setText("");
- }
- });
- }
- private void connect(){
- ta.append("嘗試連接\n");
- try {
- socket=new Socket("192.168.1.3",8998);
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- writer=new PrintWriter(socket.getOutputStream(),true);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ta.append("完成連接\n");
- }
-
- private void getClientMessage(){
- try {
- reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- while(true){
- String news=reader.readLine();
- if(news!=null){
- ta.append("User1:"+news+"\n");
- }else{
- ta.append("User1(服務器) 已斷開鏈接,等待服務器重連之時,重啟User2(客戶端)進行通信\n");
- break;
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- ta.append("User1(服務器) 已斷開鏈接,等待服務器重連之時,重啟User2(客戶端)進行通信\n");
- e.printStackTrace();
- }
- try{
- if(reader!=null){
- reader.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(socket!=null){
- try {
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- Client user2=new Client("User2");
- user2.setBounds(0,0,300,400);
- user2.setVisible(true);
- user2.setResizable(false);
- user2.connect();
- user2.getClientMessage();
- }
- }