我已經創建了一個處理java草圖。這個草圖是服務器。我希望這個程序能夠做的是客戶端和服務器可以相互連接和寫入消息(句子)。案例1成功,但案例2不成功。我已經解釋了每個案例的過程以及錯誤/成功的原因。案例1)在同一臺計算機(Mac)上,我啓動了服務器程序,並在終端(Mac上的「命令提示符」)中輸入了telnet local host 5204
,並將客戶端(Mac)與服務器(Mac)連接。我能夠在服務器和客戶端之間鍵入句子(或字符串),並且它是成功的。所以無論我在服務器中輸入什麼語句,客戶端都可以看到,反之亦然。 注意:服務器和客戶端都在同一臺計算機上。處理java草圖(服務器)沒有按照我想要的方式迴應
案例2)在Mac上,我啓動了服務器程序。在另一臺計算機上(Windows 7) 我通過命令提示符連接到服務器。連接成功。在這種情況下,字符串可以從服務器發送到客戶端,並且字符串對客戶端可見。 但是,當我試圖從客戶端向服務器發送字符串時,服務器只能接收字符的信息,而不是整個句子/字符串。我試圖改變端口號,客戶端設備,frameRate,但我仍然沒有成功。
這是我的問題。請評論,如果我的問題可以更清楚,或者如果我需要提供更多的細節。謝謝你的回答。
下面是我的服務器代碼:
import processing.net.*;
Server myServer;
//Strings from server and client
String typing = "";
String c = "";
void setup() {
size(400, 400);
//creating server on port 5204
myServer = new Server(this, 5204);
}
void draw() {
background(255);
//displaying server's text and client's text
fill(0);
text(typing, 100, 100);
text("Client: " + c, 100, 150);
Client client = myServer.available();
if(client != null) {
//reading input from client
c = client.readString();
c.trim();
}
}
void keyPressed() {
//Server can type sentences to client
if(key == '\n') {
myServer.write(typing + '\n');
typing = "";
}else{
typing = typing + key;
}
}