
JAVA多线程__Socket
发布日期:2021-05-07 00:20:25
浏览次数:20
分类:原创文章
本文共 5660 字,大约阅读时间需要 18 分钟。
JAVA多线程__Socket
#服务器
package com.hp.socket;import java.io.*;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class Server { private Map ListRecode=new HashMap<>(); /** * 多线程模拟socket * @param args */ public static void main(String[] args){ new Server().test(); } /** * */ public void test(){ // System.out.println(">>请输入要监听的端口号:"); Scanner sc = new Scanner(System.in); server ser = new server(); ser.port=sc.nextInt(); new Thread(ser).start(); } class server implements Runnable{ Integer port=null; @Override public void run() { try { System.out.println("开启服务器"); ServerSocket server = new ServerSocket(port); accep acc = new accep(); acc.server=server; new Thread(acc).start(); } catch (IOException e){ System.out.print(e); } } } /** * */ class accep implements Runnable{ ServerSocket server=null; @Override public void run() { while(true) { try { System.out.println("服务器开始监听!.."); Socket ser = server.accept(); System.out.print(ser.getRemoteSocketAddress()+"客户端请求...."); client cli = new client(); cli.clien=ser; new Thread(cli).start(); } catch (IOException e) { System.out.print(e); continue; } } } } class client implements Runnable{ Socket clien=null; @Override public void run() { if(clien!=null){ try { inp in = new inp(); in.ip=clien.getInputStream(); out ou = new out(); ou.op=clien.getOutputStream(); new Thread(in).start(); new Thread(ou).start(); } catch (IOException e) { System.out.print(e); } } } } class inp implements Runnable{ InputStream ip=null; int len=0; byte[] b=new byte[1024]; @Override public void run() { while(true){ try { while((len= ip.read(b))!=-1){ System.out.println(new String(b,0,len)); } } catch (IOException e) { System.out.print(e); break; } } } } class out implements Runnable{ OutputStream op=null; Scanner sc=new Scanner(System.in); String con=""; @Override public void run(){ if(op!=null){ while(true){ System.out.println("请输入要发送的信息:>>"); con=sc.next(); try { op.write(con.getBytes()); } catch (IOException e) { System.out.print(e); break; } } } } }}
#客户端
package com.hp.socket;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.util.Scanner;public class client { private Scanner sc=new Scanner(System.in); public static void main(String[] args){ new client().test(); } public void test(){ System.out.println("请输入Ip地址>>"); String ip=sc.next(); System.out.println("请输入端口号>>"); Integer port=sc.nextInt(); clie cli = new clie(); if(!ip.equals("")){ cli.ip=ip; } cli.port=port; new Thread(cli).start(); } class clie implements Runnable{ String ip=""; Integer port=0; @Override public void run() { try { Socket clien = new Socket(ip,port); OutputStream oup = clien.getOutputStream(); InputStream inp = clien.getInputStream(); inp in = new inp(); in.ip=inp; out ou = new out(); ou.op=oup; new Thread(in).start(); new Thread(ou).start(); } catch (IOException e) { System.out.print(e); } } } class inp implements Runnable{ InputStream ip=null; int len=0; byte[] b=new byte[1024]; @Override public void run() { while(true){ try { while((len= ip.read(b))!=-1){ System.out.println(new String(b,0,len)); } } catch (IOException e) { System.out.print(e); break; } } } } class out implements Runnable{ OutputStream op=null; Scanner sc=new Scanner(System.in); String con=""; @Override public void run(){ if(op!=null){ while(true){ System.out.println("请输入要发送的信息:>>"); con=sc.next(); try { op.write(con.getBytes()); } catch (IOException e) { System.out.print(e); break; } } } } }}
发表评论
最新留言
不错!
[***.144.177.141]2025年04月14日 16时45分49秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
firefox中angular2嵌套发送请求问题
2019-03-05
C++
2019-03-05
[CTFSHOW]PHP特性
2019-03-05
navigator对象
2019-03-05
关于EFI系统分区(ESP)你应该知道的3件事
2019-03-05
5.Mybatis复杂映射开发
2019-03-05
Servlet2.5的增删改查功能分析与实现------删除功能(四)
2019-03-05
环境配置 jdk_mysql_myeclipse8.6
2019-03-05
Session验证码的实现(2018-7-3)
2019-03-05
spring启动错误:Could not resolve placeholder
2019-03-05
日志写入xml上传ftp遇到的问题
2019-03-05
下载任意版本vmware对应的vmware tools
2019-03-05
将 github 中他人的 仓库 导入 码云中,从而 加快下载速度的 方式
2019-03-05
Java 类加载的过程 加载、验证、准备、解析、初始化
2019-03-05
JavaWeb---实现JavaBean来接收参数、请求转发、域对象
2019-03-05
瀚高数据库中 java代码类型与bit对应(APP)
2019-03-05
选择性估算器绕过行安全策略漏洞
2019-03-05
PostgreSQL中的触发器
2019-03-05
对PostgreSQL数据库结构的宏观理解
2019-03-05
Postgresql 日期和时间类型
2019-03-05