java ServerSocket
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的更多相关文章
- Java ServerSocket的服务端代码介绍
转自:http://developer.51cto.com/art/201003/190007.htm 所谓Java ServerSocket通常也称作"套接字",有不少的时候需要 ...
- Java ServerSocket详解
ServerSocket 构造方法 ServerSocket serverSocket = new ServerSocket(); ServerSocket(); //无参数 ServerSocket ...
- java ServerSocket服务端编程
public class Test { public static void main(String[] args) throws Exception{ //1. 构造ServerSocket实例,指 ...
- Java网络编程和NIO详解开篇:Java网络编程基础
Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...
- 初步接触 Java Net 网络编程
本文目的是大概了解 Java 网络编程体系,需要一点点 Java IO 基础,推荐教程 系统学习 Java IO.主要参考 JavaDoc 和 Jakob Jenkov 的英文教程<Java N ...
- LinkedIn的即时消息:在一台机器上支持几十万条长连接
最近我们介绍了LinkedIn的即时通信,最后提到了分型指标和读回复.为了实现这些功能,我们需要有办法通过长连接来把数据从服务器端推送到手机或网页客户端,而不是许多当代应用所采取的标准的请求-响应模式 ...
- Tomcat源码分析--转
一.架构 下面谈谈我对Tomcat架构的理解 总体架构: 1.面向组件架构 2.基于JMX 3.事件侦听 1)面向组件架构 tomcat代码看似很庞大,但从结构上看却很清晰和简单,它主要由一堆组件组成 ...
- netty开发教程(一)
Netty介绍 Netty is an asynchronous event-driven network application framework for rapid development o ...
- 一个简单的"RPC框架"代码分析
0,服务接口定义---Echo.java /* * 定义了服务器提供的服务类型 */ public interface Echo { public String echo(String string) ...
随机推荐
- 1024. Palindromic Number (25)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- AIC和BIC
一.模型选择之AIC和BIC 人们提出许多信息准则,通过加入模型复杂度的惩罚项来避免过拟合问题,此处我们介绍一下常用的两个模型选择方法 赤池信息准则(Akaike Information Criter ...
- CentOS Linux release 7.3破解密码详解
CentOS Linux release 7.3破解密码详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 公司最近接了一个项目,拿到客户现有的源代码,但是服务器用户密码并不知情, ...
- 读取Easy UI的DATa grid里面的所有数据
目前我知道的有两种方法: 1.读取行数,循环读出 $("#btnEcxel").click(function () { var rows = $('#tbVehicleList') ...
- H5 localStorage sessionStorage
localStorage 用于长久保存整个网站的数据,没有过期时间,除非手动去除. sessionStorage 会话存储,临时存储,当用户关闭浏览器窗口后,数据被删除. 共同方法 以 localSt ...
- 20155334 2016-2017-2 《Java程序设计》第五周学习总结
20155334 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 第八章:异常处理 Java中所有错误都会被打包为对象,在编程的时候会遇到因各种原因而导致的错 ...
- Redis 主从模式
系统:Centos6.6x64安装目录:/usr/local/主:192.168.100.103从:192.168.100.104 ,下载安装: 安装依赖: # yum install gcc tcl ...
- 日期与时间控件QDate, QTime, QDateTime
QDate类用于处理公历日期.QTime类用于处理时间.QDateTime类将QDate对象和QTime对象整合为一个对象 QDate: from PyQt5.QtCore import QDate, ...
- POJ 2503 Babelfish (STL)
题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...
- 推荐几款在Windows中比较好用的软件
gif录制软件:LICEcap 下载地址:https://www.cockos.com/licecap/ 演示