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. Telnet的三种登录方式

    Telnet的三种登录方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.华为创建telnet的三种验证方式 首先,我们可以简单的看一个拓扑图,让我们可以在亦庄的路由器上对双桥 ...

  2. maven中经常使用的插件

    tomcat插件:非常实用,特点就是不用配置tomcat,可以任意修改端口号. <plugin> <groupId>org.apache.tomcat.maven</gr ...

  3. mySQL数值类型的取值范围

    如下图,int最大为2145483647,手机号码应该用bigint

  4. java虚拟内存设置 防止内存溢出 OutOfMemory【转】【原】

    outofmemory permgen 这个问题主要还是由 java.lang.OutOfMemoryError: Java heap space 引起的. 有这两种解决方法: 1.设置环境变量 解决 ...

  5. 两年.net码农总结

    一直都是在博客园看文章,几乎每个两三天都会来,不管是看技术分享还是看经验总结,我觉得这真是个好地方. 工作两年,24.5岁,目前达到8.5K(即10W)的.net web. 文章水平不好,各位见谅了, ...

  6. Error: failed to execute 'C:\Keil\ARM\ARMCC'的解决办法

    在KEIL新建工程时,容易出现该问题,我查了一些资料,最终找到该问题解决方法: 第一步:在keil里的菜单栏依次选择Project->Manage->Components,Environm ...

  7. metasploit中meterpreter命令

    meterpreter是Metasploit框架中的一个杀手锏,通常作为漏洞溢出后的攻击载荷所使用,攻击载荷在触发漏洞后能够返回给我们一个控制通道. 常见的meterpreter命令 run scri ...

  8. JS ——document、“或”、event(事件对象)

    1.document <document>是所以HTML的最高节点,比<html>的等级还要高. <document>的第一个子节点是“!”——document.c ...

  9. Java EE之Hibernate异常总结【3】Disabling contextual LOB creation as createClob() method threw error java.lang.reflect.InvocationTargetException

    参考文献:https://stackoverflow.com/questions/4588755/disabling-contextual-lob-creation-as-createclob-met ...

  10. POJ 2503 Babelfish (STL)

    题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...