JAVA多客户端通信
如何使服务端同时与多个客户端通信?一个ServerSocket可以accept多个Socket么?...
如何使服务端同时与多个客户端通信?
一个ServerSocket可以accept多个Socket么? 展开
一个ServerSocket可以accept多个Socket么? 展开
展开全部
给你贴一点程序
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServer {
public void serverStart(int port) {
try {
ServerSocket serverSocket = new ServerSocket(8060);
while (true) {
Socket socket = serverSocket.accept();
new Processor(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port=8060;
if(args.length==1){
port=Integer.parseInt(args[0]);
}
new WebServer().serverStart(port);
}
}
设置一个默认端口,然后去监听。采用socket+多线程技术就可以实现服务端与多个客户端通信
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Processor extends Thread {
private Socket socket;
private InputStream in;
private PrintStream out;
public final static String WEB_ROOT = "d:\\workplace\\webserver\\htdocs\\";
public Processor(Socket socket) {
this.socket = socket;
try {
in = socket.getInputStream();
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
String filename = parse(in);
sendFile(filename);
}
public String parse(InputStream in) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String filename = null;
try {
String httpMessage = br.readLine();
String[] content = httpMessage.split(" ");
if (content.length != 3) {
this.sendErrorMessage(400, "client query error");
return null;
}
System.out.println("code" + content[0] + ",name" + content[1]
+ ",http-version" + content[2]);
filename = content[2];
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
public void sendErrorMessage(int errorCode, String errorMessage) {
out.println("HTTP/1.0 " + errorCode + "" + errorMessage);
out.println("content-type: text/html");
out.println("<html>");
out.println("<title> error message");
out.println("</title>");
out.println("<body>");
out.println("<h1>ErrorCode:" + errorCode + "Message" + errorMessage
+ "</h1>");
out.println("</body>");
out.println("</html>");
}
public void sendFile(String filename) {
File file = new File(Processor.WEB_ROOT + "filename");
if (file.exists()) {
sendErrorMessage(404, "FILE NOT FOUND!");
return;
}
try {
InputStream in = new FileInputStream(file);
byte content[] = new byte[(int) file.length()];
try {
in.read(content);
out.println("HTTP/1.0 200 queryfile");
out.println("content-length:" + content.length);
out.println();
out.write(content);
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
自己去研究下吧!不懂再问,我就不详细写每行注释了
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServer {
public void serverStart(int port) {
try {
ServerSocket serverSocket = new ServerSocket(8060);
while (true) {
Socket socket = serverSocket.accept();
new Processor(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port=8060;
if(args.length==1){
port=Integer.parseInt(args[0]);
}
new WebServer().serverStart(port);
}
}
设置一个默认端口,然后去监听。采用socket+多线程技术就可以实现服务端与多个客户端通信
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Processor extends Thread {
private Socket socket;
private InputStream in;
private PrintStream out;
public final static String WEB_ROOT = "d:\\workplace\\webserver\\htdocs\\";
public Processor(Socket socket) {
this.socket = socket;
try {
in = socket.getInputStream();
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
String filename = parse(in);
sendFile(filename);
}
public String parse(InputStream in) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String filename = null;
try {
String httpMessage = br.readLine();
String[] content = httpMessage.split(" ");
if (content.length != 3) {
this.sendErrorMessage(400, "client query error");
return null;
}
System.out.println("code" + content[0] + ",name" + content[1]
+ ",http-version" + content[2]);
filename = content[2];
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
public void sendErrorMessage(int errorCode, String errorMessage) {
out.println("HTTP/1.0 " + errorCode + "" + errorMessage);
out.println("content-type: text/html");
out.println("<html>");
out.println("<title> error message");
out.println("</title>");
out.println("<body>");
out.println("<h1>ErrorCode:" + errorCode + "Message" + errorMessage
+ "</h1>");
out.println("</body>");
out.println("</html>");
}
public void sendFile(String filename) {
File file = new File(Processor.WEB_ROOT + "filename");
if (file.exists()) {
sendErrorMessage(404, "FILE NOT FOUND!");
return;
}
try {
InputStream in = new FileInputStream(file);
byte content[] = new byte[(int) file.length()];
try {
in.read(content);
out.println("HTTP/1.0 200 queryfile");
out.println("content-length:" + content.length);
out.println();
out.write(content);
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
自己去研究下吧!不懂再问,我就不详细写每行注释了
展开全部
一个ServerSocket可以accept多个Socket么?
答案是肯定的!
一个端口只能打开一个ServerSocket,再打开一个就将前一个阻塞
一个ServerSocket可以连接多个Socket
答案是肯定的!
一个端口只能打开一个ServerSocket,再打开一个就将前一个阻塞
一个ServerSocket可以连接多个Socket
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以开一个端口一直监听 一但有连接到达
就把它仍给其他线程处理 或者新开线程处理
---------------------------------------------------------
到网上搜索一个chat 聊天室小java程序
就把它仍给其他线程处理 或者新开线程处理
---------------------------------------------------------
到网上搜索一个chat 聊天室小java程序
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
利用线程
实现runable接口
或者直接继承thread类
实现runable接口
或者直接继承thread类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询