這個程序只是簡單的兩台計算機之間的通訊
其實
服務器:
import java
import java
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 java
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
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類繼承了下來
[
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27212.html