1.服务端
Java代码
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服务器启动");
}
public void service(){
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run(){
try {
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服务器启动");
}
public void service(){
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run(){
try {
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.客户端
Java代码
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadClient {
public static void main(String[] args) {
int numTasks = 10;
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < numTasks; i++) {
exec.execute(createTask(i));
}
}
// 定义一个简单的任务
private static Runnable createTask(final int taskID) {
return new Runnable() {
private Socket socket = null;
private int port=8821;
public void run() {
System.out.println("Task " + taskID + ":start");
try {
socket = new Socket("localhost", port);
// 发送关闭命令
OutputStream socketOut = socket.getOutputStream();
socketOut.write("shutdown\r\n".getBytes());
// 接收服务器的反馈
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String msg = null;
while ((msg = br.readLine()) != null)
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
}
Java代码
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服务器启动");
}
public void service(){
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run(){
try {
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服务器启动");
}
public void service(){
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run(){
try {
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.客户端
Java代码
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadClient {
public static void main(String[] args) {
int numTasks = 10;
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < numTasks; i++) {
exec.execute(createTask(i));
}
}
// 定义一个简单的任务
private static Runnable createTask(final int taskID) {
return new Runnable() {
private Socket socket = null;
private int port=8821;
public void run() {
System.out.println("Task " + taskID + ":start");
try {
socket = new Socket("localhost", port);
// 发送关闭命令
OutputStream socketOut = socket.getOutputStream();
socketOut.write("shutdown\r\n".getBytes());
// 接收服务器的反馈
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String msg = null;
while ((msg = br.readLine()) != null)
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
}
发表评论
-
NIO连接socket
2009-10-15 22:45 1728NIO连接socket 早就听说JAVA的NIO比IO牛一些 ... -
java socket例子
2009-10-13 16:21 1347package test.bwl; impor ... -
Log4j使用总结
2008-10-29 09:22 2274以前的项目中为了能够 ... -
JAVA验证码带中文
2008-10-10 15:55 4836该类包含数字验证码和中文汉字验证码 creatImage() ... -
自己用HTTCLIENT写的一个小程序
2008-06-02 15:56 1687实现功能:匹配A表中是否B中的手机号码,如果 没有就从B查 ... -
用JAVA实现文件copy的代码示例
2008-05-26 11:14 1634/** * 拷贝一个文件到另一个目录 */ public bo ... -
用Java实现文本形式的树状结构显示
2008-02-21 14:19 1320代码如下,可根据需求衍生出各种格式的输出和使用。 packa ...
相关推荐
网址:http://www.javathinker.org/ JavaThinker是一个致力于Java技术研究与分享的网站,提供了深度的技术分析文章和实践指南。对于想要深入了解Java核心技术和最新进展的开发者来说,这里是获取高质量学习材料的好...
按照Java Web的标准,Servlet的.class文件应该放在WEB-INF/classes目录下,如果类位于包结构中,如org.javathinker.HelloServlet,那么实际的路径应为(D) helloapp/WEB-INF/classes/org/javathinker/HelloServlet....
学员成绩管理系统(来源:http://www.javathinker.org) 问题描述 用Java编写一个程序实现学员成绩管理,每个学员包括3门课的成绩,从键盘输入学员信息(最多输入20个,但最少并不知道),包括学号(String)姓名...
问题 3: 假设在 helloapp 应用中有一个 HelloServlet 类,它在 web.xml 文件中的配置如下:<servlet> <servlet-name> HelloServlet </servlet-name> <servlet-class>org.javathinker.HelloServlet</servlet-class> ...
- HelloServlet类位于`org.javathinker`包下,其class文件应放在`helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class`,符合Java Web应用的标准目录结构,其中`WEB-INF/classes`用于存放编译后的class...
在JSP(Java Server Pages)中,网页开发涉及多种概念和技术,这些在jsp测试一(含答案).doc文档中有所体现。以下是对文档中提到的一些关键知识点的详细解释: 1. **JSP URL访问**: - 当一个JSP页面位于应用的特定...
在J2EE面试中,Java相关的技术,尤其是JSP和Servlet是常见的考察点。以下是对这些面试题目的详细解析: 1. 访问JSP页面的URL:在J2EE应用中,如果JSP文件位于`%TOMCAT_HOME%/webapps/helloapp/hello/hello.jsp`,...
2. **Servlet类的位置**:Servlet类的编译后class文件应该放在Web应用的`WEB-INF/classes`目录下,如果Servlet类名为`org.javathinker.HelloServlet`,那么class文件路径应为`(D) helloapp/WEB-INF/classes/org/...
2. servlet类的位置:按照Java Web应用的标准目录结构,HelloServlet类的class文件应该放在(D) `helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class`。这是因为在WEB-INF目录下的classes子目录存放编译后...
<servlet-class>org.javathinker.HelloServlet</servlet-class> </servlet> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> ``` **那么在浏览器端访问...
正确答案是(D)helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class。在Java Web应用中,Servlet类的编译后的class文件通常放置在WEB-INF/classes目录下,按照包结构组织。 3. 第三题考察Servlet映射。...
- B `main()`:Java程序的主方法。 - C `service()`:正确,处理请求的方法。 - D `doGet()`:处理GET请求的方法。 - **正确答案:** C `service()` - **解析:** `service()`方法是Servlet程序的主要入口点,...
这本书引用了《Java面向对象编程》作为参考书籍,并提供了技术支持网址www.javathinker.org以及作者孙卫琴的联系邮箱linda_j2ee@yahoo.com.cn。 Java编程语言的主要特性包括: 1. **面向对象**:Java是一种面向...
根据示例,如果 `HelloServlet` 类位于 `org.javathinker` 包下,则其 `.class` 文件应放在 `helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class` 路径下。`WEB-INF` 目录通常用于存放Web应用的私有资源...
javathinker网站mysql版的帖子精华。
19. Servlet部署:HelloServlet类位于org.javathinker包下,对应的class文件应放在helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class目录下。 20. 打印命令位置:与打印输出相关的命令通常在文件菜单中...
8. **JSP类文件位置**:在JSP应用中,类文件通常放置在`WEB-INF/classes`目录下,对于包结构,如`org/javathinker/HelloServlet.class`,类文件应按照包名反向放置。 9. **Servlet的`getWriter()`**:在Servlet中,...
【计算机二级等级考试知识...25. Servlet部署:在Web应用中,HelloServlet类位于org.javathinker包下,对应的class文件应放在WEB-INF/classes/org/javathinker/HelloServlet.class目录下,符合Java Web应用的部署规范。