這程式主要是透過socket
用來server端跟clent都能夠相互對話
原來的程式只能夠一對一
也就是
一個講完換另外一個講
因為原本的程式是讀取readLine()完後,才會執行
我希望能透過java所提供的多執行緒的功能
能夠打破這樣的限制
可是透過多執行緒的過程當中
必須撰寫另外的class
我把socket所產生的物件
丟入到建構子當中
卻會產生變數讀取的問題
有人能幫我解決嗎
贈送廿點
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.Date;
public class Server{
private static ServerSocket servSock;
private static final int PORT = 23;
public static void main(String[] args)throws IOException{
System.out.println("等候對方連線中\n");
servSock = new ServerSocket(PORT);
Socket link = null;
link = servSock.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
BufferedReader type = new BufferedReader(new InputStreamReader(System.in));
PrintWriter output = new PrintWriter(link.getOutputStream(), true);
String message_from_client;
String message_from_server;
do{
output.println("歡迎來到主機伺服器:歡迎與管理員對話");
Talk_form_server from_server = new Talk_form_server( );//這邊原本丟入三個物件,產生的input type output stream物件
Talk_form_client from_client = new Talk_form_client( );//但是透過建構子傳值就出問題
from_server.start();
from_client.start();
}while(true);
}
}
class Talk_form_server extends Thread{
Talk_form_server (){
/*建構子*/
}
public void run(){
do{
message_from_server = type.readLine();
output.println(message_from_server);
}while(true);
}
}
class Talk_form_client extends Thread{
public Talk_form_client(){
/*建構子*/
}
public void run(){
do{
message_from_client = input.readLine();
System.out.println(message_from_client);
}while(true);
}
}