好吧,我現在很難描述我的問題,但我會盡我所能,以便我得到一些幫助。serversocket服務器+客戶端問題
從技術上講,我有一個server.java和client.java作爲超類。我對我的服務器和客戶端連接佈局結構是這樣的
MAIN SERVER --- CLIENT/SERVER ----- CLIENT
我的主要問題是這個CLIENT/SERVER部分是調用2個不同的類是客戶端和服務器一起1個驅動類...這當我的CLIENT發送需要由MAIN SERVER端接收的內容需要通過CLIENT/SERVER部分時會產生問題。如果發生這種情況。
CLIENT當然需要與客戶端/服務器(服務器)部分進行交互,因爲它是接受CLIENT數據的服務器。但現在我希望CLIENT/SERVER中的(服務器)部分將數據傳輸到CLIENT/SERVER部分中的(CLIENT),以便它可以發送到主服務器
我怎麼可能寫允許CLIENT/SERVER互相交互,以便它們可以在它們之間傳輸數據,反之亦然?怎麼過,這是我的調用客戶端和服務器一起
public class Slave {
public static void main(String args []) throws IOException{
try{
// set Config file settings to slave mode
Config cfg = new Config("Slave");
String MasterServerIP = cfg.getProperty("ServerIP");
String MasterServerPort = cfg.getProperty("ServerPort");
String SlaveServerPort = cfg.getProperty("ListeningPort");
System.out.println("Slave Client connecting to Master Server");
// start connect to master server by calling the SlaveClient class
new SlaveClient(MasterServerIP,Integer.parseInt(MasterServerPort)).start();
int numClient = 0;
ServerSocket listener = new ServerSocket(Integer.parseInt(SlaveServerPort));
System.out.println("Server starts running");
try{
while(true){
// start listening to the server port by calling SlaveServer class
new SlaveServer(listener.accept(), numClient++, Integer.parseInt(SlaveServerPort)).start();
}
} finally {
listener.close();
}
} catch (FileNotFoundException file) {
System.out.println("File Not Found Error: "+file.getMessage());
}
}
代碼}
上面只是調用2對象類是服務器和客戶端的驅動程序類。
我會在這裏附上我slaveserver和slaveclient代碼,但就像你說的
public class SlaveServer extends Server {
private JFrame frame = new JFrame();
private JTextArea msgArea = new JTextArea();
private JTextArea connectionArea = new JTextArea();
// SlaveServer Constructor
public SlaveServer(Socket socket, int numClient, int port) {
super(socket, numClient, port);
}
public void writeToMsg(String msg){
msgArea.append(msg+"\n");
}
public void writeToConnection(String msg){
connectionArea.append(msg+"\n");
}
public void run(){
try{
startGUI();
// initial BufferedReader and PrintWriter object by binding it with Socket
BufferedReader in = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));
PrintWriter out = new PrintWriter(getSocket().getOutputStream(), true);
// welcome message send from server to client
out.println("Welcome to the Slave Server port:"+getPort()+" client #"+getNumClient());
while(true){
String readmsg = in.readLine();
writeToMsg(readmsg);
}
} catch (IOException e){
writeToMsg("Error in closing Socket");
}
writeToConnection("Connection from client #"+getNumClient()+" is closed");
}
}
public class SlaveClient extends Client{
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame();
private JTextArea msgArea = new JTextArea();
private JTextArea connectionArea = new JTextArea();
// SlaveClient Constructor
public SlaveClient(String ip, int port) {
super(ip, port);
}
public void run(){
startGUI();
Socket sock = null;
try {
sock = new Socket(getIp(), getPort());
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
out = new PrintWriter(sock.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
out.println("TEST");
// while loop for reading message from server
while(true){
try {
getMsg(in);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我已經重新編輯我的問題,上面的SlaveClient和SlaveServer代碼... – user1217609 2012-02-27 21:48:36
我建議你看看writeToMsg()方法,它看起來像它在服務器超類中,我們看不到。您可能需要修改SlaveClient以發送其他「TEST」。 – DNA 2012-02-27 22:15:55
其實我的writeToMsg是第n個相關的原因,他們只是來自SlaveServer的方法,我沒有提到它上面,但這裏是我從我的帖子再次編輯的實際代碼..他們只是方法,我用來追加我的JTextArea爲我的圖形用戶界面....現在我沒有創建任何方法發送數據,但我想到這個問題,並找到一個解決方案第一..似乎很難 – user1217609 2012-02-27 22:25:48