服務器:
import java
import
public class MyServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(
while(true){
Socket client=server
BufferedReader in=new BufferedReader(new InputStreamReader(client
PrintWriter out=new PrintWriter(client
while(true){
String str=in
System
out
out
if(str
break;
}
client
}
}
}
這裡僅僅只是加了一個外層的While循環
那麼下面的問題是如何使用線程
import
import java
public class MultiUser extends Thread{
private Socket client;
public MultiUser(Socket c){
this
}
public void run(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(client
PrintWriter out=new PrintWriter(client
//Mutil User but can parallel
while(true){
String str=in
System
out
out
if(str
break;
}
client
}catch(IOException ex){
}finally{
}
}
public static void main(String[] args)throws IOException{
ServerSocket server=new ServerSocket(
while(true){
//transfer location change Single User or Multi User
MultiUser mu=new MultiUser(server
mu
}
}
}
我的類直接從Thread類繼承了下來
以上的代碼使用的是
BufferedReader in=new BufferedReader(new InputStreamReader(client
PrintWriter out=new PrintWriter(client
還有一種方法是使用
DataInputStream isFromClient = new DataInputStream(client
DataOutputStream osToClient = new DataOutputStream(client
關於這兩種輸入輸出流的不同
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26834.html