多线程完成socket
//服务器端代码
public class Service { //服务器
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket=null;
try {
//创建一个超市
serverSocket=new ServerSocket(8800);
while(true){
//超市开门 等待顾客上门购物 怎么保证超市是24消失营业的??
socket = serverSocket.accept();
//使用多线程来实现多个顾客能同时购物 同时结账
ServiceThread thread=new ServiceThread(socket);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器端代码
//服务器端需要的线程类
public class ServiceThread extends Thread {
//没启动一个线程 就相当于有一个顾客 进入 超市
Socket socket=null;
public ServiceThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
InputStream is=null;
OutputStream os=null;
ObjectInputStream ois=null; //反序列化
try {
//拿出钱包,推上购物车
is=socket.getInputStream();
os=socket.getOutputStream();
//反序列化 获取 顾客的信息
ois=new ObjectInputStream(is);
User user=(User) ois.readObject(); //读到进入超市的顾客信息
if (user!=null) {
System.out.println("服务器说:您的姓名是:"+user.getUserName());
}
//给顾客一个回应
os.write("欢迎您的光临".getBytes());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
os.close();
ois.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器需要的线程类
//客户端代码
public class Client1 { //第一个顾客
public static void main(String[] args) {
Socket socket=null;
InputStream is=null;
OutputStream os=null;
ObjectOutputStream oos=null;
//接收服务器的信息 读
BufferedReader br=null;
try {
//进入了我们指定的 超市购物
socket=new Socket("localhost", 8800);
is=socket.getInputStream();
os=socket.getOutputStream();
//序列化对象
oos=new ObjectOutputStream(os);
User user=new User("小黑黑1", "admin");
oos.writeObject(user);
//购物完毕 shutdownOutput 与 close
socket.shutdownOutput();
//接收到服务器给你说的 欢迎光临
br=new BufferedReader(new InputStreamReader(is));
String line=null;
while((line=br.readLine())!=null){
System.out.println("我是客户端:服务器 对我说:"+line);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
oos.close();
os.close();
is.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端代码
多线程完成socket的更多相关文章
- Python拾忆--多线程的socket服务器
阳光明媚的午后,想想最近要开始从写Java到写Python了,就随手打开电脑来体验一下Python与Java之间的不同吧~ 记得我还在上大二的时候,那个时候才开始学Java,最感兴趣的就是Java书最 ...
- C# 实现的多线程异步Socket数据包接收器框架
转载自Csdn : http://blog.csdn.net/jubao_liang/article/details/4005438 几天前在博问中看到一个C# Socket问题,就想到笔者2004年 ...
- 通过编写聊天程序来熟悉python中多线程及socket的用法
1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...
- 多线程Java Socket编程示例
package org.merit.test.socket; import java.io.BufferedReader; import java.io.IOException; import jav ...
- 《Unity 3D游戏客户端基础框架》多线程异步 Socket 框架构建
引言: 之前写过一个 demo 案例大致讲解了 Socket 通信的过程,并和自建的服务器完成连接和简单的数据通信,详细的内容可以查看 Unity3D -- Socket通信(C#).但是在实际项目应 ...
- 可扩展多线程异步Socket服务器框架EMTASS 2.0 (转自:http://blog.csdn.net/hulihui)
可扩展多线程异步Socket服务器框架EMTASS 2.0 (转自:http://blog.csdn.net/hulihui) 0 前言 >>[前言].[第1节].[第2节].[第3节]. ...
- 并发编程~~~多线程~~~计算密集型 / IO密集型的效率, 多线程实现socket通信
一 验证计算密集型 / IO密集型的效率 IO密集型: IO密集型: 单个进程的多线程的并发效率高. 计算密集型: 计算密集型: 多进程的并发并行效率高. 二 多线程实现socket通信 服务器端: ...
- 利用多线程使socket服务端可以与多个客户端同时通讯
利用多线程使socket服务端可以与多个客户端同时通讯 server import socket 1. 符合TCP协议的手机 server = socket.socket(socket.AF_INET ...
- day36——死锁、递归锁、信号量、GIL、多线程实现socket通信、线程池和进程池
day36 死锁现象与递归锁 死锁现象 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这 ...
随机推荐
- android HAL 教程(含实例)
http://www.cnblogs.com/armlinux/archive/2012/01/14/2396768.html Android Hal 分析 ...
- 利用cytoscape做网络图
首先做出下面的基因间相互关系图 1.准备sif文件 data.sif 网络数据文件 gene1 pp gene2 gene3 gene4 gene5 gene6 gene7 gene8 gene9 n ...
- RPM安装MySQL
# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.15-1.el6.x86_64.rpm-bundle.tar # tar x ...
- lucene 索引 demo
核心util /** * Alipay.com Inc. * Copyright (c) 2004-2015 All Rights Reserved/ */ package com.lucene.de ...
- Linux内核创建一个新进程
张雨梅 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000 创建新进程 如果同一个程序被多 ...
- miniui
//android提供了一个库minui用于简单的UI输出,源码在bootable/recovery/minui中, //gr_init()和gr_font_size()为minui库提供方法,gr_ ...
- WPF感悟
WPF感悟 UI层与逻辑层要尽可能地剥离(解耦). Routed Event和Command比Event的耦合度要低. UI层与逻辑层的“血管”是数据关联(Data Binding). 尽量不要试图通 ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 更新关系数据
Updating related data¶ 7 of 7 people found this helpful The Contoso University sample web applicatio ...
- centos7装机和初步运维
1.装机-制作U盘启动盘 CentOS7是一个比较新的版本.在服务器领域用得比较多.因为前安装软件可能没有Ubuntu那样方便,所以桌面领域好像不是很多. https://www.centos.org ...
- Sql Server中实现Mysql中的group_concat函数效果
), GuestName) , , '') as CustomerName FROM orderitem oi 以上涉及的两个表是OrderItem和Guest,以属性OrderSN和ItemId连接 ...