首页 > 编程语言 >socket编程

socket编程

时间:2022-08-28 23:23:14浏览次数:40  
标签:java socket read 编程 new import public

# bio 
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(9000);
while (true) {
System.out.println("Start to accept.");
Socket socket = serverSocket.accept();
new Thread(() -> {
try {
handler(socket);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}

public static void handler(Socket socket) throws IOException{
byte[] bytes = new byte[1024];
int read = socket.getInputStream().read(bytes);
if (read != -1) {
System.out.println(new String(bytes, 0, read));
}
}
}

 

# nio
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;

public class SocketServer {

    private static final Set<SocketChannel> socketChannels = new HashSet<>();
    public static void main(String[] args) throws Exception{
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(9000));
        serverSocketChannel.configureBlocking(false);

        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            if (socketChannel != null) {
                socketChannel.configureBlocking(false);
                socketChannels.add(socketChannel);
            }
            Iterator iterator = socketChannels.iterator();
            while (iterator.hasNext()) {
                SocketChannel socketChannel1 = (SocketChannel) iterator.next();
                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                int len = socketChannel1.read(byteBuffer);
                if (len > 0) {
                    System.out.println(new String(byteBuffer.array()));
                }
                if (len == -1){
                    socketChannels.remove(socketChannel1);
                }
            }
        }
    }
}

 

标签:java,socket,read,编程,new,import,public
From: https://www.cnblogs.com/wzllzw/p/16630896.html

相关文章

  • 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(上)
    学习ASP.NETCoreBlazor编程系列一——综述一、概述      Blazor是一个生成交互式客户端WebUI的框架: 使用C#代替JavaScript来创建信息丰富的交互式......
  • C++【多线程编程】之【初识线程】
    1.用c++11的thread库还是用pthread库?至于选择哪种多线程编程方案,需要根据你的实际项目、运行平台、团队协作等因素来考虑。一般而言,如果使用的是Linux操作系统,那么可以......
  • Java并发编程异步操作Future和FutureTask
    学习来源:https://www.cnblogs.com/steakliu/p/16060651.htmlFuture和FutureTaskFuture是一个接口,FutureTask是一个类,实现RunnableFuture接口,RunnableFuture接口继承Fut......
  • Java我的高效编程之环境搭建
    前言:刚毕业,工作之余写博客有利于提高技术,更是能帮助人,接下来会认认真真写好每一篇博客。希望大家多多支持。废话不多说,马上开始。这是一篇环境搭建的博客。jdk+eclipse+s......
  • 《机器人SLAM导航核心技术与实战》第1季:第2章_C++编程范式
    《机器人SLAM导航核心技术与实战》第1季:第2章_C++编程范式视频讲解【第1季】2.第2章_C++编程范式-视频讲解【第1季】2.1.第2章_C++编程范式-C++工程的组织结构-视频......
  • 编程之道
    学编程,将近一年了,从去年暑假用Java16,敲下第一行代码的时候,我开始走上了编程这条路,走了很多的路,明白了一些大道理,编程是一件长久,一件纯粹的的事情,不是一件操之过急的事情,而......
  • 学习ASP.NET Core Blazor编程系列一——综述
    一、NET6概述   .NET6是微软统一.NetCore与.NetFramework两大框架的第二个版本,微软在.NET5中开始进行这两大框架的统一之路。   .NET6将作为长期支持......
  • [ROS学习]7. 订阅者Subscriber的编程实现
    笔记参考:【ROS学习笔记】7.订阅者Subscriber的编程实现(C++和Python)基于B站ROS公开课:【古月居】古月·ROS入门21讲基于Ubuntu20.04.1、Noetic版本(部分图摘自:b站【古月居......
  • xshell ssh隧道做socket代理上网
    一、xshellxshell选择想要访问的会话右键属性 ssh隧道添加连接打开隧道窗格转移规则 发现已打开二、火狐附加组件搜索添加组件 SwitchyOmega设置......
  • Netty网络编程-服务端启动
    1、Netty的Handler模型2、服务端代码示例根据模型图可以更好的理解ServerBootstrap引导类设置Netty的属性。publicclassTimeServer{privateintport;pub......