项目结构

项目设计

客户端同时大量请求服务端,服务端多线程处理连接,并发序列化获得客户端发送的数据,并做出处理。

IClients

package simple.socket;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Date;

import java.util.Random;

public class IClients {

    /**

     * IServer Class

     * </br> Start 1000 client connect the server

     */

    public static void main(String[] args) {

        // start 20 clients to connect the Server

        for (int j = 0; j < 1000; j++) {

            new Thread(new Runnable() {

                @Override

                public void run() {

                    try {

                        Socket socket = new Socket("127.0.0.1", 90);

                        ObjectOutputStream outputStream = new ObjectOutputStream(

                                socket.getOutputStream());

                        Message message = new Message();

                        message.setUserId("" + new Random().nextLong());

                        message.setMessageDate(new Date().toString());

                        message.setUserName(Thread.currentThread().getName());

                        message.setMessage(Thread.currentThread().getName()

                                + "xxxx");

                        outputStream.writeObject(message);

                        outputStream.close();

                        socket.close();

                        Thread.sleep(10);

                    } catch (UnknownHostException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }).start();

        }

    }

}

IServer

package simple.socket;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class IServer {

    /**

     * IServer Class

     * Server main class

     */

    ServerSocket serverSocket = null;

    final static int SERVER_PORT = 90;

    Socket connectSocket = null;

    public IServer() {

        try {

            System.out.println("服务器启动了。。。");

            serverSocket = new ServerSocket(SERVER_PORT);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public void startServer() {

        try {

            while (!serverSocket.isClosed()) {

                connectSocket = serverSocket.accept();

                if (connectSocket != null) {

                    IServerHandel handle = new IServerHandel(connectSocket);

                    new Thread(handle).start();

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public void shutdownServer() {

        if (!serverSocket.isClosed()) {

            try {

                serverSocket.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    public static void main(String[] args) {

        new IServer().startServer();

    }

}

IServerHandel

package simple.socket;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.net.Socket;

public class IServerHandel implements Runnable {

    /**

     * IServerHandel Class

     * </br>handle the Muliti-clients Socket connections

     */

    Socket connectSocket = null;

    ObjectInputStream inputStream = null;

    Message message = null;

    public IServerHandel(Socket connectSocket) {

        this.connectSocket = connectSocket;

    }

    public void readMsg() {

        try {

            inputStream = new ObjectInputStream(connectSocket.getInputStream());

            if ((message = (Message) inputStream.readObject()) != null) {

                System.out.println(message.getMessage());

            }

            inputStream.close();

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

    }

    @Override

    public void run() {

        readMsg();

    }

}

Message

package simple.socket;

import java.io.Serializable;

public class Message implements Serializable {

    /**

     * message Object

     */

    private static final long serialVersionUID = -8256700753720022810L;

    String userId = "";

    String userName = "";

    String message = "";

    String messageDate = "";

    public String getUserId() {

        return userId;

    }

    public void setUserId(String userId) {

        this.userId = userId;

    }

    public String getUserName() {

        return userName;

    }

    public void setUserName(String userName) {

        this.userName = userName;

    }

    public String getMessage() {

        return message;

    }

    public void setMessage(String message) {

        this.message = message;

    }

    public String getMessageDate() {

        return messageDate;

    }

    public void setMessageDate(String messageDate) {

        this.messageDate = messageDate;

    }

}

多线程Server client的更多相关文章

  1. Qt搭建多线程Server

    起因是MySQL在Android上没有驱动.也就是说,移动端想要访问远程数据库,必须通过一台(或多台)PC进行中转. 中转PC作为Server,接受来自移动端Socket访问数据库的要求,Server ...

  2. key-value 多线程server的Linux C++实现

    项目需求 整体思路 网络通信 字符解析 数据存储与查询 1 存储管理 2 数据查询 多线程 待改进未实现的想法 GitHub源代码 项目需求 设计一个基于Socket或基于HTTP的server,服务 ...

  3. 基于QT的多线程server

    // thread.cpp #include "thread.h" Thread::Thread(int socketDescriptor, QObject *parent) : ...

  4. 基于I/O的Server/Client实现

    在前面的文章中讲了基于NIO实现的Server/Client.本文就讲讲基于同步堵塞式I/O实现的Server/Client好与前面的NIO中的Server/Client进行对照. 网络编程中须要解决 ...

  5. NetMQ(ZeroMQ)Client => Server => Client 模式的实现

    ØMQ (也拼写作ZeroMQ,0MQ或ZMQ)是一个为可伸缩的分布式或并发应用程序设计的高性能异步消息库.它提供一个消息队列, 但是与面向消息的中间件不同,ZeroMQ的运行不需要专门的消息代理(m ...

  6. docker报Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.19)

    docker version Client: Version: 17.05.0-ce API version: 1.24 (downgraded from 1.29) Go version: go1. ...

  7. Redis2.2.2源码学习——Server&Client链接的建立以及相关Event

    Redis中Server和User建立链接(图中的client是服务器端用于描述与客户端的链接相关的信息) Redis Server&Client链接的建立时相关Event的建立(图中的cli ...

  8. Socket编程--基础(基本server/client实现)

    IPv4套接口地址结构 IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件中 LINUX结构下的常用结构,一般创建套接字的时候都要将这个结构里面的 ...

  9. 用XMLRPC开服务进行server/client通信

    本文讲一下怎样用python的xmlrpc开服务,进行server/client的通信. 应用场景:1)需多client訪问应用程序给予应答情况--网页服务.  2)数据极大,希望载入一次.后面仅仅用 ...

随机推荐

  1. 关于shape_trans (ConnectedRegions, ConvexRegions, 'convex')的作用于对比

    * crystal.hdev: extraction of hexagonally shaped crystals via local thresholding and region post-pro ...

  2. MFC对话框中使用CHtmlEditCtrl

    MFC对话框中使用CHtmlEditCtrl 感谢原帖作者: http://blog.csdn.net/tingya/article/details/2028172 我在VS2008校正了一些代码. ...

  3. Sort命令使用

    查看haproxy日志的某一时间段的关于某个url的访问IP降序排序:           注意:排序(sort)在去重(uniq)之后使用 p 指的是从包含某一个字符到某一字符的区间的所有行打印输出 ...

  4. JS原型和继承

    //所有的函数都有一个prototype属性 function aa() { } console.info(aa.prototype); //这个prototype属性引用了一个对象,即原型,初始化时 ...

  5. 刷CM7固件 乐padA1-07专用固件

    --------------------------------------------------------------------------------               前几天在版 ...

  6. linux+php+apache web调用python脚本权限问题解决方案

    lamp : linux + apache + mysql + php 在上篇随笔中linux+php+apache调用python脚本时出现的问题的根本原因是:apache运行时使用的apache用 ...

  7. 13 Balls Problem

    今天讨论的是称球问题. No.3 13 balls problem You are given 13 balls. The odd ball may be either heavier or ligh ...

  8. APP测试之登录

    我们公司产品登录很简单 手机号为账号,每次登录输入短信验证码.验证码为6位数字.登陆后可以记录登录状态.未登录时可以微信登录然后验证或者APP登录后可以在个人中心绑定微信. 一APP内部登录 1.手机 ...

  9. CentOS 7 安装php开发环境

    安装服务 : yum install httpd httpd-devel  service httpd start 启动     安装mariadb : yum -y install mariadb* ...

  10. 对于.h文件和.c文件

    C语言中.h文件和.c文件详细解析_云止水_新浪博客http://blog.sina.com.cn/s/blog_73006d600102wcx5.html