Java Socket基礎記錄筆記第二部分

2.服務端測試----ServerTest.java

1

2

3

4

5

6

7

8

9

10

11

/**

 * Created by JXJ on 2017/6/26.

 */

import javax.swing.JFrame;

public class ServerTest {

    public static  void main(String[] args){

        Server server=new Server();

        server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        server.startRununing();

    }

}

  

   

3.客戶端----Client.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

/**

 * Created by JXJ on 2017/6/26.

 */

import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Client extends JFrame {

    private JTextField userText;

    private JTextArea chatwindow;

    private ObjectInputStream input;

    private ObjectOutputStream output;

    private String message="";

    private String serverIP;

    private Socket connection;

 

    //構造方法

    public Client(String host){

        super("客戶端");

        serverIP=host;

        userText=new JTextField();

        userText.setEditable(false);

        userText.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                sendMessage(e.getActionCommand());

                userText.setText("");

            }

        });

        add(userText,BorderLayout.NORTH);

        chatwindow=new JTextArea();

        add(new JScrollPane(chatwindow),BorderLayout.CENTER);

        setSize(300,150);

        setVisible(true);

 

    }

    //建立連接

    public void startRununing(){

        try{

            connectToServer();

            setupStreams();

            whileChatting();

        }catch (EOFException eofException){

                showMessage("客戶端斷開連接! \n");

        }catch (IOException ioExceptiopn){

                ioExceptiopn.printStackTrace();

        }

        finally {

            closeCrap();

        }

    }

 

    //連接到服務端

    private void connectToServer() throws IOException {

        showMessage("正在嘗試連接服務端... \n");

        connection=new Socket(InetAddress.getByName(serverIP),6789);

        showMessage("已連接至"+connection.getInetAddress().getHostName());

    }

 

    //建立輸入輸出流

    private void setupStreams() throws IOException {

        output=new ObjectOutputStream(connection.getOutputStream());

        output.flush();

        input=new ObjectInputStream(connection.getInputStream());

        showMessage("已創建輸入輸出流... \n");

    }

    //聊天信息處理

    private void whileChatting() throws IOException {

        ableToType(true);

        do{

            try{

                message=(String)input.readObject();

                showMessage(message+"\n");

            }catch (ClassNotFoundException classNotFoundException){

                showMessage(" 未知的輸入對象類型\n");

            }

        }while(!message.equals("server-end"));

    }

    //關閉輸入輸出流和socket

    private void closeCrap() {

        showMessage("關閉客戶端連接資源\n");

        ableToType(false);

        try{

            output.close();

            input.close();

            connection.close();

        }catch (IOException ioException){

            ioException.printStackTrace();

        }

    }

    //給服務端發送信息

    private void sendMessage(String message) {

        try{

            output.writeObject("client-"+message);

            output.flush();

            showMessage("client-"+message+"\n");

        }catch (IOException ioException){

            showMessage("客戶端發送數據失敗\n");

        }

    }

    //在窗口上實時顯示聊天信息

    private void showMessage(final String text) {

        //注意窗口更新信息的方式

        SwingUtilities.invokeLater(new Runnable() {

            @Override

            public void run() {

                chatwindow.append(text);

            }

        });

    }

    //讓用戶輸入信息

    private void ableToType(final boolean tof) {

        //注意按鈕的禁用與啓用的方式

        SwingUtilities.invokeLater(new Runnable() {

            @Override

            public void run() {

                userText.setEditable(tof);

            }

        });

    }

}

  

4.客戶端測試----ClientTest.java

1

2

3

4

5

6

7

8

9

10

11

12

import javax.swing.*;

 

/**

 * Created by JXJ on 2017/6/26.

 */

public class ClientTest {

    public static  void main(String[] args){

        Client client=new Client("127.0.0.1");

        client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        client.startRununing();

    }

}

http://pp.163.com/kagoupei/pp/83247217.html

http://pp.163.com/kagoupei/pp/83246303.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章