public Socket accept() //等待连接,该方法阻塞
public void close() //关闭服务器套接字

ServerSocket只连一次的程序

/* this is serversocket */
package test; import java.io.*;
import java.net.*; class ServerOne extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServerOne(Socket s) throws IOException{
socket = s;
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream(),"UTF-8"));
//Enbale auto-flush;
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream(),"UTF-8")),true);
start();
}
public void run(){
try{
while(true){
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing:" + socket.getInetAddress());
out.println("str");
}
}catch(IOException e){
}finally{
try{
socket.close();
}catch(IOException e){
}
}
}
}
public class MultiServer{
static final int PORT = 5000;
public static void main(String[] args)throws IOException{
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try{
while(true){
Socket socket = s.accept();
try{
new ServerOne(socket);
}catch(IOException e){
socket.close();
}
}
}finally{
s.close();
} }}

多服务端程序实例

/* this is serversocket */
package socket; import java.io.*;
import java.net.*; class ServerOne extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServerOne(Socket s) throws IOException{
socket = s;
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream(),"UTF-8"));
//Enbale auto-flush;
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream(),"UTF-8")),true);
start();
}
public void run(){
try{
while(true){
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing:" + socket.getInetAddress() + socket.getPort());
out.println(str);
}
}catch(IOException e){
}finally{
try{
socket.close();
}catch(IOException e){
}
}
}
}
public class MultiServer{
static final int PORT = 5000;
public static void main(String[] args)throws IOException{
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try{
while(true){
Socket socket = s.accept();
try{
new ServerOne(socket);
}catch(IOException e){
socket.close();
}
}
}finally{
s.close();
}
}
}

规定线程个数的服务器

//: MultiJabberClient.java
// Client that tests the MultiJabberServer
// by starting up multiple clients.
import java.net.*;
import java.io.*; class JabberClientThread extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter = 0;
private int id = counter++;
private static int threadcount = 0;
public static int threadCount() {
return threadcount;
}
public JabberClientThread(InetAddress addr) {
System.out.println("Making client " + id);
threadcount++;
try {
socket =
new Socket(addr, MultiJabberServer.PORT);
} catch(IOException e) {
// If the creation of the socket fails,
// nothing needs to be cleaned up.
}
try {
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto-flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
start();
} catch(IOException e) {
// The socket should be closed on any
// failures other than the socket
// constructor:
try {
socket.close();
} catch(IOException e2) {}
}
// Otherwise the socket will be closed by
// the run() method of the thread.
}
public void run() {
try {
for(int i = 0; i < 25; i++) {
out.println("Client " + id + ": " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} catch(IOException e) {
} finally {
// Always close it:
try {
socket.close();
} catch(IOException e) {}
threadcount--; // Ending this thread
}
}
} public class MultiJabberClient {
static final int MAX_THREADS = 40;
public static void main(String[] args)
throws IOException, InterruptedException {
InetAddress addr =
InetAddress.getByName(null);
while(true) {
if(JabberClientThread.threadCount()
< MAX_THREADS)
new JabberClientThread(addr);
Thread.currentThread().sleep(100);
}
}
} ///:~

java ServerSocket的更多相关文章

  1. Java ServerSocket的服务端代码介绍

    转自:http://developer.51cto.com/art/201003/190007.htm 所谓Java ServerSocket通常也称作"套接字",有不少的时候需要 ...

  2. Java ServerSocket详解

    ServerSocket 构造方法 ServerSocket serverSocket = new ServerSocket(); ServerSocket(); //无参数 ServerSocket ...

  3. java ServerSocket服务端编程

    public class Test { public static void main(String[] args) throws Exception{ //1. 构造ServerSocket实例,指 ...

  4. Java网络编程和NIO详解开篇:Java网络编程基础

    Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...

  5. 初步接触 Java Net 网络编程

    本文目的是大概了解 Java 网络编程体系,需要一点点 Java IO 基础,推荐教程 系统学习 Java IO.主要参考 JavaDoc 和 Jakob Jenkov 的英文教程<Java N ...

  6. LinkedIn的即时消息:在一台机器上支持几十万条长连接

    最近我们介绍了LinkedIn的即时通信,最后提到了分型指标和读回复.为了实现这些功能,我们需要有办法通过长连接来把数据从服务器端推送到手机或网页客户端,而不是许多当代应用所采取的标准的请求-响应模式 ...

  7. Tomcat源码分析--转

    一.架构 下面谈谈我对Tomcat架构的理解 总体架构: 1.面向组件架构 2.基于JMX 3.事件侦听 1)面向组件架构 tomcat代码看似很庞大,但从结构上看却很清晰和简单,它主要由一堆组件组成 ...

  8. netty开发教程(一)

    Netty介绍 Netty is an asynchronous event-driven network application framework  for rapid development o ...

  9. 一个简单的"RPC框架"代码分析

    0,服务接口定义---Echo.java /* * 定义了服务器提供的服务类型 */ public interface Echo { public String echo(String string) ...

随机推荐

  1. ISO七层模型详解

    ISO七层模型详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我刚刚接触运维这个行业的时候,去面试时总是会做一些面试题,笔试题就是看一个运维工程师的专业技能的掌握情况,这个很 ...

  2. ELK 5.6.8 安装部署

    操作系统版本: LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64: ...

  3. js的this到底是什么意思

    首先确定一点,this在声明时确定不了,在执行时才知道指向的谁!!! call() , apply(),bind()  方法的用法 比如下面一个例子: function fn(name,age){ a ...

  4. BIO | NIO | AIO (Java版)

    几篇解释的不错的文章: BIO NIO AIO NIO.2 入门,第 1 部分: 异步通道 API 使用异步 I/O 大大提高应用程序的性能

  5. 获取客户端的请求IP地址

    获取客户端的请求IP地址 package com.microClass.util; import javax.servlet.http.HttpServletRequest; import java. ...

  6. DOM-Element对象

    一. 整体介绍  这里介绍DOM对象中Element对象. 那么何为Element对象呢?Element对象就是HTML元素,Element对象包括:元素节点.文本节点.属性节点. 下面利用一张图来总 ...

  7. vue项目首次加载过慢

    vue项目优化 浅谈 Vue 项目优化 关于vue在app首次加载缓慢的解决办法 nginx开启缓存 在http部分加入 #要想开启nginx的缓存功能,需要添加此处的两行内容! #设置Web缓存区名 ...

  8. 16. Spring boot 错误页面

      默认效果:1).浏览器,返回一个默认的错误页面 1.1 请求头 1.2返回结果 2).如果是其他客户端,默认响应一个json数据 2.1请求头 2.2返回结果 { "timestamp& ...

  9. Ubuntu18.04下vim的tab缩进设置为4个空格

    在/etc/vim/vimrc最后添加如下内容 set ts = 4 set exbandtab set autoindent

  10. Linux TCP 连接数

    查看 TCP 连接数 : 每一个 IP 访问的链接数:head 默认 前10 netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print ...