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. Redis之RDB与AOF

    AOF定义:以日志的形式记录每个操作,将Redis执行过的所有指令全部记录下来(读操作不记录),只许追加文件但不可以修改文件,Redis启动时会读取AOF配置文件重构数据 换句话说,就是Redis重启 ...

  2. db nosql redis / Redis Sentinel

    s Redis基础原理和日常操作方法 http://itsm.cns*****.com/kindeditor/img/20170527/759128afca564051b491e6a51a5bad40 ...

  3. JAVA记录-@Controller和RequestMapping注解代码介绍

    1.spring-mvc.xml加入配置 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component ...

  4. 用jsch.jar实现SFTP上传下载删除【转】【补】

    java类: 需要引用的jar: jsch-0.1.53.jar 关于jsch有篇文章关于目录的问题写得非常好:http://www.zzzyk.com/show/9f02969327434a6c.h ...

  5. jdk8中奖Date转换为String格式的方法

    public static String getLocalDateStr(Date date,String formatter) { DateTimeFormatter dateTimeFormatt ...

  6. Bellman-Ford算法:POJ No.3169 Layout 差分约束

    #define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...

  7. 四、移植 JZ2440 开发板

    4.1 移植第一步 前面已经分析过了 .config 的过程,可以知道移植需要用到的文件: .config 文件 arch/arm/cpu 下的文件 board 目录  .config 文件是根据后面 ...

  8. 基于802.11Fuzz技术的研究

    转自安全客 关于无线的Fuzz最开始接触了解时,国内基本毛线都搜不到.经过几个月的资料搜集和学习,将大约全网的fuzz资料整理翻译分析并读懂写下,就为填补国内空白,也希望无线爱好者能多多交流. 在各个 ...

  9. 标签页QTabWidget

    样式: import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication, QWidget, QTab ...

  10. JavaScript之Dom操作【删除当前节点】

    //最新更新:2017-11-25 //现在可以通过更强大而快捷的方式为所有的HTMLElement元素的Dom操作扩展新的方法[注意事项:处理HTMLElemnt元素时,此法对IE-8无效] //原 ...