Java服务端Socket多线程通信_四海名汀_新浪博客

    在项目实施中,涉及到java方面的网络编程,主要是服务端和c#客服端之间的socket多线程通信。

    经过一段时间的学习和总结,发现用java进行网络编程确实很方便,很容易上手。这段时间的学习中,感觉收获不少。

    在网上查了一些资料,在调试的过程中,发现以下程序能实现多个客服端对服务端发送信息,但是不能实现服务端向同时向多个客服端发送信息(也就是服务端群发)功能,只能对连接中的{zh1}一个用户发送信息。要实现服务端群发功能,还需对代码作一定的修改和调整。

    首先,进行服务端的编程,作为服务端,编程主要按下面几个步骤进行:

    // 1. 创建socket
    ServerSocket ss = new ServerSocket(PortNumber);
    Socket s = ss.accept(); //xxx,产生阻塞,直到有连接才停止阻塞。

    // 2. 打开输入流、输出流
    OutputStream os = s.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);
    InputStream is = s.getInputStream();

    // 3. 根据一定的读写数据
    // --> 客户(服务端向客服端写数据)
    //os.write("Hello,I am Server!".getBytes());
    bos.write("Hello,I am Server!".getBytes());
    bos.flush(); //注意!!

    // <-- 客户(客服端向服务端写数据)
    byte[] buf = new byte[100];
    int len = is.read(buf);
    System.out.println(new String(buf, 0, len));

    // 4. 关系流和Socket
    //os.close();
    bos.close();
    is.close();
    s.close();
    ss.close();

   

    作为客户端则分为下面几个步骤,其实和服务端差不多,只是少了用ServerSocket进行监听。

    // 1. 创建socket
    Socket s = new Socket(InetAddress.getByName("localhost"),PortNumber);

    注:测试时localhost{zh0}是换成固定的IP地址。
    // 2. 打开输入流、输出流;
    OutputStream os = s.getOutputStream();
    InputStream is = s.getInputStream();

    // 3. 根据一定的读写数据
    // 客户<--服务器(服务端读数据)
    byte[] buf = new byte[100];
    int len = is.read(buf);
    System.out.println(new String(buf, 0, len));
    // 客户-->服务器(客服端读数据)
    os.write("Hello,I am Client!".getBytes());

    // 4. 关系流和Socket
    os.close();
    is.close();
    s.close();

    上面这些只是一个单线程的服务端->客户端的程序,只能进行一次通讯,多次通讯将被告知无法连接。

    这时,我们就需要用到进程Thread进行编程,将其改造成多线程聊天程序,可以同时进行多客户端的连接与发送信息。

    代码如下:

    //Server.java(服务端)

    import java.awt.*;
    import java.net.*;
    import java.io.*;

    public class Server extends Thread {
        ServerSocket skt;

        Socket Client[]=new Socket[10];;
        Socket Client1=null;

        int i = 0;

        TextArea in;

        int port,k=0,l=0;

        //BufferedReader theInputStream;

        PrintStream theOutputStream;

        //String readin;

        Face chat;

        public Server(int port, Face chat) {
            try {
                this.port = port;
                skt = new ServerSocket(port);
                this.chat = chat;
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void run() {

            chat.ta.append("等待连线......");

            while (true) {
                //System.out.print(""+i+"\n");
                try {


                Client[k] = skt.accept();

                //当有客户端连接时就新建一个子线程


                if (i < 2) {
                    ServerThread server[] = new ServerThread[10];
                     server[k]= new ServerThread(Client[k], this.chat, i);
                     l=server.length;
                     server[k].start();
                    chat.ta.append("客户端" + Client[k].getInetAddress() + "已连线\n");
                   
                    //for(int j=0;j<server.length;j++)
                    theOutputStream = new PrintStream(server[k].getClient().getOutputStream());
                    i = server[k].getI();
                    k++;
                } else {
                    //theOutputStream = new PrintStream(null);
                }

            } catch (SocketException e) {
                //chat.ta.append("连线中断!\n");

                //chat.clientBtn.setEnabled(true);
                //chat.serverBtn.setEnabled(true);
                //chat.tfaddress.setEnabled(true);
                //chat.tfport.setEnabled(true);
                //try {

                        //skt.close();
                        //Client.close();
                    //} catch (IOException err) {
                    //    chat.ta.append(err.toString());
                    //}
                } catch (IOException e) {
                    chat.ta.append(e.toString());
                }
            }

        }

        public void dataout(String data) {
            //for(int j=0;j<l;j++)
            theOutputStream.println(data);
        }
    }

    class ServerThread extends Thread {
        ServerSocket skt;

        Socket Client;

        TextArea in;

        int port;

        int i;

        BufferedReader theInputStream;

        PrintStream theOutputStream;

        String readin;

        Face chat;

 

//服务端子线程

 

        public ServerThread(Socket s, Face chat, int i) {
            this.i = ++i;
            Client = s;
            //this.port = port;
            //skt = new ServerSocket(port);
            this.chat = chat;

        }

        public int getI() {
            return this.i;
        }
        public Socket getClient() {
            return this.Client;
        }
        public void run() {

            //chat.ta.append("等待连线......");

            try {
                //Client = skt.accept();

                //chat.ta.append("客户端" + Client.getInetAddress() + "已连线\n");
                theInputStream = new BufferedReader(new InputStreamReader(Client
                        .getInputStream()));
                theOutputStream = new PrintStream(Client.getOutputStream());

                while (true) {
                    readin = theInputStream.readLine();
                    chat.ta.append(readin + "\n");
                }

            } catch (SocketException e) {
                chat.ta.append("连线中断!\n");

                chat.clientBtn.setEnabled(true);
                chat.serverBtn.setEnabled(true);
                chat.tfaddress.setEnabled(true);
                chat.tfport.setEnabled(true);
                try {
                    i--;
                    skt.close();
                    Client.close();
                } catch (IOException err) {
                    chat.ta.append(err.toString());
                }
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void dataout(String data) {
            theOutputStream.println(data);
        }
    }

 

    //Client.java(客服端)

    import java.net.*;
    import java.io.*;
    import javax.swing.Timer;
    class Client extends Thread {
        Socket skt;
        InetAddress host;
        int port;

        BufferedReader theInputStream;
        PrintStream theOutputStream;
        String readin;

        Face chat;

        public Client(String ip, int p, Face chat) {
            try {
                host = InetAddress.getByName(ip);
                port = p;
                this.chat = chat;
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void run() {
            try {
                chat.ta.append("尝试连线......");

                skt = new Socket(host, port);
                chat.ta.append("连线成功\n");

                theInputStream = new BufferedReader(new InputStreamReader(skt
                        .getInputStream()));

                theOutputStream = new PrintStream(skt.getOutputStream());
                //Timer myTimer = new Timer();
                while (true) {
                    readin = theInputStream.readLine();
                    chat.ta.append(readin + "\n");
                }
            } catch (SocketException e) {
                chat.ta.append("连线中断!\n");
                chat.clientBtn.setEnabled(true);
                chat.serverBtn.setEnabled(true);
                chat.tfaddress.setEnabled(true);
                chat.tfport.setEnabled(true);
                try {
                    skt.close();
                } catch (IOException err) {
                    chat.ta.append(err.toString());
                }
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void dataout(String data) {
            theOutputStream.println(data);
        }
    }

 

 

//软件界面,进行按键监听调用。

    //face.java(控制界面)

    import java.awt.*;
    import java.awt.event.*;

    public class Face extends Frame {
       
        private static final long serialVersionUID = 1L;
        Button clientBtn, serverBtn;
        TextArea ta;
        TextField tfaddress, tfport, tftype;
        int port;
        Client client;
        Server server;
        boolean iamserver;
        static Face frm;

        public Face() {
            clientBtn = new Button("客户端");
            serverBtn = new Button("服务器");
            ta = new TextArea("", 10, 50, TextArea.SCROLLBARS_BOTH);
            tfaddress = new TextField("192.168.1.104", 20);
            tfport = new TextField("2000");
            tftype = new TextField(50);

            tftype.addKeyListener(new TFListener());
            ta.setEditable(false);

            setLayout(new FlowLayout());
            add(tfaddress);
            add(tfport);
            add(clientBtn);
            add(serverBtn);
            add(ta);
            add(tftype);
            setSize(400, 300);
            setTitle("SOCKET通信界面");
            this.setVisible(true);

            clientBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    port = Integer.parseInt(tfport.getText());

                    client = new Client(tfaddress.getText(), port, frm);

                    client.start();

                    tfaddress.setEnabled(false);
                    tfport.setEnabled(false);
                    serverBtn.setEnabled(false);
                    clientBtn.setEnabled(false);
                }
            });

            serverBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    port = Integer.parseInt(tfport.getText());

                    server = new Server(port, frm);

                    server.start();

                    iamserver = true;
                    tfaddress.setText("成为服务器");

                    tfaddress.setEnabled(false);
                    tfport.setEnabled(false);
                    serverBtn.setEnabled(false);
                    clientBtn.setEnabled(false);
                }
            });

            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        }

        public static void main(String args[]) {
            frm = new Face();
        }

        private class TFListener implements KeyListener {
            public void keyPressed(KeyEvent e) {

                if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                    ta.append(">" + tftype.getText() + "\n");

                    if (iamserver)
                        server.dataout(tftype.getText());
                    else
                        client.dataout(tftype.getText());

                    tftype.setText("");
                }
            }

            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }
        }
    }

 

注:本文来源于

已投稿到:
郑重声明:资讯 【Java服务端Socket多线程通信_四海名汀_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——