zoukankan      html  css  js  c++  java
  • Java Socket编程之简单的C/S一对一的实现

    Server:

    package com.wjy.server;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    /**
     * caution:boot the server first.
     * @author Jiyuan Wang
     *
     */
    public class TalkServer extends JFrame implements ActionListener{
        private static JButton send=null;
        private static JTextField inputText=null;
        private static JTextArea showText=null;
        private static ServerSocket serverSocket=null;
        private static Socket socket=null;
        private static DataInputStream dataInputStream=null;
        private static DataOutputStream dataOutputStream=null;
        public TalkServer(){
            send=new JButton("Send");
            send.addActionListener(this);
            send.setEnabled(true);
            
            inputText=new JTextField();
            
            showText=new JTextArea();
            
            add(send,BorderLayout.NORTH);
            add(inputText, BorderLayout.SOUTH);
            add(showText, BorderLayout.CENTER);
            
            setTitle("Server");
            setBounds(1000,0,400,400);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    public static void main(String[] args)
    {
        new TalkServer();
        try {
            serverSocket=new ServerSocket(4170);
            socket=serverSocket.accept();
            showText.append("Connect successd.\n");
            dataInputStream=new DataInputStream(socket.getInputStream());
            dataOutputStream=new DataOutputStream(socket.getOutputStream());
            while (true) {
                showText.append("Client said: "+dataInputStream.readUTF()+"\n");
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try {
            dataOutputStream.writeUTF(inputText.getText());
            dataOutputStream.flush();   //本人一直把这个程序调试不通,最后发现没加这句话导致服务端显示不了客户端的输入。
            showText.append("I said: "+inputText.getText()+"\n");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    }

    Client:

    package com.wjy.client;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    /**
     * caution:boot the server first.
     * @author Jiyuan Wang
     *
     */
    
    public class TalkClient extends JFrame implements ActionListener{
        private static JButton send=null;
        private static JTextField inputText=null;
        private static JTextArea showText=null;
        private static Socket socket=null;
        private static DataInputStream inputStreamReader=null;
        private static DataOutputStream outputStreamWriter=null;
        public TalkClient(){
            send=new JButton("Send");
            send.addActionListener(this);
            send.setEnabled(true);
            
            inputText=new JTextField();
            
            showText=new JTextArea();
            
            add(send,BorderLayout.NORTH);
            add(inputText, BorderLayout.SOUTH);
            add(showText, BorderLayout.CENTER);
            
            setTitle("Client");
            setBounds(0,0,400,400);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    public static void main(String[] args)
    {
        new TalkClient();
        try {
            socket=new Socket("127.0.0.1",4170);
            inputStreamReader=new DataInputStream(socket.getInputStream());
            outputStreamWriter=new DataOutputStream(socket.getOutputStream());
            while(true){
                showText.append("The Server said: "+inputStreamReader.readUTF()+"\n");
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try {
            outputStreamWriter.writeUTF(inputText.getText());
            outputStreamWriter.flush();   //本人一直把这个程序调试不通,最后发现没加这句话导致服务端显示不了客户端的输入。
            showText.append("I said: "+inputText.getText()+"\n");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    }
  • 相关阅读:
    MongoDB 基础
    类加载流程,类加载机制及自定义类加载器详解
    Object中有哪些方法及其作用
    Intellij IDEA 导入Maven项目
    用IDEA开发Spring程序
    深入浅出 Java 8 Lambda 表达式
    UUID.randomUUID()简单介绍
    json字符串转成 json对象 json对象转换成java对象
    字符串转 Boolean 的正确方式
    获取JSON中所有的KEY
  • 原文地址:https://www.cnblogs.com/wangjiyuan/p/socketsimplecs.html
Copyright © 2011-2022 走看看